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

apache-poi, java, xlsx

Solution

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

XSSFSheet.createRow takes an `int`, so you don't need to cast your number.

Problem

I was trying to export a report in to `xlsx` format using Apache POI. Below is the code used for. ``` 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. ``` im here im here1 im here2 java.lang.IllegalArgumentException: Invalid row number (-32768) outside allowable range (0..1048575) ```

Original source