Read Excel Files Using Open XML SDK In ASP.NET C#

Background

A few days back, I got a requirement to read MS Excel files and store those values in the SQL Server database. Hence, in this example, I am going to show how to get the basic four types of important data such as Excel Work Book Name, Worksheet Count in that Workbook, Name of the Worksheets, and finally the Value of the First Cell in that Worksheet.

Prerequisites

First step is to download the Openxml dll from the official site.

Second step is to kindly ensure to add the above dll, as shown in the screenshot, given below:

add

add

Important note

Once you load this dll and write the code, you will get the following error:

error

Resolution for above problem is,

dll

Add the above dll also to the project.

Namespace

  1. using DocumentFormat.OpenXml.Packaging;
  2. using DocumentFormat.OpenXml.Spreadsheet;

C# Code


  1.  

Note

If the cell contains a string, then this value is an index into the shared string table, pointing to the actual string value. Otherwise, the value of the cell is expressed directly in this element

Source: CellValue Class

For taking the string Value from Cell


  1.  

  2.  

Other References

Link to: Read excel files using Microsoft Office Interop Assemblies in asp.net

I hope the above information was useful. Kindly let me know your thoughts.

951 total views, 2 views today

Leave a Reply

Your email address will not be published. Required fields are marked *