Hi @R, Divya
When we open the input excel file in Microsoft Excel Application and modify any cell values and then try to read the same file in C# Console App using OpenXML library, the time to read 4 lakh records is growing even more than few hours.
After modifying the excel file, whether the excel file size changed to much? And when read the excel file, whether the memory usage is very high?
As far as I know, when using SpreadsheetDocument
and SheetData
classes, which load the entire file into memory and can cause performance issues for large files. So, if the issue relates the memory, you can employ an OpenXMLReader
to read the XML in the file one element at a time, without having to load the entire file into memory.
Code like this:
// The SAX approach.
static void ReadExcelFileSAX(string fileName)
{
using (SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.Open(fileName, false))
{
WorkbookPart workbookPart = spreadsheetDocument.WorkbookPart;
WorksheetPart worksheetPart = workbookPart.WorksheetParts.First();
OpenXmlReader reader = OpenXmlReader.Create(worksheetPart);
string text;
while (reader.Read())
{
if (reader.ElementType == typeof(CellValue))
{
text = reader.GetText();
Console.Write(text + " ");
}
}
Console.WriteLine();
Console.ReadKey();
}
}
More detail information, see Parse and read a large spreadsheet document (Open XML SDK) (#Approaches to Parsing Open XML Files).
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.
Best regards,
Dillion