2014-05-19 214 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)

相關問題