How to upload Excel file to Database from ASP.NET
-
Dear friends, I want to upload an Excel file from ASP.NET web form to Sql Server Database. I have all employees salary in the excel file. I want to upload it to the database through ASP.NET web form. I am using ASP.NET 3.5 with C# and SQL Server 2005. Please, anyone can help me, How to upload excel file using the FileUpload control? I need it urgently. Please help me. Regards, Dileep.
-
Dear friends, I want to upload an Excel file from ASP.NET web form to Sql Server Database. I have all employees salary in the excel file. I want to upload it to the database through ASP.NET web form. I am using ASP.NET 3.5 with C# and SQL Server 2005. Please, anyone can help me, How to upload excel file using the FileUpload control? I need it urgently. Please help me. Regards, Dileep.
use this function
public static DataTable GetDataTableFromExcel(string SourceFilePath)
{
string ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" +
"Data Source=" + SourceFilePath + ";" +
"Extended Properties=Excel 8.0;";using (OleDbConnection cn = new OleDbConnection(ConnectionString)) { cn.Open(); DataTable dbSchema = cn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null); if (dbSchema == null || dbSchema.Rows.Count < 1) { throw new Exception("Error: Could not determine the name of the first worksheet."); } string WorkSheetName = dbSchema.Rows\[0\]\["TABLE\_NAME"\].ToString(); OleDbDataAdapter da = new OleDbDataAdapter("SELECT \* FROM \[" + WorkSheetName + "\]", cn); DataTable dt = new DataTable(WorkSheetName); da.Fill(dt); return dt; }
}
This will return you datatable after that with the help of loop insert it into database
Gaurav Dudeja http://www.gdinfotechindia.com
Dont be afraid of changing your life to better !