一、需求介紹
當前B/S模式已成爲應用開發的主流,而在開發企業辦公系統的過程中,常常有客戶這樣子要求:把系統數據庫中的數據導出到Excel,用戶查看報表時直接用Excel打開。或者是:用戶已經習慣用Excel打印,也就要求必須把數據導入到Excel文件。這樣在我們實際的開發中,很多時候需要實現導出Excel的應用。目前從網上找到的比較常用的實現Java導出Excel的技術有三種 POI、JXL、PageOffice,(CSV技術就不討論了,它只是生成以逗號間隔的文本文件)下面我就分析一下這三種方案。
二、方案分析
POI 是apache的子項目,目標是處理ole2對象,它提供了一組操縱Windows文檔的Java API。用於操作Excel格式文件的是HSSF接口,處理MS Excel對象,它不象我們用CSV生成的僅僅是沒有格式的可以由Excel轉換的文本文件,而是模擬操作Excel對象,你可以控制一些屬性如sheet,cell等等。HSSF 是Horrible SpreadSheet Format的縮寫,翻譯過來就是“討厭的電子表格格式”。 HSSF的名字很不嚴肅,但通過HSSF,你可以用Java代碼來修改Excel文件內容。HSSF 爲讀取操作提供了兩類API:usermodel和eventusermodel,即“用戶模型”和“事件-用戶模型”,接口比較抽象,不好理解。
JXL屬於開放源碼項目,通過JXL同樣可以修改Excel文件中的數據內容。JXL 由於其小巧 易用的特點, 逐漸已經取代了 POI-excel的地位, 成爲更多的java開發人員生成excel文件的首選。JXL的特點:支持Excel 2000標準格式、支持字體、數字、日期格式化操作 、支持對單元格加陰影和加色彩、比POI更小更省內存。這些功能能夠大致滿足用戶的一般需求。
PageOffice封裝Office VBA接口爲Java調用的Excel簡化對象模型,指的是完全用Java實現的一組Excel存取對象,便於服務器端Java代碼直接調用。這套對象模型不是簡單地模仿Excel的VBA對象接口,而是從開發者常用的編程代碼角度,結合數據庫數據訪問的特點抽象出的一套實用、調用簡單的對象庫,把原本複雜的Excel讀寫操作簡單化了,同時解決了Excel文件的在線打開問題,用戶可以直接在網頁中查看和編輯Excel,無需繁瑣的上傳下載操作。
篇幅有限,也爲了節約大家的閱讀時間,下面只討論相對比較好的PageOffice方案。
用PageOffice可以用簡單調用方式生成Excel文件,也可以用高級調用方式生成。簡單調用方式是指在Excel模板文件中先設置好所有的表格樣式,開發的時候只是編寫代碼給Excel模板填充數據即可,此方式編寫的代碼量最少,效果並不差,這也是PageOffice推薦的調用方式;高級調用方式可以實現在一個空白的Excel文件中生成很漂亮的表格(甚至給單元格賦值公式),但是編程的工作量就大多了。
三、簡單調用方式
1. 製作excel模板:
2. 編寫代碼:
PageOfficeCtrl poCtrl1 = new PageOfficeCtrl(request);
poCtrl1.setServerPage(request.getContextPath()+"/poserver.zz");
//定義Workbook對象
Workbook workBook = new Workbook();
//定義Sheet對象,"Sheet1"是打開的Excel表單的名稱
Sheet sheet = workBook.openSheet("Sheet1");
//定義Table對象
Table table = sheet.openTable("B4:F13");
for(int i=0; i < 50; i++)
{
table.getDataFields().get(0).setValue("產品 " + i);
table.getDataFields().get(1).setValue("100");
table.getDataFields().get(2).setValue(String.valueOf(100+i));
table.nextRow();
}
table.close();
poCtrl1.setWriter(workBook);
//打開Excel文件
poCtrl1.webOpen("doc/test.xls", OpenModeType.xlsNormalEdit, "張三");
2. 生成Excel的效果:
四、高級調用方式
1. 編碼生成Excel
Workbook wb = new Workbook();
// 設置背景
Table backGroundTable = wb.openSheet("Sheet1").openTable("A1:P200");
backGroundTable.getBorder().setLineColor(Color.white);
// 設置標題
wb.openSheet("Sheet1").openTable("A1:H2").merge();
wb.openSheet("Sheet1").openTable("A1:H2").setRowHeight(30);
Cell A1 = wb.openSheet("Sheet1").openCell("A1");
A1.setHorizontalAlignment(XlHAlign.xlHAlignCenter);
A1.setVerticalAlignment(XlVAlign.xlVAlignCenter);
A1.setForeColor(new Color(0, 128, 128));
A1.setValue("出差開支預算");
//設置字體
wb.openSheet("Sheet1").openTable("A1:A1").getFont().setBold(true);
wb.openSheet("Sheet1").openTable("A1:A1").getFont().setSize(25);
// 畫表頭
Border C4Border = wb.openSheet("Sheet1").openTable("C4:C4").getBorder();
C4Border.setWeight(XlBorderWeight.xlThick);
C4Border.setLineColor(Color.yellow);
Table titleTable = wb.openSheet("Sheet1").openTable("B4:H5");
titleTable.getBorder().setBorderType(XlBorderType.xlAllEdges);
titleTable.getBorder().setWeight(XlBorderWeight.xlThick);
titleTable.getBorder().setLineColor(new Color(0, 128, 128));
// 畫表體
Table bodyTable = wb.openSheet("Sheet1").openTable("B6:H15");
bodyTable.getBorder().setLineColor(Color.gray);
bodyTable.getBorder().setWeight(XlBorderWeight.xlHairline);
Border B7Border = wb.openSheet("Sheet1").openTable("B7:B7").getBorder();
B7Border.setLineColor(Color.white);
Border B9Border = wb.openSheet("Sheet1").openTable("B9:B9").getBorder();
B9Border.setBorderType(XlBorderType.xlBottomEdge);
B9Border.setLineColor(Color.white);
Border C6C15BorderLeft = wb.openSheet("Sheet1").openTable("C6:C15").getBorder();
C6C15BorderLeft.setLineColor(Color.white);
C6C15BorderLeft.setBorderType(XlBorderType.xlLeftEdge);
Border C6C15BorderRight = wb.openSheet("Sheet1").openTable("C6:C15").getBorder();
C6C15BorderRight.setLineColor(Color.yellow);
C6C15BorderRight.setLineStyle(XlBorderLineStyle.xlDot);
C6C15BorderRight.setBorderType(XlBorderType.xlRightEdge);
Border E6E15Border = wb.openSheet("Sheet1").openTable("E6:E15").getBorder();
E6E15Border.setLineStyle(XlBorderLineStyle.xlDot);
E6E15Border.setBorderType(XlBorderType.xlAllEdges);
E6E15Border.setLineColor(Color.yellow);
Border G6G15BorderRight = wb.openSheet("Sheet1").openTable("G6:G15").getBorder();
G6G15BorderRight.setBorderType(XlBorderType.xlRightEdge);
G6G15BorderRight.setLineColor(Color.white);
Border G6G15BorderLeft = wb.openSheet("Sheet1").openTable("G6:G15").getBorder();
G6G15BorderLeft.setLineStyle(XlBorderLineStyle.xlDot);
G6G15BorderLeft.setBorderType(XlBorderType.xlLeftEdge);
G6G15BorderLeft.setLineColor(Color.yellow);
Table bodyTable2 = wb.openSheet("Sheet1").openTable("B6:H15");
bodyTable2.getBorder().setWeight(XlBorderWeight.xlThick);
bodyTable2.getBorder().setLineColor(new Color(0, 128, 128));
bodyTable2.getBorder().setBorderType(XlBorderType.xlAllEdges);
// 畫表尾
Border H16H17Border = wb.openSheet("Sheet1").openTable("H16:H17").getBorder();
H16H17Border.setLineColor(new Color(204, 255, 204));
Border E16G17Border = wb.openSheet("Sheet1").openTable("E16:G17").getBorder();
E16G17Border.setLineColor(new Color(0, 128, 128));
Table footTable = wb.openSheet("Sheet1").openTable("B16:H17");
footTable.getBorder().setWeight(XlBorderWeight.xlThick);
footTable.getBorder().setLineColor(new Color(0, 128, 128));
footTable.getBorder().setBorderType(XlBorderType.xlAllEdges);
// 設置行高列寬
wb.openSheet("Sheet1").openTable("A1:A1").setColumnWidth(1);
wb.openSheet("Sheet1").openTable("B1:B1").setColumnWidth(20);
wb.openSheet("Sheet1").openTable("C1:C1").setColumnWidth(15);
wb.openSheet("Sheet1").openTable("D1:D1").setColumnWidth(10);
wb.openSheet("Sheet1").openTable("E1:E1").setColumnWidth(8);
wb.openSheet("Sheet1").openTable("F1:F1").setColumnWidth(3);
wb.openSheet("Sheet1").openTable("G1:G1").setColumnWidth(12);
wb.openSheet("Sheet1").openTable("H1:H1").setColumnWidth(20);
wb.openSheet("Sheet1").openTable("A16:A16").setRowHeight(20);
wb.openSheet("Sheet1").openTable("A17:A17").setRowHeight(20);
// 設置表格中字體大小爲10
for (int i = 0; i < 12; i++) {//excel表格行號
for (int j = 0; j < 7; j++) {//excel表格列號
wb.openSheet("Sheet1").openCellRC(4 + i, 2 + j).getFont().setSize(10);
}
}
// 填充單元格背景顏色
for (int i = 0; i < 10; i++) {
wb.openSheet("Sheet1").openCell("H" + (6 + i)).setBackColor(new Color(255, 255, 153));
}
wb.openSheet("Sheet1").openCell("E16").setBackColor(new Color(0, 128, 128));
wb.openSheet("Sheet1").openCell("F16").setBackColor(new Color(0, 128, 128));
wb.openSheet("Sheet1").openCell("G16").setBackColor(new Color(0, 128, 128));
wb.openSheet("Sheet1").openCell("E17").setBackColor(new Color(0, 128, 128));
wb.openSheet("Sheet1").openCell("F17").setBackColor(new Color(0, 128, 128));
wb.openSheet("Sheet1").openCell("G17").setBackColor(new Color(0, 128, 128));
wb.openSheet("Sheet1").openCell("H16").setBackColor(new Color(204, 255, 204));
wb.openSheet("Sheet1").openCell("H17").setBackColor(new Color(204, 255, 204));
//填充單元格文本和公式
Cell B4 = wb.openSheet("Sheet1").openCell("B4");
B4.getFont().setBold(true);
B4.setValue("出差開支預算");
Cell H5 = wb.openSheet("Sheet1").openCell("H5");
H5.getFont().setBold(true);
H5.setValue("總計");
H5.setHorizontalAlignment(XlHAlign.xlHAlignCenter);
Cell B6 = wb.openSheet("Sheet1").openCell("B6");
B6.getFont().setBold(true);
B6.setValue("飛機票價");
Cell B9 = wb.openSheet("Sheet1").openCell("B9");
B9.getFont().setBold(true);
B9.setValue("酒店");
Cell B11 = wb.openSheet("Sheet1").openCell("B11");
B11.getFont().setBold(true);
B11.setValue("餐飲");
Cell B12 = wb.openSheet("Sheet1").openCell("B12");
B12.getFont().setBold(true);
B12.setValue("交通費用");
Cell B13 = wb.openSheet("Sheet1").openCell("B13");
B13.getFont().setBold(true);
B13.setValue("休閒娛樂");
Cell B14 = wb.openSheet("Sheet1").openCell("B14");
B14.getFont().setBold(true);
B14.setValue("禮品");
Cell B15 = wb.openSheet("Sheet1").openCell("B15");
B15.getFont().setBold(true);
B15.getFont().setSize(10);
B15.setValue("其他費用");
wb.openSheet("Sheet1").openCell("C6").setValue("機票單價(往)");
wb.openSheet("Sheet1").openCell("C7").setValue("機票單價(返)");
wb.openSheet("Sheet1").openCell("C8").setValue("其他");
wb.openSheet("Sheet1").openCell("C9").setValue("每晚費用");
wb.openSheet("Sheet1").openCell("C10").setValue("其他");
wb.openSheet("Sheet1").openCell("C11").setValue("每天費用");
wb.openSheet("Sheet1").openCell("C12").setValue("每天費用");
wb.openSheet("Sheet1").openCell("C13").setValue("總計");
wb.openSheet("Sheet1").openCell("C14").setValue("總計");
wb.openSheet("Sheet1").openCell("C15").setValue("總計");
wb.openSheet("Sheet1").openCell("G6").setValue(" 張");
wb.openSheet("Sheet1").openCell("G7").setValue(" 張");
wb.openSheet("Sheet1").openCell("G9").setValue(" 晚");
wb.openSheet("Sheet1").openCell("G10").setValue(" 晚");
wb.openSheet("Sheet1").openCell("G11").setValue(" 天");
wb.openSheet("Sheet1").openCell("G12").setValue(" 天");
wb.openSheet("Sheet1").openCell("H6").setFormula("=D6*F6");
wb.openSheet("Sheet1").openCell("H7").setFormula("=D7*F7");
wb.openSheet("Sheet1").openCell("H8").setFormula("=D8*F8");
wb.openSheet("Sheet1").openCell("H9").setFormula("=D9*F9");
wb.openSheet("Sheet1").openCell("H10").setFormula("=D10*F10");
wb.openSheet("Sheet1").openCell("H11").setFormula("=D11*F11");
wb.openSheet("Sheet1").openCell("H12").setFormula("=D12*F12");
wb.openSheet("Sheet1").openCell("H13").setFormula("=D13*F13");
wb.openSheet("Sheet1").openCell("H14").setFormula("=D14*F14");
wb.openSheet("Sheet1").openCell("H15").setFormula("=D15*F15");
for (int i = 0; i < 10; i++) {
//設置數據以貨幣形式顯示
wb.openSheet("Sheet1").openCell("D" + (6 + i)).setNumberFormatLocal("¥#,##0.00;¥-#,##0.00");
wb.openSheet("Sheet1").openCell("H" + (6 + i)).setNumberFormatLocal("¥#,##0.00;¥-#,##0.00");
}
Cell E16 = wb.openSheet("Sheet1").openCell("E16");
E16.getFont().setBold(true);
E16.getFont().setSize(11);
E16.setForeColor(Color.white);
E16.setValue("出差開支總費用");
E16.setVerticalAlignment(XlVAlign.xlVAlignCenter);
Cell E17 = wb.openSheet("Sheet1").openCell("E17");
E17.getFont().setBold(true);
E17.getFont().setSize(11);
E17.setForeColor(Color.white);
E17.setFormula("=IF(C4>H16,\"低於預算\",\"超出預算\")");
E17.setVerticalAlignment(XlVAlign.xlVAlignCenter);
Cell H16 = wb.openSheet("Sheet1").openCell("H16");
H16.setVerticalAlignment(XlVAlign.xlVAlignCenter);
H16.setNumberFormatLocal("¥#,##0.00;¥-#,##0.00");
H16.getFont().setName("Arial");
H16.getFont().setSize(11);
H16.getFont().setBold(true);
H16.setFormula("=SUM(H6:H15)");
Cell H17 = wb.openSheet("Sheet1").openCell("H17");
H17.setVerticalAlignment(XlVAlign.xlVAlignCenter);
H17.setNumberFormatLocal("¥#,##0.00;¥-#,##0.00");
H17.getFont().setName("Arial");
H17.getFont().setSize(11);
H17.getFont().setBold(true);
H17.setFormula("=(C4-H16)");
// 填充數據
Cell C4 = wb.openSheet("Sheet1").openCell("C4");
C4.setNumberFormatLocal("¥#,##0.00;¥-#,##0.00");
C4.setValue("2500");
Cell D6 = wb.openSheet("Sheet1").openCell("D6");
D6.setNumberFormatLocal("¥#,##0.00;¥-#,##0.00");
D6.setValue("1200");
wb.openSheet("Sheet1").openCell("F6").getFont().setSize(10);
wb.openSheet("Sheet1").openCell("F6").setValue("1");
Cell D7 = wb.openSheet("Sheet1").openCell("D7");
D7.setNumberFormatLocal("¥#,##0.00;¥-#,##0.00");
D7.setValue("875");
wb.openSheet("Sheet1").openCell("F7").setValue("1");
//打開文件
PageOfficeCtrl poCtrl1 = new PageOfficeCtrl(request);
poCtrl1.setWriter(wb);
poCtrl1.setServerPage(request.getContextPath()+"/poserver.zz");
poCtrl1.webOpen("doc/test.xls", OpenModeType.xlsNormalEdit, "");
2. 生成Excel的效果:
五、源碼下載
1. 訪問:http://www.zhuozhengsoft.com/dowm/,下載PageOffice for Java;
2. 解壓PageOffice開發包,拷貝Samples4文件夾到Tomcat的Webapps目錄下,訪問:http://localhost:8080/Samples4/index.html;
3. 查看示例:一、23、對Excel中的一塊區域賦值,並自動增加行;三、8、完全編程實現動態生成Excel文件