Background
A few days ago I got a requirement to read Excel files and store those values in the Sql server database. So in this example am going to show how to get the basic four import datas such as Excel Work Book Name, Worksheet Count in that Workbook, Name of the First Worksheet, and finally the value of the first cell in that worksheet.
Prerequisites
Kindly ensure you add the following dll as shown in the below screenshot,
Namespace
using Excel = Microsoft.Office.Interop.Excel;
C# Code
- protected void BtnGetExcelFileDetails_Click(object sender, EventArgs e)
- {
-
- try
- {
-
- Excel.Application oExcel = new Excel.Application();
-
-
- string filepath = @ "D:\TPMS\Uploaded_Boq\Raveena_boq_From_Db.xlsx";
-
-
- Excel.Workbook WB = oExcel.Workbooks.Open(filepath);
-
-
-
- string ExcelWorkbookname = WB.Name;
-
-
- int worksheetcount = WB.Worksheets.Count;
-
- Excel.Worksheet wks = (Excel.Worksheet) WB.Worksheets[1];
-
-
-
- string firstworksheetname = wks.Name;
-
-
- var firstcellvalue = ((Excel.Range) wks.Cells[1, 1]).Value;
-
- } catch (Exception ex)
- {
-
- string error = ex.Message;
- }
-
- }
I hope the above information was useful, kindly let me know your thoughts.