0

I have a 2D arrays about 16000 X 16000 and i want to export these records to excel file. currently i can export up to 1000 X1000 of that 2D array within short time. But when I increase the size the array for example 3000 X 3000 my program run for a long time without returning any data. I am asking for help to export the whole 2D array to an excel file and am using POI.

My sample codes to export data where one if the parameters is my 2D array.

public class 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");
  }

}

3
  • 1
    Did you try debugging to find out which line the execution is hung? May be wb.write(fileOut);? Commented Jul 21, 2014 at 10:11
  • i didn't try to do that but i am looking for any suggestions of better way of output my whole array to excel file. Commented Jul 21, 2014 at 10:15
  • If possible you should avoid POI and such libraries and just save the file directly in CSV format. Excel can open those, convert them, etc. Commented Jul 21, 2014 at 10:23

2 Answers 2

2

As I see your data is int[][]. So I believe that its plan static data (without any excel formulas)

Then, why don't you write your data into a CSV file? it is quick + there is restriction on the number of rows as in POI limited to 65,000+ records new sheet.

You can use CSVWritter

Sign up to request clarification or add additional context in comments.

2 Comments

Can you help with a tutorial to do this... @Girish
I tried for a while now to intergrate my array in your sample tutorial i found some difficulties how to write 2D array. i understand the example but the problem is to print the repsective values from my array as shown in the code above. Please help @ Girish
1

Here the complete example of using CSVWritter to print your 2D array

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.