Java使用POI實現數據導出excel報表java
在上篇文章中,咱們簡單介紹了java讀取word,excel和pdf文檔內容 ,但在實際開發中,咱們用到最多的是把數據庫中數據導出excel報表形式。不只僅簡單的讀取office中的數據.尤爲是在生產管理或者財務系統中用的很是廣泛,由於這些系統常常要作一些報表打印的工做。而數據導出的格式通常是EXCEL或者PDF 。因此今天咱們來簡單看一下利用Apache POI實現數據庫中數據導出excel報表。在java中有不少實現數據導出excel報表的第三方jar包。但在比較了一下感受仍是POI相對來講比較好用。若是你們想學習其餘的導出方式能夠自行研究一下。數據庫
首先來了解一下 Apache POIapache
Apache POI 是用Java編寫的免費開源的跨平臺的 Java API,Apache POI提供API給Java程式對Microsoft Office格式檔案讀和寫的功能。Apache POI 是建立和維護操做各類符合Office Open XML(OOXML)標準和微軟的OLE 2複合文檔格式(OLE2)的Java API。用它可使用Java讀取和建立,修改MS Excel文件.並且,還可使用Java讀取和建立MS Word和MSPowerPoint文件。Apache POI 提供Java操做Excel解決方案(適用於Excel97-2008)。 下面咱們來看一下Apache POI 中提供的幾大部分的做用:數組
HSSF - 提供讀寫Microsoft Excel XLS格式檔案的功能。 工具
XSSF - 提供讀寫Microsoft Excel OOXML XLSX格式檔案的功能。 學習
HWPF - 提供讀寫Microsoft Word DOC格式檔案的功能。 字體
HSLF - 提供讀寫Microsoft PowerPoint格式檔案的功能。 this
HDGF - 提供讀Microsoft Visio格式檔案的功能。 spa
HPBF - 提供讀Microsoft Publisher格式檔案的功能。 excel
HSMF - 提供讀Microsoft Outlook格式檔案的功能。
本節咱們所要學習的是POI對excel的操做。因此咱們只需用到HSSF 這部份內容就能夠了。其餘的請你們自行研究吧。哈哈。
下面咱們就一步一步的來看一下如何建立一個excel報表:
1. 建立新的Excel工做薄
HSSFWorkbook workbook = new HSSFWorkbook();
在Excel工做簿中建一工做表,其名爲缺省值。POI中還提供了其餘的一些其餘的workbook 構造方法。下面咱們來看一下:
2.建立一個工做表
如要新建一名爲"工資表"的工做表,其語句爲:
HSSFSheet sheet = workbook.createSheet("工資表");
3.建立行
在索引0的位置建立行(最頂端的行)
HSSFRow row = sheet.createRow(0);
4.建立單元格
在索引0的位置建立單元格(左上端)
HSSFCell cell = row.createCell((short) 0);
定義單元格爲字符串類型,這個字符串類型也可在建立單元格里面設置。
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
在單元格中輸入一些內容
cell.setCellValue("增長值");
5.新建一輸出文件流,把相應的Excel工做簿 輸出到本地
FileOutputStream fOut = new FileOutputStream(outputFile);
workbook.write(fOut);
fOut.flush();
操做結束,關閉文件
fOut.close();
OK,在給單元格設置下從數據庫中讀取的數據。這樣咱們就能夠把數據庫裏面的內容導入到excel了。固然這裏咱們只是簡單的介紹了一些POI的用法。並無細講,好比設置單元格里面數據的格式。單元格的一些屬性設置。這些將會在我下面的例子中用到。也算是給你們一些示例吧。好了。最後看一個實例吧:
EXCEL報表工具類:ExportExcel.java
- package com.bzu.search.action;
-
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.IOException;
-
- import org.apache.poi.hssf.usermodel.HSSFCell;
- import org.apache.poi.hssf.usermodel.HSSFCellStyle;
- import org.apache.poi.hssf.usermodel.HSSFFont;
- import org.apache.poi.hssf.usermodel.HSSFRichTextString;
- import org.apache.poi.hssf.usermodel.HSSFRow;
- import org.apache.poi.hssf.usermodel.HSSFSheet;
- import org.apache.poi.hssf.usermodel.HSSFWorkbook;
- import org.apache.poi.hssf.util.HSSFColor;
- import org.apache.poi.hssf.util.Region;
-
-
- public class ExportExcel {
-
- private HSSFWorkbook wb = null;
-
- private HSSFSheet sheet = null;
-
-
- public ExportExcel(HSSFWorkbook wb, HSSFSheet sheet) {
- super();
- this.wb = wb;
- this.sheet = sheet;
- }
-
-
- public HSSFSheet getSheet() {
- return sheet;
- }
-
-
- public void setSheet(HSSFSheet sheet) {
- this.sheet = sheet;
- }
-
-
- public HSSFWorkbook getWb() {
- return wb;
- }
-
-
- public void setWb(HSSFWorkbook wb) {
- this.wb = wb;
- }
-
-
- public void createNormalHead(String headString, int colSum) {
-
- HSSFRow row = sheet.createRow(0);
-
-
- HSSFCell cell = row.createCell(0);
- row.setHeight((short) 400);
-
-
- cell.setCellType(HSSFCell.ENCODING_UTF_16);
- cell.setCellValue(new HSSFRichTextString("南京城區各網點進件統計報表"));
-
-
- sheet.addMergedRegion(new Region(0, (short) 0, 0, (short) colSum));
-
- HSSFCellStyle cellStyle = wb.createCellStyle();
-
- cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
- cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
- cellStyle.setWrapText(true);
-
-
- HSSFFont font = wb.createFont();
- font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
- font.setFontName("宋體");
- font.setFontHeight((short) 300);
- cellStyle.setFont(font);
-
- cell.setCellStyle(cellStyle);
- }
-
-
- public void createNormalTwoRow(String[] params, int colSum) {
- HSSFRow row1 = sheet.createRow(1);
- row1.setHeight((short) 300);
-
- HSSFCell cell2 = row1.createCell(0);
-
- cell2.setCellType(HSSFCell.ENCODING_UTF_16);
- cell2.setCellValue(new HSSFRichTextString("統計時間:" + params[0] + "至"
- + params[1]));
-
-
- sheet.addMergedRegion(new Region(1, (short) 0, 1, (short) colSum));
-
- HSSFCellStyle cellStyle = wb.createCellStyle();
- cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
- cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
- cellStyle.setWrapText(true);
-
-
- HSSFFont font = wb.createFont();
- font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
- font.setFontName("宋體");
- font.setFontHeight((short) 250);
- cellStyle.setFont(font);
-
- cell2.setCellStyle(cellStyle);
-
- }
-
-
- public void createColumHeader(String[] columHeader) {
-
-
- HSSFRow row2 = sheet.createRow(2);
-
-
- row2.setHeight((short) 600);
-
- HSSFCellStyle cellStyle = wb.createCellStyle();
- cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
- cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
- cellStyle.setWrapText(true);
-
-
- HSSFFont font = wb.createFont();
- font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
- font.setFontName("宋體");
- font.setFontHeight((short) 250);
- cellStyle.setFont(font);
-
-
-
-
- cellStyle.setFillForegroundColor(HSSFColor.GREY_25_PERCENT.index);
- cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
-
- HSSFCell cell3 = null;
-
- for (int i = 0; i < columHeader.length; i++) {
- cell3 = row2.createCell(i);
- cell3.setCellType(HSSFCell.ENCODING_UTF_16);
- cell3.setCellStyle(cellStyle);
- cell3.setCellValue(new HSSFRichTextString(columHeader[i]));
- }
-
- }
-
-
- public void cteateCell(HSSFWorkbook wb, HSSFRow row, int col, short align,
- String val) {
- HSSFCell cell = row.createCell(col);
- cell.setCellType(HSSFCell.ENCODING_UTF_16);
- cell.setCellValue(new HSSFRichTextString(val));
- HSSFCellStyle cellstyle = wb.createCellStyle();
- cellstyle.setAlignment(align);
- cell.setCellStyle(cellstyle);
- }
-
-
- public void createLastSumRow(int colSum, String[] cellValue) {
-
- HSSFCellStyle cellStyle = wb.createCellStyle();
- cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
- cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
- cellStyle.setWrapText(true);
-
-
- HSSFFont font = wb.createFont();
- font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
- font.setFontName("宋體");
- font.setFontHeight((short) 250);
- cellStyle.setFont(font);
-
- HSSFRow lastRow = sheet.createRow((short) (sheet.getLastRowNum() + 1));
- HSSFCell sumCell = lastRow.createCell(0);
-
- sumCell.setCellValue(new HSSFRichTextString("合計"));
- sumCell.setCellStyle(cellStyle);
- sheet.addMergedRegion(new Region(sheet.getLastRowNum(), (short) 0,
- sheet.getLastRowNum(), (short) colSum));
-
- for (int i = 2; i < (cellValue.length + 2); i++) {
- sumCell = lastRow.createCell(i);
- sumCell.setCellStyle(cellStyle);
- sumCell.setCellValue(new HSSFRichTextString(cellValue[i - 2]));
-
- }
-
- }
-
-
- public void outputExcel(String fileName) {
- FileOutputStream fos = null;
- try {
- fos = new FileOutputStream(new File(fileName));
- wb.write(fos);
- fos.close();
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
報表生成類:ComplexExportExcelClient.java
- package com.bzu.search.action;
-
- import java.util.ArrayList;
- import java.util.List;
-
- import org.apache.poi.hssf.usermodel.HSSFCell;
- import org.apache.poi.hssf.usermodel.HSSFCellStyle;
- import org.apache.poi.hssf.usermodel.HSSFFont;
- import org.apache.poi.hssf.usermodel.HSSFRichTextString;
- import org.apache.poi.hssf.usermodel.HSSFRow;
- import org.apache.poi.hssf.usermodel.HSSFSheet;
- import org.apache.poi.hssf.usermodel.HSSFWorkbook;
- import org.apache.poi.hssf.util.Region;
-
-
- public class ComplexExportExcelClient {
-
- private static HSSFWorkbook wb = new HSSFWorkbook();
-
- private static HSSFSheet sheet = wb.createSheet();
-
- @SuppressWarnings({ "unchecked", "deprecation" })
- public static void main(String[] args) {
-
- ExportExcel exportExcel = new ExportExcel(wb, sheet);
-
-
- List fialList = new ArrayList();
-
- fialList.add("申請人未提供任何聯繫方式");
- fialList.add("無工做單位信息且未提供收入來源信息");
- fialList.add("有工做單位但未提供單位地址或電話");
- fialList.add("家庭地址缺失");
- fialList.add("客戶身份證實資料缺");
- fialList.add("簽名缺失或簽名不符合要求");
- fialList.add("其它");
-
- List errorList = new ArrayList();
-
- errorList.add("客戶主動取消");
- errorList.add("我的徵信不良");
- errorList.add("欺詐申請");
- errorList.add("申請人基本條件不符");
- errorList.add("申請材料不合規");
- errorList.add("沒法正常完成徵信");
- errorList.add("重複申請");
- errorList.add("其餘");
-
-
- int number = 2 + fialList.size() * 2 + errorList.size() * 2;
-
-
- for (int i = 0; i < number; i++) {
- sheet.setColumnWidth(i, 3000);
- }
-
-
- HSSFCellStyle cellStyle = wb.createCellStyle();
-
-
- cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
-
-
- cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
-
-
- cellStyle.setWrapText(true);
-
-
- HSSFFont font = wb.createFont();
- font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
- font.setFontName("宋體");
- font.setFontHeight((short) 200);
- cellStyle.setFont(font);
-
-
- exportExcel.createNormalHead("南京地區申請資料拒件分析統計", number);
-
-
- String[] params = new String[] { " 年 月 日", " 年 月 日" };
- exportExcel.createNormalTwoRow(params, number);
-
-
- HSSFRow row2 = sheet.createRow(2);
-
- HSSFCell cell0 = row2.createCell(0);
- cell0.setCellStyle(cellStyle);
- cell0.setCellValue(new HSSFRichTextString("機構代碼"));
-
- HSSFCell cell1 = row2.createCell(1);
- cell1.setCellStyle(cellStyle);
- cell1.setCellValue(new HSSFRichTextString("支行名稱"));
-
- HSSFCell cell2 = row2.createCell(2);
- cell2.setCellStyle(cellStyle);
- cell2.setCellValue(new HSSFRichTextString("無效件"));
-
- HSSFCell cell3 = row2.createCell(2 * fialList.size() + 2);
- cell3.setCellStyle(cellStyle);
- cell3.setCellValue(new HSSFRichTextString("拒絕件"));
-
- HSSFRow row3 = sheet.createRow(3);
-
-
- row3.setHeight((short) 800);
-
- HSSFCell row3Cell = null;
- int m = 0;
- int n = 0;
-
-
- for (int i = 2; i < number; i = i + 2) {
-
- if (i < 2 * fialList.size() + 2) {
- row3Cell = row3.createCell(i);
- row3Cell.setCellStyle(cellStyle);
- row3Cell.setCellValue(new HSSFRichTextString(fialList.get(m)
- .toString()));
- m++;
- } else {
- row3Cell = row3.createCell(i);
- row3Cell.setCellStyle(cellStyle);
- row3Cell.setCellValue(new HSSFRichTextString(errorList.get(n)
- .toString()));
- n++;
- }
-
- }
-
-
- row3Cell = row3.createCell(number);
- row3Cell.setCellStyle(cellStyle);
- row3Cell.setCellValue(new HSSFRichTextString("合計"));
-
-
- HSSFRow row4 = sheet.createRow(4);
-
-
- sheet.addMergedRegion(new Region(2, (short) 0, 4, (short) 0));
-
-
- sheet.addMergedRegion(new Region(2, (short) 1, 4, (short) 1));
-
-
- int aa = 2 * fialList.size() + 1;
- sheet.addMergedRegion(new Region(2, (short) 2, 2, (short) aa));
-
- int start = aa + 1;
-
- sheet.addMergedRegion(new Region(2, (short) start, 2,
- (short) (number - 1)));
-
-
- for (int i = 2; i < number; i = i + 2) {
- sheet.addMergedRegion(new Region(3, (short) i, 3, (short) (i + 1)));
-
- }
-
-
- for (int i = 2; i < number; i++) {
- if (i < 2 * fialList.size() + 2) {
-
- if (i % 2 == 0) {
- HSSFCell cell = row4.createCell(i);
- cell.setCellStyle(cellStyle);
- cell.setCellValue(new HSSFRichTextString("無效量"));
- } else {
- HSSFCell cell = row4.createCell(i);
- cell.setCellStyle(cellStyle);
- cell.setCellValue(new HSSFRichTextString("佔比"));
- }
- } else {
- if (i % 2 == 0) {
- HSSFCell cell = row4.createCell(i);
- cell.setCellStyle(cellStyle);
- cell.setCellValue(new HSSFRichTextString("拒絕量"));
- } else {
- HSSFCell cell = row4.createCell(i);
- cell.setCellStyle(cellStyle);
- cell.setCellValue(new HSSFRichTextString("佔比"));
- }
- }
-
- }
-
-
- for (int i = 5; i < number; i++) {
- HSSFRow row = sheet.createRow((short) i);
- for (int j = 0; j <= number; j++) {
- exportExcel
- .cteateCell(wb, row, (short) j,
- HSSFCellStyle.ALIGN_CENTER_SELECTION, String
- .valueOf(j));
- }
-
- }
-
-
- String[] cellValue = new String[number - 1];
- for (int i = 0; i < number - 1; i++) {
- cellValue[i] = String.valueOf(i);
-
- }
- exportExcel.createLastSumRow(1, cellValue);
-
- exportExcel.outputExcel("c:\\拒絕件統計.xls");
-
- }
- }
運行上述兩段代碼你就會在c盤的根目錄下看到一個拒絕件統計.xls文件