Carlos Oliveira Carlos Oliveira - 1 year ago 108
C# Question

Need help to read Excel file without installing Microsoft Access Database Engine 2010 Redistributable

I use the LINQ library to read data from excel file in my MVC Application, but it needs to be installed : Microsoft Access Database Engine 2010 Redistributable.

Is there any method to use LINQ without an installation of this Microsoft framework?

Answer Source

In case of Microsoft Excel 2007 or newer:

These files (.xlsx) are in the OpenXML format which is a zip archive inside containing XML-formated files. You can extract this zip archive and use LINQ to XML to query data - but note, it will be a really raw data.

Alternatively, you should use a dedicated library provided by Microsoft: Open XML SDK
You don't need to install anything (e.g. MS Office) - just reference the library to the MVC project.
Then you can query data in a more user-friendly way with LINQ syntax too.

For example:

var refIndex = 7;
var sheetData = worksheetPart.Worksheet.GetFirstChild<SheetData>();
var row = sheetData.Elements<Row>().FirstOrDefault(x => x.RowIndex >= rowIndex);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download