Java 超出允许范围 (0..1048575) 的无效行号 (-32768)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23734004/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 00:56:14  来源:igfitidea点击:

Invalid row number (-32768) outside allowable range (0..1048575)

javaapache-poixlsx

提问by being_uncertain

I was trying to export a report in to xlsxformat using Apache POI. Below is the code used for.

我试图将报告导出为xlsx使用 Apache POI 的格式。下面是用于的代码。

public static void main(String[]args){
        try{
            XSSFWorkbook wb=new XSSFWorkbook();
            XSSFSheet sheet = wb.createSheet("new sheet");

            XSSFRow rowhead= sheet.createRow((short)0);
            rowhead.createCell((short) 0).setCellValue("column1");
            rowhead.createCell((short) 1).setCellValue("column2");
            rowhead.createCell((short) 2).setCellValue("column3");
            rowhead.createCell((short) 3).setCellValue("column4");
            rowhead.createCell((short) 4).setCellValue("column5");
                 System.out.println("im here");
            Class.forName("oracle.jdbc.driver.OracleDriver");
            Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:...,....);
            Statement st=con.createStatement();         
            System.out.println("im here1");
            ResultSet rs=st.executeQuery("SELECT * FROM table3 ");
            System.out.println("im here2");         
            int i=1;
            while(rs.next()){
                XSSFRow row= sheet.createRow((short)i);
                row.createCell((short) 0).setCellValue(rs.getString("column1"));
                row.createCell((short) 1).setCellValue(rs.getString("column2"));
                row.createCell((short) 2).setCellValue(rs.getString("column3"));
                row.createCell((short) 3).setCellValue(rs.getString("column4"));
                row.createCell((short) 4).setCellValue(rs.getString("column5"));
                i++;
            }

            FileOutputStream fileOut = new FileOutputStream(new File("E:/report.xlsx"));
            wb.write(fileOut);
            fileOut.close();
            System.out.println("Your excel file has been generated!");


        } catch ( Exception ex ) {
            System.out.println(ex);

        }

The total row count is only 200000. But while processing, I am getting the Invalid row number error.Here is the output.

总行数仅为 200000。但在处理时,我收到无效行号错误。这是输出。

im here
im here1
im here2
java.lang.IllegalArgumentException: Invalid row number (-32768) outside allowable range (0..1048575)

采纳答案by Evan Knowles

The highest value that a short (which you're casting to) can represent is 32,767, after which it wraps around to -32768.

短(您要转换到的)可以表示的最高值是 32,767,之后它会环绕到 -32768。

XSSFSheet.createRowtakes an int, so you don't need to cast your number.

XSSFSheet.createRow需要一个int,所以你不需要投射你的号码。

回答by Nikolay Lagutko

I believe you should use long instead of int.

我相信你应该使用 long 而不是 int。

回答by Bathsheba

The Integer type in VBA is only 16 bits long. (I kid you not).

VBA 中的整数类型只有 16 位长。(我不骗你)。

As it's a signed type its range is -32768 to +32767. Incrementing 32767 by 1 causes wrap-around to the smallest number: this is what's happening to you.

因为它是有符号类型,所以它的范围是 -32768 到 +32767。将 32767 增加 1 会导致回绕到最小的数字:这就是发生在你身上的事情。

Use a Longtype instead: which is 32 bits long.

改用一个Long类型:它是 32 位长。

回答by Cristopher Plasma

Ehlo, team!

嘿,团队!

I try copy rows from Listto XSSLSheetFor this I try execute next code:

我尝试将行从List复制到XSSLSheet为此,我尝试执行下一个代码:

HashMap<String, XSSFRow> listRows = new HashMap<String, XSSFRow>();
List<XSSFRow> rl = new ArrayList<XSSFRow>(listRows.values());
CellCopyPolicy ccp = new CellCopyPolicy();
ccp.setCopyCellValue(true);
ccp.setCopyCellStyle(false);
ccp.setCopyMergedRegions(false);

destSheet.copyRows(rl, 5, ccp)

But system responds with the following message:

但系统响应以下消息:

java.lang.IllegalArgumentException: Invalid row number (-354) outside allowable range (0..1048575) org.apache.poi.xssf.usermodel.XSSFRow.setRowNum(XSSFRow.java:407) org.apache.poi.xssf.usermodel.XSSFSheet.createRow(XSSFSheet.java:739) org.apache.poi.xssf.usermodel.XSSFSheet.copyRows(XSSFSheet.java:2897)

java.lang.IllegalArgumentException:无效的行号 (-354) 超出允许范围 (0..1048575) org.apache.poi.xssf.usermodel.XSSFRow.setRowNum(XSSFRow.java:407) org.apache.poi.xssf。 usermodel.XSSFSheet.createRow(XSSFSheet.java:739) org.apache.poi.xssf.usermodel.XSSFSheet.copyRows(XSSFSheet.java:2897)

In my case resolve of this trouble - add parametr to CellCopyPolicy:

在我的情况下解决了这个问题 - 将参数添加到 CellCopyPolicy:

ccp.setCondenseRows(true);

Code working area:

代码工作区:

HashMap<String, XSSFRow> listRows = new HashMap<String, XSSFRow>();
List<XSSFRow> rl = new ArrayList<XSSFRow>(listRows.values());
CellCopyPolicy ccp = new CellCopyPolicy();
ccp.setCopyCellValue(true);
ccp.setCopyCellStyle(false);
ccp.setCopyMergedRegions(false); 
ccp.setCondenseRows(true); //look here

destSheet.copyRows(rl, 5, ccp)

Best regards & 73 )

最好的问候和 73 )