Open Source .NET Library for Reading Excel Spreadsheets
ExcelDataReader allows you read Microsoft Excel file formats using C#.
ExcelDataReader is an open source lightweight API written in C# for reading Microsoft Excel Files. Using the API you can read Microsoft XLS, XLSX, and CSV easily. The API supports older versions of XLS files back to Excel 2.0, supports text dates, cached formula values, and empty sheet paths in XLSX.
Furthermore, the API supports fallback encoding in XLS and more flexible column name handling in Datasets. It is easy to configure and is available on NuGet.
Getting Started with ExcelDataReader
The recommended way to install ExcelDataReader is from NuGet, Please use the following command for faster installation.
Install ExcelDataReader from NuGet
Install-Package ExcelDataReader -Version 3.6.0
Read Excel Files via .NET API
ExcelDataReader allows C# .NET developers to read Microsoft Excel Files easily and efficiently. The AsDataSet() extension method is a convenient helper for quickly getting the data. IExcelDataReader extends the System.Data.IDataReader and IDataRecord interfaces to navigate and retrieve data at a lower level.
Reading Header and Footer of Excel Files via C# .NET
sing System;
using System.Text;
namespace ExcelDataReader.Core.BinaryFormat
{
///
/// Represents a string value of a header or footer.
///
internal sealed class XlsBiffHeaderFooterString : XlsBiffRecord
{
private readonly IXlsString _xlsString;
internal XlsBiffHeaderFooterString(byte[] bytes, uint offset, int biffVersion)
: base(bytes, offset)
{
if (biffVersion < 8)
_xlsString = new XlsShortByteString(bytes, offset + 4);
else if (biffVersion == 8)
_xlsString = new XlsUnicodeString(bytes, offset + 4);
else
throw new ArgumentException("Unexpected BIFF version " + biffVersion, nameof(biffVersion));
}
///
/// Gets the string value.
///
public string GetValue(Encoding encoding)
{
return _xlsString.GetValue(encoding);
}
}
}
Read Protected Workbooks via .NET API
The open source .NET API ExcelDataReader also allows you to read password protected Microsoft Excel documents. You can read password-protected files using the password setting in ExcelReaderConfiguration configuration and opening it using CreateOpenXmlReader() method.
How to Apply Formatting to Spreadsheet Cells via C# API
// Use the following code to Access your protected Spreadsheet file
var conf = new ExcelReaderConfiguration { Password = "yourPassword" };
excelReader = ExcelReaderFactory.CreateOpenXmlReader(excelStream, conf);
Apply Formatting to Excel Spreadsheet Cells using C#
The open source ExcelDataReader library allows software programmers to apply formatting to their excel cells with just a couple of lines of C# code. Please note that ExcelDataReader does not support formatting features directly. You need to retrieve the number of the cell containing the format string and use the third-party ExcelNumberFormat library for formatting purposes. The following examples will help you to understand how to achieve it.
How to Apply Formatting to Spreadsheet Cells via C# API
string GetFormattedValue(IExcelDataReader reader, int columnIndex, CultureInfo culture)
{
var value = reader.GetValue(columnIndex);
var formatString = reader.GetNumberFormatString(columnIndex);
if (formatString != null)
{
var format = new NumberFormat(formatString);
return format.Format(value, culture);
}
return Convert.ToString(value, culture);
}