xls适合百万级别如下数据表,HSSFWORKBOOK操做.xls的excel文档,支持模板java
工程结构 apache
代码eclipse
utilthis
package com.gzn.poi.util; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.util.ArrayList; import java.util.List; import org.apache.poi.hssf.usermodel.HSSFWorkbook; 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.Workbook; public class WbUtil { /** * 读取模板 * @param path * @param templateName * @return Workbook * @throws IOException */ public static Workbook getTemplate(String path, String templateName) throws IOException { FileInputStream is = null; Workbook wb = null; is = new FileInputStream(path + templateName); wb = new HSSFWorkbook(is); WbUtil.closeAll(is, null, wb); return wb; } /** * 获取行单元格样式列表 * @param nRow 行 * @param cells 单元格数 * @param start 单元格起始下标 * @return List<CellStyle> */ public static List<CellStyle> rowCellsStyle(Row nRow, int cells, int start) { List<CellStyle> cellStyleList = new ArrayList<CellStyle>(cells); if (nRow == null) return cellStyleList; for (int i = 0; i < cells; i++) { Cell cell = nRow.getCell(start++); CellStyle cellStyle = cell.getCellStyle(); cellStyleList.add(cellStyle); } return cellStyleList; } /** * 关闭资源 * @param is * @param os * @param wb */ public static void closeAll(FileInputStream is, FileOutputStream os, Workbook wb) { try { if (is != null) { is.close(); } } catch (IOException e) { e.printStackTrace(); } try { if (os != null) { os.close(); } } catch (IOException e) { e.printStackTrace(); } try { if (wb != null) { wb.close(); } } catch (IOException e) { e.printStackTrace(); } } }
demospa
package com.gzn.poi.demo; import java.io.FileOutputStream; import java.io.IOException; import java.util.List;
import java.util.ArrayList; 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 com.gzn.poi.util.WbUtil; public class Demo1 { public static void main(String[] args) { Workbook wb = null; FileOutputStream os = null;
Demo1 demo1 = new Demo1(); try { wb = demo1.contractProductPrint(); // 输出数据到磁盘/供用户下载 os = new FileOutputStream("F:\\poi-demo\\xxx出货表.xls"); wb.write(os); } catch (Exception e) { e.printStackTrace(); } finally { WbUtil.closeAll(null, os, wb); } } /** * 模板打印 * @return Workbook * @throws IOException */ public Workbook contractProductPrint() throws IOException {
List list = null; Workbook wb = null; wb = WbUtil.getTemplate("F:\\poi-demo\\", "tContractProduct.xls"); // 获取模板
// 获取数据集合
// list = ... this.generateContractProductXLS(wb, 0, list); // sheet表生成数据 WbUtil.closeAll(null, null, wb); return wb; } /** * 生成对应的sheet表,并填充数据 * @param wb 工做簿 * @param sheetIndex sheet表 * @param list 数据源 */ public void generateContractProductXLS(Workbook wb, int sheetIndex, List list) { Row nRow = null; Cell nCell = null; Integer rowNo = 0; Integer cellNo = 1; Sheet sheet = wb.getSheetAt(0); // 获取工做簿的第一个工做表 nRow = sheet.getRow(rowNo++); // 获取行(大标题行) nCell = nRow.getCell(cellNo); // 获取行->单元格 String bigTitleCellContent = nCell.getStringCellValue(); // 获取单元格内容(大标题) CellStyle bigTitleCellStyle = nCell.getCellStyle(); // 获取单元格样式(大标题) // 小标题 rowNo++; // 获取输出内容行 nRow = sheet.getRow(rowNo); short rowHeight = nRow.getHeight(); // 设置行高 List<CellStyle> rowCellsStyle = WbUtil.rowCellsStyle(nRow, 8, 1); int rowCellsStyleIndex = 0; for (int i = 0; i < 10; i++) { // 遍历list cellNo = 1; // 重置单元格游标 nRow = sheet.createRow(rowNo++); // 建立行 nRow.setHeight(rowHeight); // 设置行高 nCell = nRow.createCell(cellNo++); nCell.setCellStyle(rowCellsStyle.get(rowCellsStyleIndex++)); nCell.setCellValue("单元格 1内容"); // 单元格插入对象属性值 nCell = nRow.createCell(cellNo++); nCell.setCellStyle(rowCellsStyle.get(rowCellsStyleIndex++)); nCell.setCellValue("单元格2内容"); nCell = nRow.createCell(cellNo++); nCell.setCellStyle(rowCellsStyle.get(rowCellsStyleIndex++)); nCell.setCellValue("单元格3内容"); nCell = nRow.createCell(cellNo++); nCell.setCellStyle(rowCellsStyle.get(rowCellsStyleIndex++)); nCell.setCellValue("单元格4内容"); nCell = nRow.createCell(cellNo++); nCell.setCellStyle(rowCellsStyle.get(rowCellsStyleIndex++)); nCell.setCellValue("单元格5内容"); nCell = nRow.createCell(cellNo++); nCell.setCellStyle(rowCellsStyle.get(rowCellsStyleIndex++)); nCell.setCellValue("单元格6内容"); nCell = nRow.createCell(cellNo++); nCell.setCellStyle(rowCellsStyle.get(rowCellsStyleIndex++)); nCell.setCellValue("单元格7内容"); nCell = nRow.createCell(cellNo++); nCell.setCellStyle(rowCellsStyle.get(rowCellsStyleIndex++)); nCell.setCellValue("单元格8内容"); rowCellsStyleIndex = 0; } } }
模板debug
结果excel
*关于eclipse自动弹出debug窗口
this kind of launch is configured to open the Debug perspective when it...
查看程序是否没有释放资源,特别是try catch时,加上finally释放资源解决问题code