在处理拥有大型数据集的大文件时优化内存使用

优化内存

读取大型Excel文件

以下示例展示了如何以优化模式读取大型Microsoft Excel文件。

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getDataDir(ReadLargeExcelFiles.class);
// Specify the LoadOptions
LoadOptions opt = new LoadOptions();
// Set the memory preferences
opt.setMemorySetting(MemorySetting.MEMORY_PREFERENCE);
// Instantiate the Workbook
// Load the Big Excel file having large Data set in it
Workbook wb = new Workbook(dataDir + "Book1.xlsx", opt);

写入大型Excel文件

以下示例显示如何在优化模式下将大型数据集写入工作表。

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getSharedDataDir(WritingLargeExcelFiles.class) + "articles/";
// Instantiate a new Workbook
Workbook wb = new Workbook();
// Set the memory preferences
// Note: This setting cannot take effect for the existing worksheets that are created before using the below line of code
wb.getSettings().setMemorySetting(MemorySetting.MEMORY_PREFERENCE);
/*
* Note: The memory settings also would not work for the default sheet i.e., "Sheet1" etc. automatically created by the
* Workbook. To change the memory setting of existing sheets, please change memory setting for them manually:
*/
Cells cells = wb.getWorksheets().get(0).getCells();
cells.setMemorySetting(MemorySetting.MEMORY_PREFERENCE);
// Input large dataset into the cells of the worksheet.Your code goes here.
// Get cells of the newly created Worksheet "Sheet2" whose memory setting is same with the one defined in
// WorkbookSettings:
cells = wb.getWorksheets().add("Sheet2").getCells();

注意

默认选项{0}适用于所有版本。对于某些情况,例如构建包含大型单元格数据集的工作簿,{1}选项可以优化内存使用并降低应用程序的内存成本。然而,在一些特殊情况下,比如:
1. 随机和重复访问单元格: 访问单元格集合的最有效顺序是逐行逐单元格,然后逐行。特别是,如果通过{0}、{1}和{2}获得的枚举器访问行/单元格,性能将通过{3}得到最大化。

  1. 随机和重复访问单元格:访问单元格集合最有效的顺序是一行一行地逐个访问单元格,尤其是如果通过CellsRowCollectionRow获得的枚举器来访问行/单元格,则使用MemorySetting.MEMORY_PREFERENCE将最大化性能。
  2. 插入和删除单元格和行:请注意,如果有大量的单元格/行插入/删除操作,与MemorySetting.MEMORY_PREFERENCE模式相比,性能下降将非常明显。
  3. 操作不同的单元格类型: 如果大多数单元格包含字符串值或公式,则内存成本与MemorySetting.NORMAL模式相同,但如果存在大量空单元格,或单元格的值是数字、布尔值等,则MemorySetting.MEMORY_PREFERENCE选项将提供更好的性能。