Read Excel Files Using Open XML SDK In ASP.NET C#
Introduction
In the Below simple 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:
Important note
**
**Once you load this dll and write the code, you will get the following error:
Resolution for above problem is,
**
http://csharpcorner.mindcrackerinc.netdna-cdn.com/article/read-excel-files-using-open-xml-sdk-in-asp-net-c-sharp/Images/image004.jpg
**
Add the above dll also to the project.
Namespace
- using DocumentFormat.OpenXml.Packaging;
- using DocumentFormat.OpenXml.Spreadsheet;
C# Code
- protected void insertBoqElements_Click(object sender, EventArgs e)
- {
- try
- {
- //specify the file name where its actually exist
- string filepath = @ "D:\TPMS\Uploaded_Boq\test.xlsx";
- //open the excel using openxml sdk
- using(SpreadsheetDocument doc = SpreadsheetDocument.Open(filepath, false))
- {
- //create the object for workbook part
- WorkbookPart wbPart = doc.WorkbookPart;
- //statement to get the count of the worksheet
- int worksheetcount = doc.WorkbookPart.Workbook.Sheets.Count();
- //statement to get the sheet object
- Sheet mysheet = (Sheet) doc.WorkbookPart.Workbook.Sheets.ChildElements.GetItem(0);
- //statement to get the worksheet object by using the sheet id
- Worksheet Worksheet = ((WorksheetPart) wbPart.GetPartById(mysheet.Id)).Worksheet;
- //Note: worksheet has 8 children and the first child[1] = sheetviewdimension,....child[4]=sheetdata
- int wkschildno = 4;
- //statement to get the sheetdata which contains the rows and cell in table
- SheetData Rows = (SheetData) Worksheet.ChildElements.GetItem(wkschildno);
- //getting the row as per the specified index of getitem method
- Row currentrow = (Row) Rows.ChildElements.GetItem(1);
- //getting the cell as per the specified index of getitem method
- Cell currentcell = (Cell) currentrow.ChildElements.GetItem(1);
- //statement to take the integer value
- string currentcellvalue = currentcell.InnerText;
- }
- } catch (Exception Ex)
- {
- lbldisplayerrors.Text = Ex.Message;
- }
- }
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
- protected void insertBoqElements_Click(object sender, EventArgs e)
- {
- try
- {
- //specify the file name where its actually exist
- string filepath = @ "D:\TPMS\Uploaded_Boq\test.xlsx";
- //open the excel using openxml sdk
- using(SpreadsheetDocument doc = SpreadsheetDocument.Open(filepath, false))
- {
- //create the object for workbook part
- WorkbookPart wbPart = doc.WorkbookPart;
- //statement to get the count of the worksheet
- int worksheetcount = doc.WorkbookPart.Workbook.Sheets.Count();
- //statement to get the sheet object
- Sheet mysheet = (Sheet) doc.WorkbookPart.Workbook.Sheets.ChildElements.GetItem(0);
- //statement to get the worksheet object by using the sheet id
- Worksheet Worksheet = ((WorksheetPart) wbPart.GetPartById(mysheet.Id)).Worksheet;
- //Note: worksheet has 8 children and the first child[1] = sheetviewdimension,....child[4]=sheetdata
- int wkschildno = 4;
- //statement to get the sheetdata which contains the rows and cell in table
- SheetData Rows = (SheetData) Worksheet.ChildElements.GetItem(wkschildno);
- //getting the row as per the specified index of getitem method
- Row currentrow = (Row) Rows.ChildElements.GetItem(0);
- //getting the cell as per the specified index of getitem method
- Cell currentcell = (Cell) currentrow.ChildElements.GetItem(0);
- string currentcellvalue = string.Empty;
- if (currentcell.DataType != null)
- {
- if (currentcell.DataType == CellValues.SharedString)
- {
- int id = -1;
- if (Int32.TryParse(currentcell.InnerText, out id))
- {
- SharedStringItem item = GetSharedStringItemById(wbPart, id);
- if (item.Text != null)
- {
- //code to take the string value
- currentcellvalue = item.Text.Text;
- } else if (item.InnerText != null)
- {
- currentcellvalue = item.InnerText;
- } else if (item.InnerXml != null)
- {
- currentcellvalue = item.InnerXml;
- }
- }
- }
- }
- }
- } catch (Exception Ex)
- {
- lbldisplayerrors.Text = Ex.Message;
- }
- }
- public static SharedStringItem GetSharedStringItemById(WorkbookPart workbookPart, int id)
- {
- return workbookPart.SharedStringTablePart.SharedStringTable.Elements < SharedStringItem > ().ElementAt(id);
- }
Other References
**
Link to:** Read excel files using Microsoft Office Interop Assemblies in asp.net
Downloads:
you can download the executable in from here : https://gallery.technet.microsoft.com/OpenXMLSimpleExample-a757f10c
hope the above information was useful. Kindly let me know your thoughts.