使用POI生成Excel报表 (二)

2014-11-24 10:18:58 · 作者: · 浏览: 1
w.getHeightInPoints(); } } } } /** * 创建行 */ public void createNewRow(){ //下移行的条件有2个:当前行非初始行,且当前行没有超过最后一行 if(this.currRowIndex!=this.initRowIndex && this.lastRowIndex>this.currRowIndex){ //将指定的几行进行下移一行 sheet.shiftRows(this.currRowIndex, this.lastRowIndex, 1, true, true); //既然下移了那么最后一行下标就也要增大了 this.lastRowIndex++; } //在指定的行上创建一个空行(如果此行原本有单元格和数据,那么也会被空行覆盖,且创建出来的空行是没有单元格的) this.currRow = sheet.createRow(this.currRowIndex); this.currRow.setHeightInPoints(this.defaultRowHeight); this.currRowIndex++; this.currColIndex = this.initColIndex; } /** * 构造单元格(包括创建单元格和填充数据) */ public void buildCell(String value){ Cell cell = this.currRow.createCell(this.currColIndex); if(this.allCellStyle.containsKey(this.currColIndex)){ cell.setCellStyle(this.allCellStyle.get(this.currColIndex)); }else{ cell.setCellStyle(this.allCellStyle.get(99)); } cell.setCellValue(value); this.currColIndex++; } /** * 插入序号 */ private void insertSerialNo(){ int index = 1; Row row = null; Cell cell = null; for(int i=this.initRowIndex; i constantData = new HashMap(); constantData.put("title", "优秀学生名单"); constantData.put("date", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())); constantData.put("developer", "玄玉博客"); for(Row row : sheet){ for(Cell cell : row){ if(Cell.CELL_TYPE_STRING != cell.getCellType()){ continue; } String str = cell.getStringCellValue().trim(); if(str.startsWith("#")){ if(constantData.containsKey(str.substring(1))){ cell.setCellValue(constantData.get(str.substring(1))); } } } } } /** * 将生成的excel文件写到输出流中 * @see 适用于文件下载 */ public void writeToStream(OutputStream os){ this.insertSerialNo(); this.replaceConstantData(); try { wb.write(os); } catch (IOException e) { throw new RuntimeException("写入流失败", e); } } /** * 将生成的excel文件写到指定的文件中 * @see 适用于硬盘保存 */ public void writeToFile(String filepath){ this.insertSerialNo(); this.replaceConstantData(); FileOutputStream fos = null; try { fos = new FileOutputStream(filepath); wb.write(fos); } catch (FileNotFoundException e) { throw new RuntimeException("写入的文件[" + filepath + "]不存在", e); } catch (IOException e) { throw new RuntimeException("写入数据失败", e); } finally { if(null != fos){ try { fos.close(); } catch (IOException e) { e.printStackTrace(); } } } } } package com.jadyer.report; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import java.text.SimpleDateFormat; import java.util.Date; import java.util.HashMap; import java.util.Map; import org.apache.poi.openxml4j.exceptions.InvalidFormatException; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.CellStyle; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.ss.usermodel.WorkbookFactory; /** * 使用POI生成Excel报表 * @see 它所生成的报表是根据Excel模块文件生成的 * @see 这里要用到poi-3.9-20121203.jar和poi-ooxml-3.9-20121203.jar * @see 另外模板文件<
>下载地址为http://download.csdn.net/detail/jadyer/5736263