POI SXSSFWorkbook read template exists formula solution

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;
/**
 * Template export
 * @author lly
 *
 */
public class TemplateExportServiceImpl implements ExcelExportService {

    /**
     * Template address
     */
    protected String tempAddress;
    /**
     * Template result set
     */
    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);
             // Get the last row in the template to determine whether there is a formula 
            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); // Delete after getting the formula 
            
            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();
                // Empty the number of lines cached in memory 
                if (i % 500 == 0 ) {
                    ((SXSSFSheet) sheet).flushRows();
                }
            }
            // Data cleaning 
            data.clear();
            data = null;
            workbook.setForceFormulaRecalculation(true);//计算公式
            long etime = System.currentTimeMillis();
            System.out.println( "Time to process writing template data: " + (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( "Time to process writing template data: " + (etime - stime) / 1000 );
         return  null ;
    }

    

}

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325521904&siteId=291194637