Optimización del uso de la memoria mientras se trabaja con archivos grandes que tienen grandes conjuntos de datos

Optimización de la memoria

Leer archivos grandes de Excel

El siguiente ejemplo muestra cómo leer un archivo de Excel grande Microsoft en modo optimizado.

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
// Specify the LoadOptions
LoadOptions opt = new LoadOptions();
// Set the memory preferences
opt.MemorySetting = MemorySetting.MemoryPreference;
// Instantiate the Workbook
// Load the Big Excel file having large Data set in it
Workbook wb = new Workbook(dataDir+ "Book1.xlsx", opt);

Escribir archivos grandes de Excel

El siguiente ejemplo muestra cómo escribir un conjunto de datos grande en una hoja de trabajo en un modo optimizado.

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
// 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.Settings.MemorySetting = MemorySetting.MemoryPreference;
// 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.Worksheets[0].Cells;
cells.MemorySetting = MemorySetting.MemoryPreference;
// 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.Worksheets.Add("Sheet2").Cells;
// .........
// Input large dataset into the cells of the worksheet.
// Your code goes here.
// .........

Precaución

La opción por defecto,AjusteMemoria.Normalse aplica para todas las versiones. Para algunas situaciones, como crear un libro de trabajo con un gran conjunto de datos para celdas, elMemorySetting.MemoryPreferenceLa opción puede optimizar el uso de la memoria y disminuir el costo de la memoria para la aplicación. Sin embargo, esta opción puede degradar el rendimiento en algunos casos especiales, como el siguiente.

  1. Acceso al Cells de forma aleatoria y repetida : La secuencia más eficiente para acceder a la colección de celdas es celda por celda en una fila y luego fila por fila. Especialmente, si accede a filas/celdas por el Enumerador adquirido deCells, RowCollection yFila , el rendimiento se maximizaría conMemorySetting.MemoryPreference.
  2. Inserción y eliminación de Cells y filas : Tenga en cuenta que si hay muchas operaciones de inserción/eliminación para Cells/Rows, la degradación del rendimiento será notable paraMemoriaPreferencia modo en comparación con elNormalmodo.
  3. Operando en diferentes tipos Cell : Si la mayoría de las celdas contienen valores de cadena o fórmulas, el costo de memoria será el mismo queNormal pero si hay muchas celdas vacías, o los valores de las celdas son numéricos, booleanos, etc., elMemorySetting.MemoryPreferenceopción dará un mejor rendimiento.