easyexcel 使用SheetWriteHandler写入csv文件时空指针异常

xiozqbni  于 5个月前  发布在  其他
关注(0)|答案(1)|浏览(43)

代码如下:

@Test
    public void customer() {
        String fileName = TestFileUtil.getPath() + "sheetWriteHandlerForCsv" + System.currentTimeMillis() + ".csv";
        // 这里 需要指定写用哪个class去写
        ExcelWriter excelWriter = EasyExcel.write(fileName)
                .registerWriteHandler(new SheetWriteHandler() {
                    @Override
                    public void afterSheetCreate(WriteWorkbookHolder writeWorkbookHolder, WriteSheetHolder writeSheetHolder) {
                        Sheet sheet = writeSheetHolder.getSheet();

                        for (int column = 0; column < 10; column++) {
                            for (int rowIndex = 0; rowIndex < 20; rowIndex++) {
                                Row row = sheet.getRow(rowIndex);
                                if (row == null) {
                                    row = sheet.createRow(rowIndex);
                                }

                                Cell cell = row.getCell(column);
                                if (cell == null) {
                                    cell = row.createCell(column);
                                }
                                cell.setCellValue("测试" + column + ":" + rowIndex);
                            }
                        }
                    }
                })
                .build();
        WriteSheet writeSheet = EasyExcel.writerSheet("模板").build();
        excelWriter.write(Collections.emptyList(), writeSheet);
        excelWriter.finish();
    }

报错:
java.lang.NullPointerException
at com.alibaba.excel.metadata.csv.CsvSheet.getRow(CsvSheet.java:139)
at com.alibaba.easyexcel.test.demo.rare.CsvTest$1.afterSheetCreate(CsvTest.java:40)
at com.alibaba.excel.write.handler.SheetWriteHandler.afterSheetCreate(SheetWriteHandler.java:37)
at com.alibaba.excel.write.handler.chain.SheetHandlerExecutionChain.afterSheetCreate(SheetHandlerExecutionChain.java:40)
at com.alibaba.excel.util.WriteHandlerUtils.afterSheetCreate(WriteHandlerUtils.java:98)
at com.alibaba.excel.util.WriteHandlerUtils.afterSheetCreate(WriteHandlerUtils.java:92)
at com.alibaba.excel.context.WriteContextImpl.initSheet(WriteContextImpl.java:206)
at com.alibaba.excel.context.WriteContextImpl.currentSheet(WriteContextImpl.java:135)
at com.alibaba.excel.write.ExcelBuilderImpl.addContent(ExcelBuilderImpl.java:54)
at com.alibaba.excel.ExcelWriter.write(ExcelWriter.java:73)
at com.alibaba.excel.ExcelWriter.write(ExcelWriter.java:50)
at com.alibaba.easyexcel.test.demo.rare.CsvTest.customer(CsvTest.java:56)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.platform.commons.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:725)
at org.junit.jupiter.engine.execution.MethodInvocation.proceed(MethodInvocation.java:60)
at

xqnpmsa8

xqnpmsa81#

建议getRow方法不要抛出异常,由外部调用自己做非空校验 @zhuangjiaju

相关问题