Ottimizzazione dell uso della memoria durante il lavoro con grandi file contenenti grandi set di dati

Ottimizzazione della Memoria

Lettura di File Excel di Grandi Dimensioni

L’esempio seguente mostra come leggere un grande file Microsoft Excel in modalità ottimizzata.

// 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);

Scrittura di file Excel di grandi dimensioni

L’esempio seguente mostra come scrivere un ampio dataset in un foglio di lavoro in modalità ottimizzata.

// 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.
// .........

Attenzione

L’opzione predefinita, MemorySetting.Normal è applicata per tutte le versioni. Per alcune situazioni, come la costruzione di un workbook con un ampio set di dati per le celle, l’opzione MemorySetting.MemoryPreference può ottimizzare l’uso della memoria e ridurre il costo della memoria dell’applicazione. Tuttavia, questa opzione può degradare le prestazioni in alcuni casi speciali come segue.

  1. Accesso Casuale e Ripetuto alle Celle: La sequenza più efficiente per accedere alla collezione di celle è cella per cella in una riga e poi riga per riga. In particolare, se si accedono alle righe/celle tramite l’Enumerator acquisito da Cells, RowCollection e Row, le prestazioni sarebbero massimizzate con MemorySetting.MemoryPreference.
  2. Inserimento ed eliminazione di celle e righe: Si noti che se ci sono molte operazioni di inserimento/eliminazione per Celle/Righe, la degradazione delle prestazioni sarà notevole per la modalità MemoryPreference rispetto alla modalità Normale.
  3. Operare su diversi tipi di celle: Se la maggior parte delle celle contiene valori di stringa o formule, il costo della memoria sarà lo stesso della modalità Normale, ma se ci sono molte celle vuote, o i valori delle celle sono numerici, booleani e così via, l’opzione MemorySetting.MemoryPreference darà migliori prestazioni.