做的Struts 項(xiàng)目中有這樣的功能,用戶可以將數(shù)據(jù)導(dǎo)出到Excel 報(bào)表,如圖1 所示。

圖1 報(bào)表樣式圖
1 設(shè)計(jì)思路
Java 對(duì)于Excel 的操作一般借助于POI 類庫(kù),由于該報(bào)表的表頭比較復(fù)雜,直接用POI 控制報(bào)表的生成比較困難,這時(shí)可以先制作Excel 報(bào)表模板
,而后再通過(guò)Java 調(diào)用POI 函數(shù)將用戶數(shù)據(jù)寫(xiě)入到Excel 報(bào)表模板,最后導(dǎo)出到新的目標(biāo)文件即可。
2 設(shè)計(jì)步驟
2.1 Excel 報(bào)表模板
根據(jù)需要設(shè)計(jì)出Excel 報(bào)表,并保存為report.xls。該報(bào)表有復(fù)雜的表頭,報(bào)表第4 行為合計(jì)行,用于對(duì)所有數(shù)值型列的各行數(shù)據(jù)進(jìn)行匯總,如圖1 所示。
2.2 Struts 的動(dòng)作執(zhí)行函數(shù)ExcelExportAction
該Action 函數(shù)在用戶需要執(zhí)行報(bào)表導(dǎo)出時(shí)通過(guò)Struts 頁(yè)面調(diào)用或用戶觸發(fā)執(zhí)行。
package com.tj.struts.action;
import java.io.FileOutputStream;
import javax.servlet.http.*;
import org.apache.struts.action.*;
import databaseUtil.ExcelPoi;
public class ExcelExportAction extends Action {
public ActionForward execute ( ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response)
throws Exception {
//執(zhí)行SQL 獲得輸出到報(bào)表的數(shù)據(jù)
String sql=" select * fron project" ;
//準(zhǔn)備輸出的報(bào)表路徑,及文件名
String outputfile =" c:\\output.xls" ;
//制作好的報(bào)表模板存放路徑
String templatefile=" c:\\report.xls" ;
//模板的列數(shù)為39
int column=39;
try {
//實(shí)例化具體的業(yè)務(wù)處理類ExcelPoi
ExcelPoi pd = new ExcelPoi (colnum) ;
//getExcelSheet 以sql 為參數(shù)執(zhí)行查詢,將查詢數(shù)據(jù)寫(xiě)入模版文件“templatefile”的當(dāng)前工作簿的選定工作表pd.getExcelSheeet (sql,templatefile) ;
// 新建以文件outputfile 為目標(biāo)的輸出文件流
FileOutputStream fos = new FileOutputStream (outputfile) ;
//將工作簿寫(xiě)入輸出文件流,得到輸出報(bào)表文件
pd.exportExcel (fos) ;
} catch (Exception e) {
e.printStackTrace () ;
} finally {
try {fos.close () ;
} catch (Exception e) {
e.printStackTrace () ;
}}
return mapping.findForward (" success") ;
}}
2.3 設(shè)計(jì)業(yè)務(wù)處理類ExcelPoi
package databaseUtil;
import java.io.*;
import java.sql.*;
import java.util.*;
import org.apache.poi.hssf.usermodel.*;
public class ExcelPoi {
private int columNumber = 0;
private int cellNumber=0;
private HSSFWorkbook workbook = null;
private HSSFSheet worksheet=null;
public ExcelPoi (int columNumber)
{this.columNumber=columNumber;}
<! —sql:傳入?yún)?shù),實(shí)現(xiàn)輸出數(shù)據(jù)查詢,templatefile:傳入?yún)?shù),Excel 模板的存放路徑-->
public void getExcelSheeet ( String sql,String templatefile)
throws SQLException
{try {
//新建以文件templatefile 為源文件的輸文件流,而后從中取得模板文件templatefile 的當(dāng)前工作簿
workbook = new HSSFWorkbook ( new FileInputStream
(templatefile)) ;
} catch (FileNotFoundException e) {
e.printStackTrace () ;
} catch (IOException e) {
e.printStackTrace () ;}
//取得當(dāng)前工作簿中的“天津大學(xué)工程項(xiàng)目庫(kù)管理系統(tǒng)”工作表
worksheet=workbook.getSheet (" 天津大學(xué)工程項(xiàng)目庫(kù)管
理系統(tǒng)") ;
//載入數(shù)據(jù)庫(kù)驅(qū)動(dòng),獲得數(shù)據(jù)庫(kù)的連接,數(shù)據(jù)庫(kù)名為tj,用戶名swm, 密碼adminClass.forName ( " com.microsoft.jdbc.sqlserver.SQLServerDriver") .newInstance () ;
dbConn = DriverManager.getConnection (" jdbc:microsoft:
sqlserver://localhost:1433;DatabaseName =tj" , " swm" , "
admin") ;
statement = dbConn.createStatement (ResultSet.
TYPE_SCROLL_INSENSITIVE,ResultSet.
CONCUR_UPDATABLE) ;
//以SQL 為參數(shù),執(zhí)行數(shù)據(jù)查詢
ResultSet rs = statement.executeQuery (sql) ;
int rowIndex = 4;
while (rs.next ())
{ List list = new ArrayList () ;
//將查詢得到的每行數(shù)據(jù)放入list 中
for (int i = 1;i <= columNumber;i++)
{list.add (rs.getString (i)) ; }
//調(diào)用createTableRow 把list 中數(shù)據(jù)寫(xiě)入worksheet 的第rowIndex 行
createTableRow (worksheet,list, (short) rowIndex) ;
rowIndex++;}}
<! —-用list 數(shù)據(jù)創(chuàng)建當(dāng)前工作表的第rowIndex 行,并將該行非String 數(shù)據(jù)累加到合計(jì)行行-->
public void createTableRow ( HSSFSheet worksheet1,List list,short rowIndex)
{//getRow (3) 取得工作表的第四行,即合計(jì)行(行數(shù)從0 開(kāi)始)
HSSFRow sumrow = worksheet1.getRow (3) ;
for (short i = 0;i < list.size () ;i++)
{ HSSFCell cell = sumrow.getCell (i) ;
//將list 中1、2、3、4、6、7、8 列之外(這幾列為String 類型不進(jìn)行合計(jì)),其它列數(shù)據(jù)累加到合計(jì)行的對(duì)應(yīng)列單元中
if (! (i==0||i==1||i==2||i==3||i==5||i==6||i==7)) {
cell.setCellValue ( list.getNumericCellValue ( ) + ( double)
Integer.parseInt ((String) list.get (i))) ;
}}
//創(chuàng)建當(dāng)前工作表的新行,等待放入數(shù)據(jù)
HSSFRow row = worksheet1.createRow ( ( short)rowIndex) ;
//在新創(chuàng)建行中創(chuàng)建各列單元格,并將list 中對(duì)應(yīng)數(shù)據(jù)寫(xiě)入
for (short i = 0;i < list.size () ;i++)
{HSSFCell cell = row.createCell ((short) i) ;
cell.setEncoding (HSSFCell.ENCODING_UTF_16) ;
cell.setCellValue ((String) list.get (i)) ; }
<! --該函數(shù)將存儲(chǔ)了數(shù)據(jù)的模板文件導(dǎo)出到輸出文件流,創(chuàng)建一個(gè)新的報(bào)表-->
public void exportExcel ( OutputStream os) throws
IOException
{worksheet.setGridsPrinted (true) ;
workbook.write (os) ;
}}
3 結(jié)語(yǔ)
對(duì)于一些要求非常苛刻的報(bào)表輸出可以借助于一些第三方插件,比如水晶報(bào)表等。在實(shí)際中可以隨心所欲地構(gòu)建報(bào)表模板,而后通過(guò)程序控制將需要導(dǎo)出的數(shù)據(jù)導(dǎo)出到報(bào)表中,關(guān)鍵在于如何精確地控制數(shù)據(jù)導(dǎo)出的位置,保證數(shù)據(jù)在報(bào)表中的準(zhǔn)確的位置,這是需要格外注意的。