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. protected void insertBoqElements_Click(object sender, EventArgs e)
  2. {
  3.     try
  4.     {
  5.         //specify the file name where its actually exist 
  6.         string filepath = @ “D:\TPMS\Uploaded_Boq\test.xlsx”;
  7.         //open the excel using openxml sdk
  8.         using(SpreadsheetDocument doc = SpreadsheetDocument.Open(filepath, false))
  9.         {
  10.             //create the object for workbook part
  11.             WorkbookPart wbPart = doc.WorkbookPart;
  12.             //statement to get the count of the worksheet
  13.             int worksheetcount = doc.WorkbookPart.Workbook.Sheets.Count();
  14.             //statement to get the sheet object
  15.             Sheet mysheet = (Sheet) doc.WorkbookPart.Workbook.Sheets.ChildElements.GetItem(0);
  16.             //statement to get the worksheet object by using the sheet id
  17.             Worksheet Worksheet = ((WorksheetPart) wbPart.GetPartById(mysheet.Id)).Worksheet;
  18.             //Note: worksheet has 8 children and the first child[1] = sheetviewdimension,….child[4]=sheetdata
  19.             int wkschildno = 4;
  20.             //statement to get the sheetdata which contains the rows and cell in table
  21.             SheetData Rows = (SheetData) Worksheet.ChildElements.GetItem(wkschildno);
  22.             //getting the row as per the specified index of getitem method
  23.             Row currentrow = (Row) Rows.ChildElements.GetItem(1);
  24.             //getting the cell as per the specified index of getitem method
  25.             Cell currentcell = (Cell) currentrow.ChildElements.GetItem(1);
  26.             //statement to take the integer value
  27.             string currentcellvalue = currentcell.InnerText;
  28.         }
  29.     } catch (Exception Ex)
  30.     {
  31.         lbldisplayerrors.Text = Ex.Message;
  32.     }
  33. }

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. protected void insertBoqElements_Click(object sender, EventArgs e)
  2. {
  3.     try
  4.     {
  5.         //specify the file name where its actually exist 
  6.         string filepath = @ “D:\TPMS\Uploaded_Boq\test.xlsx”;
  7.         //open the excel using openxml sdk
  8.         using(SpreadsheetDocument doc = SpreadsheetDocument.Open(filepath, false))
  9.         {
  10.             //create the object for workbook part
  11.             WorkbookPart wbPart = doc.WorkbookPart;
  12.             //statement to get the count of the worksheet
  13.             int worksheetcount = doc.WorkbookPart.Workbook.Sheets.Count();
  14.             //statement to get the sheet object
  15.             Sheet mysheet = (Sheet) doc.WorkbookPart.Workbook.Sheets.ChildElements.GetItem(0);
  16.             //statement to get the worksheet object by using the sheet id
  17.             Worksheet Worksheet = ((WorksheetPart) wbPart.GetPartById(mysheet.Id)).Worksheet;
  18.             //Note: worksheet has 8 children and the first child[1] = sheetviewdimension,….child[4]=sheetdata
  19.             int wkschildno = 4;
  20.             //statement to get the sheetdata which contains the rows and cell in table
  21.             SheetData Rows = (SheetData) Worksheet.ChildElements.GetItem(wkschildno);
  22.             //getting the row as per the specified index of getitem method
  23.             Row currentrow = (Row) Rows.ChildElements.GetItem(0);
  24.             //getting the cell as per the specified index of getitem method
  25.             Cell currentcell = (Cell) currentrow.ChildElements.GetItem(0);
  26.             string currentcellvalue = string.Empty;
  27.             if (currentcell.DataType != null)
  28.             {
  29.                 if (currentcell.DataType == CellValues.SharedString)
  30.                 {
  31.                     int id = -1;
  32.                     if (Int32.TryParse(currentcell.InnerText, out id))
  33.                     {
  34.                         SharedStringItem item = GetSharedStringItemById(wbPart, id);
  35.                         if (item.Text != null)
  36.                         {
  37.                             //code to take the string value
  38.                             currentcellvalue = item.Text.Text;
  39.                         } else if (item.InnerText != null)
  40.                         {
  41.                             currentcellvalue = item.InnerText;
  42.                         } else if (item.InnerXml != null)
  43.                         {
  44.                             currentcellvalue = item.InnerXml;
  45.                         }
  46.                     }
  47.                 }
  48.             }
  49.         }
  50.     } catch (Exception Ex)
  51.     {
  52.         lbldisplayerrors.Text = Ex.Message;
  53.     }
  54. }
  55. public static SharedStringItem GetSharedStringItemById(WorkbookPart workbookPart, int id)
  56. {
  57.     return workbookPart.SharedStringTablePart.SharedStringTable.Elements < SharedStringItem > ().ElementAt(id);
  58. }

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.

Leave a Reply

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