2014-05-19 209 views
1

我试图使用Apache POI将报告导出为xlsx格式。以下是用于的代码。无效的行号(-32768)超出允许范围(0..1048575)

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); 

     } 

总行数只有20万。但是,当处理,我收到无效的行号error.Here是输出。

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

回答

14

一个短的(你正在投射的)可以表示的最高值是32,767,之后它包装到-32768。

XSSFSheet.createRow需要int,所以你不需要投你的电话号码。

+0

错误已经一去不复返了。现在'线程中的异常'主要“java.lang.OutOfMemoryError:Java堆空间” - 我有我的'JAVA_OPTS = -Xms64m -Xmx516m -XX:MaxPermSize = 1024m' – NaaN

+0

@tailorBird增加可用内存量然后 – awksp

+1

感谢所有。我们可以使用'SXSSF'(Streaming Usermodel API)。 – NaaN

0

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

2

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

由于它是签名类型,其范围是-32768到+32767。将32767增加1会导致最小数字环绕:这就是发生在你身上的事情。

改为使用Long类型:这是32位长。

0

Ehlo,团队!

我试着从列表复制行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) 

但系统具有以下消息响应:

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)

在我的案例解决此问题 - 将参数添加到CellCopyPolicy:

ccp.setCondenseRows(true); 

代码工作区:

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) 

问候& 73)

相关问题