protected void Page_Load(object sender, EventArgs e) { DataTable dt= ReadExcelToTable("E:\\31october\\Excel\\Files\\empdata.xlsx"); }private DataTable ReadExcelToTable(string path) { //Connection String string connstring = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + path + ";Extended Properties='Excel 8.0;HDR=NO;IMEX=1';"; //the same name //string connstring = Provider=Microsoft.JET.OLEDB.4.0;Data Source=" + path + //";Extended Properties='Excel 8.0;HDR=NO;IMEX=1';"; using(OleDbConnection conn = new OleDbConnection(connstring)) { conn.Open(); //Get All Sheets Name DataTable sheetsName = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables,new object[]{null,null,null,"Table"}); //Get the First Sheet Name string firstSheetName = sheetsName.Rows[0][2].ToString(); //Query String string sql = string.Format("SELECT * FROM [{0}]",firstSheetName); OleDbDataAdapter ada =new OleDbDataAdapter(sql,connstring); DataSet set = new DataSet(); ada.Fill(set); return set.Tables[0]; } }
Wednesday, August 14, 2013
How to read excel file data into datatable in c# to store in db
Subscribe to:
Post Comments (Atom)
ASP.NET Core
Certainly! Here are 10 advanced .NET Core interview questions covering various topics: 1. **ASP.NET Core Middleware Pipeline**: Explain the...
-
The error message you encountered ("DeleteService FAILED 1072: The specified service has been marked for deletion") indicates tha...
-
replace html of a div using jquery this is simple . just use .html() method of jquery to set new html for a div . $ ( "#divID&q...
-
declare @ProductIds nvarchar(50)='18,19' SELECT * FROM products Where (',' + @ProductIds +',' LIKE '%,' ...
Thanks for sharing this code. I have found another code to export excel data to datatable using C#/.NET with Aspose.Cells for .NET library. It also allows you to export/import data from/to different sources into excel file.
ReplyDelete