Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

使用FillConfig.builder().forceNewRow(true)函数之后,他会将我需要的固定行还是给覆盖掉,大佬有解决方法不 #3845

Open
javaHBZ opened this issue Jun 19, 2024 · 0 comments
Labels
help wanted Extra attention is needed

Comments

@javaHBZ
Copy link

javaHBZ commented Jun 19, 2024

@RestControllerMapping("/erpIntf/spot_check_excel")
public class EquipmentSpotCheckExcelController {
    @GetMapping("/excel")
    public void easyExcelTest(HttpServletResponse response) throws IOException {
        // 填充普通数据
        Map<String, String> fillMap = new HashMap<>();
        fillMap.put("equipmentNumber", "小力设备");
        fillMap.put("lineNumber", "四线");
        fillMap.put("y1", "2024");
        fillMap.put("m1", "06");
        fillMap.put("ab", "A班");
        fillMap.put("bb", "b班");
        // 填充的集合数据
        List<Map<String, String>> dataList = getDataList();

        // 模板文件流
        //ByteArrayOutputStream outputStream = EasyExcelUtils.exportExcelByTemplate(fillMap, dataList, "", 0, true, new MyCellWriteHandler());
        InputStream fileInputStream = Files.newInputStream(ResourceUtils.getFile("E:\\jobwork\\文档\\大理华晟MES\\XX点检记录表(导出模板)(1).xlsx").toPath());
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        ExcelWriter excelWriter = null;
        try {
            ExcelWriterBuilder write = EasyExcel.write(outputStream);
            write.registerWriteHandler(new MyCellWriteHandler());
            excelWriter = write.withTemplate(fileInputStream).build();
            /*
             这里注意 入参用了forceNewRow 代表在写入list的时候不管list下面有没有空行 都会创建一行,然后下面的数据往后移动。默认 是false,会直接使用下一行,如果没有则创建。
             forceNewRow 如果设置了true,有个缺点 就是他会把所有的数据都放到内存了,所以慎用
             简单的说 如果你的模板有list,且list不是最后一行,下面还有数据需要填充 就必须设置 forceNewRow=true 但是这个就会把所有数据放到内存 会很耗内存
             如果数据量大 list不是最后一行 参照下一个
             */
            FillConfig fillConfig = FillConfig.builder().forceNewRow(true).build();

            // sheet页
            WriteSheet writeSheet = EasyExcel.writerSheet(0).build();

            excelWriter.fill(fillMap, writeSheet);
            // 填充list数据,因为list数据后面还需填充其他数据,所以需要fillConfig
            excelWriter.fill(dataList, fillConfig, writeSheet);
        } finally {
            //关闭流
            if (excelWriter != null) {
                excelWriter.finish();
            }
        }
        // 下面还可以使用poi进行二次操作
        //InputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray());
        //Workbook workbook = new XSSFWorkbook(inputStream);
        //Sheet sheetAt = workbook.getSheetAt(0);
        //Row row = sheetAt.getRow(2);
        //int col = CellReference.convertColStringToIndex("F");
        //Cell cell = row.getCell(col);
        //cell.setCellValue("poi设置的值");
        //setCellStyle(cell);

        // 运算函数
//        XSSFFormulaEvaluator.evaluateAllFormulaCells(workbook);

        //ByteArrayOutputStream outputStream1 = new ByteArrayOutputStream();
        //workbook.write(outputStream1);

        EasyExcelUtils.toResponseStream(outputStream, response);

    }

    /**
     * 测试数据
     */
    public static List<Map<String, String>> getDataList() {
        List<Map<String, String>> listData = new ArrayList<>();
        Random random = new Random();
        for (int i = 0; i < 10; i++) {
            Map<String, String> map = new HashMap<>();
            map.put("d", String.valueOf(random.nextInt(101)));
            map.put("e", String.valueOf(random.nextInt(101)));
            map.put("d1", String.valueOf(random.nextInt(101)));
            map.put("d2", String.valueOf(random.nextInt(101)));
            map.put("e1", String.valueOf(random.nextInt(101)));
            map.put("e2", String.valueOf(random.nextInt(101)));
            listData.add(map);
        }
        return listData;
    }

问题描述

下面是我填充的模板,红线框出是需要动态的填充的模板部分
image
在我的示例代码中就填充了几个,呈现的效果如下
image
他并没有展示20行数据,并且中间的“设备技术员签字确认”行不见了“
想要保留,并且{.d}列和{.e}各有十行数据,帮忙看看呗,大佬

@javaHBZ javaHBZ added the help wanted Extra attention is needed label Jun 19, 2024
@javaHBZ javaHBZ changed the title 使用FillConfig.builder().forceNewRow(true)函数之后,他会将我需要的固定行给覆盖掉,大佬有解决方法不 使用FillConfig.builder().forceNewRow(true)函数之后,他会将我需要的固定行还是给覆盖掉,大佬有解决方法不 Jun 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

1 participant