java - Apache POI SXSSF 在刷新行上显示 NullPointerException

标签 java jdbc apache-poi sxssf

我的表中有 500 行。当我使用 setRandomAccessWindowSize(1000) 时,它工作正常。数据已从结果集中成功导出到 Excel 文件中,但是当我使用 setRandomAccessWindowSize(100) 时,它给了我一个 NullPointerException 我不知道我做错了什么。请建议正确的方法来执行此操作。

这是我的代码:

workbook = new SXSSFWorkbook(100);      //SXSSF workbook
workbook.setCompressTempFiles(true);
SXSSFSheet spreadsheet = workbook.createSheet("Sheet1");     //Generating Excel file
SXSSFRow row;
SXSSFCell cell;
CellStyle style = workbook.createCellStyle();
Font font = workbook.createFont();//Create font
font.setBold(true);//Make font bold
style.setFont(font);//set it to bold

int y = 1;
if (rs.isBeforeFirst() == true) {
    while (rs.next()) {
        row = spreadsheet.createRow(0);
        cell = row.createCell(0);
        cell.setCellValue("SR NO");
        cell.setCellStyle(style);
        for (int s = 1; s <= rsmd.getColumnCount(); s++) {
            cell = row.createCell(s);
            cell.setCellValue(rs.getMetaData().getColumnName(+s).toUpperCase());
            cell.setCellStyle(style);
        }
        row = spreadsheet.createRow(y);
        cell = row.createCell(0);
        cell.setCellValue(y + "");
        //spreadsheet.autoSizeColumn(0);
        for (int x = 1; x <= rsmd.getColumnCount(); x++) {
            cell = row.createCell(x);
            String address = new CellReference(cell).formatAsString();
            cell.setCellValue(address);
            cell.setCellValue(rs.getString(+x));
            //spreadsheet.autoSizeColumn(x);
        }
        y++;
    }

    FileOutputStream out = new FileOutputStream(new File(destination));
    workbook.write(out);
    out.close();
    workbook.dispose();

最佳答案

我已经更新了上面代码中的 while 循环。它多次写入第 0 行。这就是为什么在第 101 行它给出 NullPointer 异常,因为第 0 行已经被刷新。

这是我的修复-

if (rs.isBeforeFirst() == true) {
    while (rs.next()) {
        //writing columns
        if (rs.isFirst()) {
            row = spreadsheet.createRow(0);
            cell = row.createCell(0);
            cell.setCellValue("SR NO");
            cell.setCellStyle(style);
            for (int s = 1; s <= rsmd.getColumnCount(); s++) {
                cell = row.createCell(s);
                cell.setCellValue(rs.getMetaData().getColumnName(+s).toUpperCase());
                cell.setCellStyle(style);
            }
        }
        //Writing data
        row = spreadsheet.createRow(y);
        cell = row.createCell(0);
        cell.setCellValue(y + "");
        //spreadsheet.autoSizeColumn(0);
        for (int x = 1; x <= rsmd.getColumnCount(); x++) {
            cell = row.createCell(x);
            cell.setCellValue(rs.getString(+x));
            //spreadsheet.autoSizeColumn(x);
        }
        y++;
    }

    FileOutputStream out = new FileOutputStream(new File(destination));
    workbook.write(out);
    out.close();
    workbook.dispose();

关于java - Apache POI SXSSF 在刷新行上显示 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60287789/

相关文章:

java - 带有 Projection 的 Spring JPA native 查询给出 "ConverterNotFoundException"

java - 我最近将我的项目转换为 Maven。但出现错误Resource read error : Could not load com/sun/xml/txw2/Content. class

java - JTable刷新数据不显示

apache-poi - Apache POI ppt。如何在幻灯片中调整图像大小和居中

java - CTP和PPR是什么意思?

java - java中如何读取超过100000行的excel文件?

java - 当一个对象只被定义时会发生什么?

java - 始终打开(创建)Eclipse RCP View

java - 如何访问oracle 11g中的默认连接 'xe'来创建用户?

java - 为什么 SQL 查询不能直接使用类的实例