美文网首页
poi使用excel模板导出数据,如何防止模板被修改

poi使用excel模板导出数据,如何防止模板被修改

作者: chenzan | 来源:发表于2019-04-19 11:17 被阅读0次

使用poi根据Excel模板填充数据

遇到的问题(模板文件会被修改 类似有缓存)

  1. 通过 File方式创建Workbook时源模板文件会被修改,Workbook中会残留之前的数据,但模板文件 xls中并不会体现

     File excelFile = GetLocalFileUtil.getFile("temp.xlsx", "fileTemp");
     //通过file创建
     XSSFWorkbook workbook = XSSFWorkbookFactory.createWorkbook(excelFile)
    

    解决方案
    使用流创建Workbook

     File excelFile = GetLocalFileUtil.getFile("temp.xlsx", "fileTemp");
     //通过流创建workbook
     XSSFWorkbook workbook = XSSFWorkbookFactory.createWorkbook(new FileInputStream(excelFile));
     //=======================2007之下版本===========================//
     POIFSFileSystem poifsFileSystem = new POIFSFileSystem(excelFile);
     Workbook workbook = WorkbookFactory.create(poifsFileSystem);
    

模板样例

image.png

完整代码 只针对xssf

public void generatorFile(String username, List<DataBean> dataBean){
    File excelFile = GetLocalFileUtil.getFile("temp.xlsx", "fileTemp");
    XSSFWorkbook workbook = null;
    FileOutputStream fileOutputStream = null;
    try {
        workbook = XSSFWorkbookFactory.createWorkbook(new FileInputStream(excelFile));

        CellStyle cellStyle = workbook.createCellStyle();
        cellStyle.setBorderBottom(BorderStyle.THIN);
        cellStyle.setBorderLeft(BorderStyle.THIN);
        cellStyle.setBorderTop(BorderStyle.THIN);
        cellStyle.setBorderRight(BorderStyle.THIN);
        cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
        cellStyle.setWrapText(true);

        Sheet sheet = workbook.getSheetAt(0);
        //处理title
        handlerTitle(currentMonth, username, sheet);
        //处理内容
        handlerRowData(sheet, cellStyle, dataBean);

        String filename = "%1$s-%2$d月任务明细表.xlsx";
        String formatFilename = String.format(filename, sysUserCard.getUsername(), currentMonth);
        String exportFilePath = FileUtil.getExportFilePath(excelUploadPath);
        File saveFile = FileUtil.getSaveFilePath(exportFilePath, formatFilename);
        fileOutputStream = new FileOutputStream(saveFile);
        workbook.write(fileOutputStream);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (workbook != null) {
            try {
                workbook.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (fileOutputStream != null) {
            try {
                fileOutputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
private void handlerTitle(int currentMonth, String username, Sheet sheet) {
    Row titleRow = sheet.getRow(0);
    Cell titleRowCell = titleRow.getCell(0);
    String titleContent = titleRowCell.getStringCellValue();
    String formatRowCellValue = String.format(titleContent, username, currentMonth,
            DateUtil.convertDateToString(new Date(), DateUtil.SIMPLE_DATE_TIME_PATTERN));
    titleRowCell.setCellValue(formatRowCellValue);
}

private void handlerRowData(Sheet sheet, CellStyle cellStyle, List<DataBean> dataBean) {
    Row headRow = sheet.getRow(1);
    int physicalNumberOfCells = headRow.getPhysicalNumberOfCells();
    for (int i = 0; i < taskCardList.size(); i++) {
        int lastNewRowNum = sheet.getLastRowNum();
        Row newRow = sheet.createRow(lastNewRowNum + 1);

        TaskCard taskCard = taskCardList.get(i);
        String taskId = taskCard.getParentId();
        Task task = daoTask.findOne("taskId", taskId);

        String projectId = task.getParentId();
        TaskProject taskProject = daoTaskProject.findOne("taskId", projectId);

        for (int cellIndex = 0; cellIndex < physicalNumberOfCells; cellIndex++) {
            Cell cell = newRow.createCell(cellIndex);
            cell.setCellStyle(cellStyle);

            Comment cellComment = headRow.getCell(cellIndex).getCellComment();
            String comment = "";
            if (cellComment != null) {
                comment = cellComment.getString().toString().trim();
            }
            switch (comment) {
                case "number":
                    cell.setCellValue(i + 1);
                    break;
                case "projectName":
                    cell.setCellValue(dataBean.getProjectName());
                    break;
                case "taskName":
                    cell.setCellValue(dataBean.getTaskName());
                    break;
                case "subTaskName":
                    cell.setCellValue(dataBean.getSubTaskName());
                    break;
                case "taskDesc":
                    cell.setCellValue(dataBean.getTaskDetail());
                    break;
                case "taskType":
                    cell.setCellValue(dataBean.getTaskType());
                    break;
                case "taskStatus":
                    cell.setCellValue(dataBean.getTaskStatus());
                    break;
                case "startTime":
                    cell.setCellValue(dataBean.getStartTime());
                    break;
            }
        }
    }
}

相关文章

网友评论

      本文标题:poi使用excel模板导出数据,如何防止模板被修改

      本文链接:https://www.haomeiwen.com/subject/tvcqgqtx.html