Read Excel Files Using Microsoft Office Interop Assemblies in ASP.NET C#
Read through the quick tutorial to learn how to read Excel files and store them in SQL Server with Office Interop Assemblies and a bit of C#.
Join the DZone community and get the full member experience.
Join For Freebackground
a few days ago, i was required to read excel files and store those values in our sql server database. so, in this example am going to show how to get four basic import data types: 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 below.
namespace
using excel = microsoft.office.interop.excel;
c# code
protected void btngetexcelfiledetails_click(object sender, eventargs e)
{
try
{
//create a instance for the excel object
excel.application oexcel = new excel.application();
//specify the file name where its actually exist
string filepath = @ "d:\tpms\uploaded_boq\raveena_boq_from_db.xlsx";
//pass that to workbook object
excel.workbook wb = oexcel.workbooks.open(filepath);
// statement get the workbookname
string excelworkbookname = wb.name;
// statement get the worksheet count
int worksheetcount = wb.worksheets.count;
excel.worksheet wks = (excel.worksheet) wb.worksheets[1];
// statement get the firstworksheetname
string firstworksheetname = wks.name;
//statement get the first cell value
var firstcellvalue = ((excel.range) wks.cells[1, 1]).value;
} catch (exception ex)
{
string error = ex.message;
}
}
i hope the above information was useful! please let me know your thoughts.
Published at DZone with permission of Karthik Elumalai. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments