尝试用法语生成 xls 时出现空指针异常

Getting Null Pointer Exception while trying to generate xls in French Language

我在 java 中使用 apache poi 生成 .xls 文件时出现空指针异常。

相同的代码在 "English Language" 上运行良好,但在尝试用 "French" 语言生成时它给了我空指针。

某些列具有空值,但我在创建单元格时处理这些空数据和空白单元格

尝试写出详细信息时得到空指针

"classExcel.write(out)";

以下是我在将所有数据保存在 DTO 中之后用于生成 xls 的方法。

    public void ExcelGenerate(
    ClassExcelDTO classExcel,
    HttpServletResponse response,
    HttpServletRequest request) throws Exception {
    request.setCharacterEncoding("iso-8859-1");
    if( classExcel != null ){
            response.reset();
            response.setContentType("application/csv");
            response.setHeader("Content-disposition","attachment;filename=\"" + classExcel.getName + ".xls\"");
            try {
                if(null!=response && null!=response.getOutputStream()){
                OutputStream out = response.getOutputStream();

                if(null!=out)
                    try {
                        classExcel.write(out);
                    } catch (Exception e) {

                        e.printStackTrace();
                    }

                out.close();
                }
            } catch (SocketException sEx) {

            } catch (IOException io) {
            } catch (Exception e) {

            }
        } else {

        // Some code
        }
}

下面是生成的异常: 14:43:09,037 错误 [stderr] (http-localhost/127.0.0.1:8080-4) java.lang.NullPointerException

14:43:09,037 错误 [stderr] (http-localhost/127.0.0.1:8080-4) 在 org.apache.poi.util.StringUtil.putCompressedUnicode(StringUtil.java:193)

14:43:09,037 错误 [stderr] (http-localhost/127.0.0.1:8080-4) 在 org.apache.poi.hssf.record.BoundSheetRecord.serialize(BoundSheetRecord.java:281)

14:43:09,037 错误 [stderr] (http-localhost/127.0.0.1:8080-4) 在 org.apache.poi.hssf.model.Workbook.serialize(Workbook.java:732)

14:43:09,037 错误 [stderr] (http-localhost/127.0.0.1:8080-4) 在 org.apache.poi.hssf.usermodel.HSSFWorkbook.getBytes(HSSFWorkbook.java:786)

14:43:09,037 错误 [stderr] (http-localhost/127.0.0.1:8080-4) 在 org.apache.poi.hssf.usermodel.HSSFWorkbook.write(HSSFWorkbook.java:732)

如果有任何想法,请帮助我。

我可以解决上面的空指针异常

问题的根本原因。

在使用法语创建每个选项卡时,我传递了包含超过 31 个字符的选项卡名称的键值。但 Apache POI 允许少于 31 个字符(例外--------------java.lang.IllegalArgumentException:Sheet 名称不能为空,不能大于 31 个字符,或包含任何/*?[] ) 并且由于此空白(空)sheet 名称,此空指针异常在编写工作簿时发生。

<i> [BOUNDSHEET]
     .bof             = 0
     .optionflags     = 0
     .sheetname length= 0
     .unicodeflag     = 0
     .sheetname       = null
</i>

所以避免 sheet 名称少于 31 个字符并且 sheet 名称不应包含任何 /*?[] 特殊字符。