- Published on
What is ClosedXml, how is it different from OpenXml?
- Authors
- Name
- Rahul Neelakantan
Introduction
ClosedXml is a .NET library for reading, manipulating and writing Excel 2007+ (.xlsx, .xlsm) files. It aims to provide an intuitive and user-friendly interface to dealing with the underlying complex OpenXml API.
To add ClosedXml to your project, you can just install the publicly available nuget package “ClosedXML”.
With ClosedXml, you can work with Excel without having to open it, this helps in backed automation, where you don’t have to deal with open excel instance which can be problematic.
Brief Introduction To OpenXml
The Open XML SDK 2.5 simplifies the task of manipulating Open XML packages and the underlying Open XML schema elements within a package.
With this SDK, we can create & modify Excel, Word, PowerPoint files.
Advantages of OpenXml Formats
OpenXml has many advantages over the binary format
- Compact files – OpenXml files are compressed, up to 75% in some cases. OpenXml format compresses all the XML files in zip format
- Improved damaged-file recovery – Different components are stored in separate files XML files
- Support for advanced features – OpenXml supports advanced features like Autosave & Accessibility Checker, which are not possible in binary format “.xls”
- Better privacy and more control over personal information – With OpenXml we can identify & remove personally identifiable information using Document Inspector, if needed.
- Better integration and interoperability of business data – Zipped XML files are easier to integrate and work with other applications like LibreOffice.
- Easier detection of documents that contain macros – All macro files are having ‘m’ at the end of file extension to easily identify if it contains macros.
ClosedXml
Features
ClosedXml has extensive API functions to handle creation & extraction of content in Excel.
With these API calls you can modify every little detail in Excel sheet or workbook.
It supports most of the major Excel features :-
- Formulas
- Validation
- Hyper-Links
- Protection (Sheet and Cell level)
- Conditional Formatting
- Freeze Panes
- Tables
- Ranges
- Styling
- Page Setup (Printing)
- Auto-Filters
- Comments
Unsupported Features
Some features of OpenXml are still not implemented. They are listed below.
- Macros – ClosedXml doesn’t support macros as its base library OpenXml also doesn’t support it.
- Embedding – We cannot embed any file into Excel using ClosedXml, no APIs built for that, so some features of OpenXml still need to be implemented.
- Charts – No functionality related to charting is present.
Overall, there are not many features that ClosedXml doesn’t support, except for Macros which the base library OpenXml itself doesn’t support.
Example code for ClosedXml
using (var workbook = new XLWorkbook())
{
var worksheet = workbook.Worksheets.Add("NewSheet");
worksheet.Cell("A1").Value = "Hello World!";
workbook.SaveAs("HelloWorld.xlsx");
}
Conclusion
If you’re planning on working with Excel files and have knowledge in C# then I would highly advocate ClosedXml. As it’s a powerful library that can help with your ever-growing requirements.