I will answer regarding Aspose.Cells for Java since you tried this too.
Creating or loading a very large Excel file almost always requires a lot of memory. Even if you read one line or several lines at a time, you will still write the contents to an instance of the book, which is loaded into memory.
Solution 1 (insufficient and very limited): Increase the heap size, if the maximum heap size is allowed for your largest file, select it.
Solution 2 (complex with some manual work): Excel 2007 and later versions allow you to create about 1 million rows per sheet. I would suggest you create one book with one sheet for 1 million lines. That is, if you have 10 million lines in a text file, create 10 separate Excel workbooks.
Later, merge them into one Excel workbook manually. Aspose.Cells will throw an exception when copying sheets with such huge data.
Below is a snippet of code that creates 10 separate Excel files, each of which has 1 million lines.
import com.aspose.cells.*; import java.util.*; public class ExcelLargeTextImport { private static String excelFile = Common.dataDir + "largedata.xlsx"; public static void main(String args[]) { try { Common.setLicenses(); importToExcel(); } catch(Exception ex) { System.out.println(ex.getMessage()); } } private static void importToExcel() throws Exception { // Process each workbook in a method for (int sheetCounter=0 ; sheetCounter<10 ; sheetCounter++) { saveWorkbook(sheetCounter); } } private static void saveWorkbook(int sheetCounter) throws Exception { Workbook workbook = new Workbook(); // Get the first sheet Worksheet worksheet = workbook.getWorksheets().get(0); Cells cells = worksheet.getCells(); // Initialize array list with 1 million records ArrayList<String> lines = new ArrayList<String>(); int rowCount = 1000000; for (int i=0 ; i<rowCount ; i++) { lines.add(i + ";value1;value2;value3"); } long lineNo = 1; for (String line : lines) { // Split the line by delimeter String[] values = line.split(";"); // First cell Cell cell = cells.get("A" + lineNo); cell.setValue(values[0]); // Second cell cell = cells.get("B" + lineNo); cell.setValue(values[1]); // Third cell cell = cells.get("C" + lineNo); cell.setValue(values[2]); // Fourth cell cell = cells.get("D" + lineNo); cell.setValue(values[2]); lineNo++; } System.out.print(sheetCounter + " "); // Saving the Excel file workbook.save(excelFile.replace(".xlsx", sheetCounter + ".xlsx")); System.out.println("\nExcel file created"); } }
PS. I am an evangelist developer at Aspose.
source share