zoukankan      html  css  js  c++  java
  • 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;
        }
    
        
    
    }
  • 相关阅读:
    hibernate对应的annocation版本
    Struts 2 OGNL
    Struts2的Stack Context和ValueStack
    Struts2中的OGNL详解
    struts2中根对象以及ognl .
    在Struts 2中实现IoC
    Struts2的属性驱动与模型驱动的区别
    Struts2的模型驱动
    Java中线程的锁和数据库中的事务隔离级别
    为什么socket编程要用到多线程
  • 原文地址:https://www.cnblogs.com/lanliying/p/9001786.html
Copyright © 2011-2022 走看看