Using C# and OpenXML to Read Large Excel Files

Introduction

Reading large Excel files efficiently can be challenging, especially when dealing with applications requiring high performance and scalability. Microsoft's OpenXML SDK provides a powerful set of tools for handling Office documents, including Excel files, without the need for Excel to be installed on the server. This article will guide you in using C# and OpenXML to read large Excel files efficiently.

Why Use OpenXML?

OpenXML is an open standard for office documents (Word, Excel, PowerPoint) and allows for manipulating these documents programmatically. Some benefits of using OpenXML include.

  • Performance: It operates directly on the file streams without loading the entire document into memory.
  • No Dependencies: No need for Microsoft Office to be installed.
  • Scalability: Ideal for server-side applications and batch processing.

Prerequisites

Before diving into the code, ensure you have the following.

  • Visual Studio or any C# IDE
  • .NET Framework or .NET Core SDK
  • OpenXML SDK: You can install it via NuGet with the command.
Install-Package DocumentFormat.OpenXml

Reading Large Excel Files with OpenXML

Here’s a step-by-step guide to reading large Excel files using C# and OpenXML.

1. Setting Up the Project

Create a new C# console application.

  1. Open Visual Studio and create a new Console App (.NET Core or .NET Framework).
  2. Install the OpenXML SDK via NuGet.

2. Opening the Excel File

First, you need to open the Excel file and access the worksheet you want to read. Use the following code to open an Excel file.

using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Spreadsheet;
using System;
using System.Collections.Generic;

namespace ReadLargeExcelFile
{
    class Program
    {
        static void Main(string[] args)
        {
            string filePath = "path/to/your/large/excelfile.xlsx";
            using (SpreadsheetDocument doc = SpreadsheetDocument.Open(filePath, false))
            {
                WorkbookPart workbookPart = doc.WorkbookPart;
                Sheet sheet = workbookPart.Workbook.Sheets.GetFirstChild<Sheet>();
                WorksheetPart worksheetPart = (WorksheetPart)workbookPart.GetPartById(sheet.Id);

                IEnumerable<Row> rows = worksheetPart.Worksheet.GetFirstChild<SheetData>().Elements<Row>();

                foreach (Row row in rows)
                {
                    foreach (Cell cell in row.Elements<Cell>())
                    {
                        string cellValue = GetCellValue(doc, cell);
                        Console.Write(cellValue + " ");
                    }
                    Console.WriteLine();
                }
            }
        }

        private static string GetCellValue(SpreadsheetDocument doc, Cell cell)
        {
            SharedStringTablePart stringTablePart = doc.WorkbookPart.SharedStringTablePart;
            string value = cell.CellValue.InnerXml;

            if (cell.DataType != null && cell.DataType.Value == CellValues.SharedString)
            {
                return stringTablePart.SharedStringTable.ChildElements[Int32.Parse(value)].InnerText;
            }
            else
            {
                return value;
            }
        }
    }
}

3. Handling Large Files Efficiently

The above code reads the entire worksheet into memory, which might not be efficient for very large files. To handle large files more efficiently, consider processing the file in chunks or using streaming techniques.

4. Optimizing for Performance

To optimize performance, you can

  • Stream the File: Use streaming techniques to process the file in parts rather than loading the entire file into memory.
  • Parallel Processing: If your application allows, you can process different parts of the file in parallel.
  • Efficient Data Structures: Use efficient data structures for storing and manipulating data.

Here’s an example using streaming.

using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Spreadsheet;
using System;
using System.Collections.Generic;

namespace ReadLargeExcelFile
{
    class Program
    {
        static void Main(string[] args)
        {
            string filePath = "path/to/your/large/excelfile.xlsx";
            using (SpreadsheetDocument doc = SpreadsheetDocument.Open(filePath, false))
            {
                WorkbookPart workbookPart = doc.WorkbookPart;
                Sheet sheet = workbookPart.Workbook.Sheets.GetFirstChild<Sheet>();
                WorksheetPart worksheetPart = (WorksheetPart)workbookPart.GetPartById(sheet.Id);

                OpenXmlReader reader = OpenXmlReader.Create(worksheetPart);
                while (reader.Read())
                {
                    if (reader.ElementType == typeof(Row))
                    {
                        Row row = (Row)reader.LoadCurrentElement();
                        foreach (Cell cell in row.Elements<Cell>())
                        {
                            string cellValue = GetCellValue(doc, cell);
                            Console.Write(cellValue + " ");
                        }
                        Console.WriteLine();
                    }
                }
            }
        }

        private static string GetCellValue(SpreadsheetDocument doc, Cell cell)
        {
            SharedStringTablePart stringTablePart = doc.WorkbookPart.SharedStringTablePart;
            string value = cell.CellValue.InnerXml;

            if (cell.DataType != null && cell.DataType.Value == CellValues.SharedString)
            {
                return stringTablePart.SharedStringTable.ChildElements[Int32.Parse(value)].InnerText;
            }
            else
            {
                return value;
            }
        }
    }
}

Conclusion

Using C# and OpenXML to read large Excel files provides a robust solution for applications requiring high performance and scalability. By following the practices outlined in this article, you can efficiently process large datasets stored in Excel files, making your applications more efficient and responsive. OpenXML's ability to manipulate Office documents without needing Office installed makes it a valuable tool in any developer's toolkit.


Similar Articles