http://www.iteye.com/topic/211707
之前用POI做了一段时间的报表,也对POI的一些方法重写了,用起来还算方便。也没什么特别的,主要就是对HSSFWorkbook,HSSFSheet,HSSFRow, HSSFCell的操作了,掌握了对这四个东西的控制,你想怎么写就怎么写。
1,首先写一个abstract class用来overwrite HSSF。
package com.eagle.excel; import java.util.ArrayList; import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFCellStyle; /** * @author eyas * @version 1.0 * @date Apr 28, 2007 10:41:26 PM */ public abstract class ExportInfoToExcel { /** * create report header */ protected abstract void createHead(); /** *create report body *columns:table header *itemList:row data */ protected abstract void createBody(String[] columns, ArrayList itemlist); /** *create report footer, ex:someone total information. */ protected abstract void createFoot(); /** * write data to a Excel cell.this method is overloadabled. */ public void createCell(HSSFCellStyle cellStyle, HSSFCell cell, String value) { cell.setEncoding(HSSFCell.ENCODING_UTF_16); cell.setCellStyle(cellStyle); cell.setCellValue(value); } }
2,再写一个interface 用来给Excel 传递数据,并生成Excel file,
package com.eagle.excel; import java.io.File; import java.util.ArrayList; import java.util.Calendar; /** * @author eyas * @version 1.0 * @date Apr 28, 2007 10:41:26 PM */ public interface ExportInfoInterface { public void setDate(Calendar startDate); // report print date public void setColumns(String[] columns); // report columns public void setDataList(ArrayList itemList); //data public void produceExportFile(File exportfile) throws Exception;//create excel file public void setOtherArg(Object arg1);// other arguments }
3,然后就可以写一个类,用来接收数据,extends ,implements 上面的abstract class and interface.
package com.eagle.excel; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.util.ArrayList; import java.util.Calendar; 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; /** * @author eyas * @version 1.0 * @date Apr 28, 2007 10:43:26 PM */ public class CreateExcelFile extends ExportInfoToExcel implements ExportInfoInterface { private HSSFWorkbook workbook; private HSSFSheet sheet; private ArrayList dataList; private Calendar date; private String[] columns; private int lineX = 0; public CreateExcelFile () { //initialized someone arguments } @Override protected void createBody() { HSSFRow bodyTile = sheet.createRow(lineX++); for (int i = 0; i < columns.length; i++) { createCell(bodyTile.createCell((short) i), columns[i]); } HSSFRow bodyRow; for (int i = 0; i < dataList.size(); i++) { bodyRow = sheet.createRow(lineX++); createCell(null,bodyRow.createCell((short) 0),(i+1)); createCell(null,bodyRow.createCell((short) 1),"eyas"); createCell(null,bodyRow.createCell((short) 2),"software"); ... } } @Override protected void createFoot() { HSSFRow totalRow = sheet.createRow(lineX++); createCell(null,bodyRow.createCell((short) 0),"total data"); } @Override protected void createHead() { lineX = 0; sheet.addMergedRegion(new Region(0, (short) 0, 0, (short) 12)); HSSFRow headRow0 = sheet.createRow(lineX++); createCell(null,headRow0.createCell((short) 0), "My Excel Report"); } public void produceExportFile(File exportfile) throws Exception { try { workbook = new HSSFWorkbook(); sheet = workbook.createSheet("First Page"); createHead(); createBody(); FileOutputStream fw = new FileOutputStream(exportfile); workbook.write(fw); fw.flush(); fw.close(); workbook = null; sheet = null; fw = null; System.out.println("Export success!"); } catch (IOException ioe) { ioe.printStackTrace(); }catch (Exception e) { e.printStackTrace(); } } public void setColumns(String[] columns) { this.columns = columns; } public void setDataList(ArrayList dataList) { this.dataList = dataList; } public void setOtherArg(Object arg1) { } public void setDate(Calendar date) { this.date = date; } }
仅对产生Excel报表,使用POI几个步骤的总结,不能保证代码完全 runable,如有改进建议,敬请发表。