允许范围外的行号(65536)无效(0..65535)

Cin*_*lla 26 java file-io xls

我正在从文本文件中读取整数,将它们作为查询的输入并获取查询输出并写入xls文件.

ResultSet rs;
Connection con = null;
PreparedStatement ps = null;
int person_org_id, external_person_org_id;
File f = null;
Scanner scan = null;

try {
    System.out.println("----------checkpoint-----------");
    Class.forName("oracle.jdbc.driver.OracleDriver");
    System.out.println("----------checkpoint 1-----------");
    con = DriverManager.getConnection("jdbc:oracle:thin:@ksdjf.kjdlk.jkd.com:2222:edb", "aaaaa", "aaaa");
    System.out.println("----------checkpoint 2 ----------");
    if (con == null) {
        System.out.println("unable to connect to database");
    }
    System.out.println("----------checkpoint 3::connected to database---------");
    StringBuffer sql = new StringBuffer();
    sql.append("select abd from edb.abd where customer_id=510 and person_org_id =? ");
    ps = con.prepareStatement(sql.toString());

    HSSFWorkbook wb = new HSSFWorkbook();
    HSSFSheet sheet = wb.createSheet("Excel Sheet");
    HSSFRow rowhead = sheet.createRow(0);
    rowhead.createCell(0).setCellValue("ABC");
    rowhead.createCell(1).setCellValue("DEF");

    f = new File("/tmp/contacts.txt");
    scan = new Scanner(f);
    int index=1;

    while (scan.hasNextInt()) {

        person_org_id = scan.nextInt();

        ps.setInt(1,person_org_id);
        rs= ps.executeQuery();

        while (rs.next()) {                 

            external_person_org_id = rs.getInt(1);

            HSSFRow row = sheet.createRow(index);
            row.createCell(0).setCellValue(person_org_id);
            row.createCell(1).setCellValue(external_person_org_id);
            index++;
        }           

    }   
    FileOutputStream fileOut = new FileOutputStream(new File("/tmp/External_contact_id.xls"));
    wb.write(fileOut);
    fileOut.close();
    System.out.println("--------checkpoint 4:: writing data to xls completed------------");
}
catch (Exception e) {
    System.out.println(e.getMessage());
}
Run Code Online (Sandbox Code Playgroud)

我收到了错误 Invalid row number (65536) outside allowable range (0..65535)

我的contacts.txt文件有大约36000个数字.

Sim*_*son 40

HSSF以Excel版本(Excel 2003)为目标,最多只支持65536行.

您可以尝试使用较新的XSSF API,它支持更高行限制的更高版本的Excel.

有一个转换指南可以帮助您在两个API之间进行转换.

  • 它可能就像用“new XSSFWorkbook()”替换“new HSSFWorkbook()”一样简单,并从方法调用中删除对“HSSF”的引用。根据 http://poi.apache.org/spreadsheet/converting.html,这些 API 应该非常相似,您应该仔细阅读。确保您使用的是最新版本的 poi JAR。 (2认同)