POI SXSSFWorkbook 读取模板 存在公式解决

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;
    }

    

}

 

转载于:https://www.cnblogs.com/lanliying/p/9001786.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值