Open Source .NET Library for Managing Spreadsheet Files

Read, Write, Manipulate & Convert Excel Spreadsheet files via Open Source Free .NET API.

 

Open XML SDK is a pure .NET library created by Microsoft to work with Microsoft Office documents. The API is open source and it is distributed under Microsoft open-source code of conduct.

Using the API you can generate a high-performance spreadsheet document and extract data from excel documents. Furthermore, you can also perform document modifications like adding, updating and removing document content as well as metadata. The Open source library support several .NET platforms such as, .NET 3.5, .NET 4.0, .NET 4.6 as well as .NET Standard 1.3.

Previous Next

Getting Started with Open XML SDK

You need to have .NET Framework 3.5 or above. Once you have met the prerequisites, you can manually download the repository from GitHub or install from NuGet.

Open XML SDK Installation via NuGet

 Install-Package DocumentFormat.OpenXml

Manipulate Spreadsheet Documents using C#

The Open Source library allows .NET programmers to create as well as modify Excel spreadsheets from within their own .NET applications. Once the worksheet is created you can add sheets, text, table, formulas and more.

Create a Spreadsheet Document - C#

// Create a new XLSX file
SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.Create("fileformat.xlsx", SpreadsheetDocumentType.Workbook);
// Add a WorkbookPart
WorkbookPart workbookpart = spreadsheetDocument.AddWorkbookPart();
workbookpart.Workbook = new Workbook();
// Add a WorksheetPart 
WorksheetPart worksheetPart = workbookpart.AddNewPart();
worksheetPart.Worksheet = new Worksheet(new SheetData());
// Add Sheets 
Sheets sheets = spreadsheetDocument.WorkbookPart.Workbook.AppendChild(new Sheets());
Sheet sheet = new Sheet() { Id = spreadsheetDocument.WorkbookPart.GetIdOfPart(worksheetPart), SheetId = 1, Name = "fileformat" };
sheets.Append(sheet);
// Save workbook
workbookpart.Workbook.Save();
// Close the document.
spreadsheetDocument.Close();

Reading Excel Sheet with C#

The Open Source API gives computer programmers the ability to access and read Excel worksheet inside their own C# applications. You can easily open an existing worksheet by providing its name and address.  The library also provides features for printing sheet name, get data from a worksheet, print total number of rows in a sheet and much more.

Reading Excel Data - C#

// Open an existing XLSX file 
WorkbookPart workbookPart = SpreadsheetDocument.Open("D:\\sample.xlsx", false).WorkbookPart;
// Get Sheets 
var sheets = workbookPart.Workbook.Descendants();
var sheet = sheets.First();
// Print sheet name
Console.WriteLine(sheet.Name);
// Get worksheet 
var workSheet = ((WorksheetPart)workbookPart.GetPartById(sheet.Id)).Worksheet;
// Get worksheet data
var sheetData = workSheet.Elements().First();
List rows = sheetData.Elements().ToList();
// Print total number of rows
Console.WriteLine("Total Number of rows: " + rows.Count());