2014-07-21 52 views
0

我有一个约16000 X 16000的二维数组,我想将这些记录导出到Excel文件。目前我可以在短时间内输出多达1000个X1000的2D阵列。但是,当我增加大小的数组例如3000 X 3000我的程序运行很长时间而不返回任何数据。 我在寻求帮助将整个2D数组导出到excel文件并使用POI。写一个巨大的数据2D数组,以Excel文件使用Java POI

我的示例代码来导出数据,其中一个参数是我的二维数组。

公共类exportData {

public static void exportDataToExcel(String fileName, String tabName, int[][] data) throws FileNotFoundException, IOException 
    { 
    //Create new workbook and tab 
     Workbook wb = new XSSFWorkbook(); 
     FileOutputStream fileOut = new FileOutputStream(fileName); 
     Sheet sheet = wb.createSheet(tabName); 

     //Create 2D Cell Array 
     Row[] row = new Row[data.length]; 
     Cell[][] cell = new Cell[row.length][]; 

     //Define and Assign Cell Data from Given 
     for(int i = 0; i < row.length; i ++) 
     { 
      row[i] = sheet.createRow(i); 
      cell[i] = new Cell[data[i].length]; 

      for(int j = 0; j < cell[i].length; j ++) 
      { 
       cell[i][j] = row[i].createCell(j); 
       cell[i][j].setCellValue(data[i][j]); 
      } 

     } 

     //Export Data 
     wb.write(fileOut); 
     fileOut.close(); 
     System.out.println("File exported successfully"); 
    } 

}

+1

你尝试调试找出哪些行的执行被挂起的完整的例子吗?可能是wb.write(fileOut);? – Maas

+0

我没有试图做到这一点,但我正在寻找更好的方式输出我的整个数组的excel文件的任何建议。 –

+0

如果可能,你应该避免POI和这样的库,并直接以CSV格式保存文件。 Excel可以打开这些,转换它们等。 – EJP

回答

1

这里使用CSVWritter来打印二维数组

import java.io.File; 
import java.io.FileNotFoundException; 
import java.io.FileWriter; 
import java.io.IOException; 

import au.com.bytecode.opencsv.CSVWriter; 

/** 
* @author Girish 
* 
*/ 
public class CSVWritterExample 
{ 
    public static void main(String[] args) throws FileNotFoundException, IOException 
    { 
     int[][] data = new int[100][100]; 

     for (int i = 0; i < 100; i++) 
     { 
      for (int j = 0; j < 100; j++) 
      { 
       data[i][j] = j * i; 
      } 
     } 

     exportDataToExcel("D:/sample.csv", data); 
    } 

    public static void exportDataToExcel(String fileName, int[][] data) throws FileNotFoundException, IOException 
    { 
     File file = new File(fileName); 
     if (!file.isFile()) 
      file.createNewFile(); 

     CSVWriter csvWriter = new CSVWriter(new FileWriter(file)); 

     int rowCount = data.length; 

     for (int i = 0; i < rowCount; i++) 
     { 
      int columnCount = data[i].length; 
      String[] values = new String[columnCount]; 
      for (int j = 0; j < columnCount; j++) 
      { 
       values[j] = data[i][j] + ""; 
      } 
      csvWriter.writeNext(values); 
     } 

     csvWriter.flush(); 
     csvWriter.close(); 
    } 
} 
2

当我看到你的数据类型为int [] []。所以我相信它的计划静态数据(没有任何Excel公式)

然后,为什么不把你的数据写入CSV文件?它很快+对行数有限制,因为POI限于65,000+条记录新表。

您可以使用CSVWritter

+0

你可以帮助一个教程做到这一点... @Girish –

+0

https://code.google.com/p/opencsv/ – Maas

+0

我试了一会儿,现在整合我的在你的示例教程中,我发现如何编写2D数组时遇到一些困难。我明白这个例子,但问题是如上面的代码中所示,从我的数组中打印重复值。请帮助@ Girish –