1. 程式人生 > >POI SXSSFWorkbook 讀取模板 存在公式解決

POI SXSSFWorkbook 讀取模板 存在公式解決

str mode asn 內存 bsp cal reat AC shee

package com.baoqilai.base.service.export;

import java.io.File;
import java.io.FileInputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.DataFormat;
import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.xssf.streaming.SXSSFSheet; import org.apache.poi.xssf.streaming.SXSSFWorkbook; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import com.baoqilai.ddg.util.ExcelUtil; import com.baoqilai.scp.exception.BaseException;
import com.baoqilai.scp.service.export.ExcelExportService; import com.baoqilai.scp.service.export.ExcelExportStragy; /** * 模板導出 * @author lly * */ public class TemplateExportServiceImpl implements ExcelExportService { /** * 模板地址 */ protected String tempAddress; /** * 模板結果集
*/ protected String[] result; public TemplateExportServiceImpl(String tempAddress, String[] result) { super(); this.tempAddress = tempAddress; this.result = result; } @Override public SXSSFWorkbook export(List<Map<String, Object>> data) throws BaseException { long stime = System.currentTimeMillis(); try { File fi = new File(tempAddress); FileInputStream is = new FileInputStream(fi); XSSFWorkbook wb = new XSSFWorkbook(is); //獲取模板中最後一行,用於判斷是否存在公式 int lastRowNum = wb.getSheetAt(0).getLastRowNum(); Sheet sheet0 = wb.getSheetAt(0); Row baseRow0=sheet0.getRow(2); lastRowNum = wb.getSheetAt(0).getLastRowNum(); Map<Integer, String> gsMap=new HashMap<>(); for (Iterator<Cell> it = baseRow0.cellIterator(); it.hasNext();) { Cell baseCell = it.next(); if (baseCell.getCellType() == Cell.CELL_TYPE_FORMULA) { String cellFormula = baseCell.getCellFormula(); gsMap.put(baseCell.getColumnIndex(), cellFormula); } } sheet0.removeRow(baseRow0); //取到公式後進行刪除 SXSSFWorkbook workbook = new SXSSFWorkbook(wb, 500); Sheet sheet = workbook.getSheetAt(0); CellStyle contextstyle = workbook.createCellStyle(); DataFormat df = workbook.createDataFormat(); contextstyle.setDataFormat(df.getFormat("#,##0.00")); final int startRow = lastRowNum; for (int i = startRow; i < data.size() + startRow; i++) { int rowNum = i - startRow; Row row = sheet.getRow(i); if (row == null) { row = sheet.createRow(i); } Map<String, Object> dataMap = data.get(rowNum); String[] columNames = result; dataMap.put("serialNum", rowNum + 1); for (int j = 0; j < columNames.length; j++) { Cell cell = row.getCell(j); if (cell == null) { cell = row.createCell(j); } System.out.println(cell.getColumnIndex()); Object val = dataMap.get(columNames[j]); ExcelUtil.setCellValue(cell, val, contextstyle); if(gsMap.get(cell.getColumnIndex())!=null){ String cellFormula =gsMap.get(cell.getColumnIndex()); String s = cellFormula.replaceAll("(\\w)\\d", "$1" + (i + 1)); cell.setCellFormula(s); cell.setCellType(Cell.CELL_TYPE_FORMULA); } } dataMap.clear(); // 清空內存中緩存的行數 if (i % 500 == 0) { ((SXSSFSheet) sheet).flushRows(); } } // 數據清理 data.clear(); data = null; workbook.setForceFormulaRecalculation(true);//計算公式 long etime = System.currentTimeMillis(); System.out.println("處理寫入模板數據用時:" + (etime - stime) / 1000); return workbook; } catch (Exception e) { e.printStackTrace(); } return null; } @Override public SXSSFWorkbook exportByStragegy(List<Map<String, Object>> data, ExcelExportStragy stragegy) throws BaseException { long stime = System.currentTimeMillis(); long etime = System.currentTimeMillis(); System.out.println("處理寫入模板數據用時:" + (etime - stime) / 1000); return null; } }

POI SXSSFWorkbook 讀取模板 存在公式解決