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

使用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;
                }
            }
        }
    }
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值