Ottimizzazione dell'utilizzo della memoria mentre si lavora con file di grandi dimensioni con set di dati di grandi dimensioni

Ottimizzazione della memoria

Lettura di file Excel di grandi dimensioni

L’esempio seguente mostra come leggere un file Excel di grandi dimensioni Microsoft 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 set di dati di grandi dimensioni 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è applicato per tutte le versioni. Per alcune situazioni, come la creazione di una cartella di lavoro con un set di dati di grandi dimensioni per le celle, il fileMemorySetting.MemoryPreferenceopzione può ottimizzare l’uso della memoria e diminuire il costo della memoria per l’applicazione. Tuttavia, questa opzione potrebbe peggiorare le prestazioni in alcuni casi speciali come follow.

  1. Accesso allo Cells in modo casuale e ripetuto : La sequenza più efficiente per accedere alla raccolta di celle è cella per cella in una riga, quindi riga per riga. Soprattutto se accedi a righe/celle dall’enumeratore acquisito daCells, RowCollection eRiga , le prestazioni sarebbero massimizzate conMemorySetting.MemoryPreference.
  2. Inserimento e cancellazione Cells e righe : Si prega di notare che se ci sono molte operazioni di inserimento/cancellazione per Cells/Rows, il degrado delle prestazioni sarà notevole perMemoryPreference modalità rispetto allaNormalemodalità.
  3. Funzionante su diversi tipi Cell : Se la maggior parte delle celle contiene valori stringa o formule, il costo della memoria sarà lo stesso diNormale mode ma se ci sono molte celle vuote o i valori delle celle sono numerici, bool e così via, ilMemorySetting.MemoryPreferencel’opzione darà prestazioni migliori.