Hi,
when i upload excel file, dynamically display dropdowns in header of gridview columns.
i am using this code
//Check file is available in File upload Control
if (FileUpload1.HasFile)
{
//Store file name in the string variable
string filename = FileUpload1.FileName;
//Save file upload file in to server path for temporary
FileUpload1.SaveAs(Server.MapPath(filename));
//Export excel data into Gridview using below method
ExportToGrid(Server.MapPath(filename));
}
}
void ExportToGrid(String path)
{
OleDbConnection MyConnection = null;
DataSet DtSet = null;
OleDbDataAdapter MyCommand = null;
//Connection for MS Excel 2003 .xls format
MyConnection =
new OleDbConnection("provider=Microsoft.Jet.OLEDB.4.0; Data Source='" + path + "';Extended Properties=Excel 8.0;");
//Connection for .xslx 2007 format
MyConnection =
new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source='" + path + "';Extended Properties=Excel 12.0;");
//Connection for .xslx 2010 format
//MyConnection = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.14.0;Data Source='" + path + "';Extended Properties=Excel 14.0;");
//Select your Excel file
MyCommand =
new System.Data.OleDb.OleDbDataAdapter("select * from [Sheet1$]", MyConnection);
DtSet =
new System.Data.DataSet();
//Bind all excel data in to data set
MyCommand.Fill(DtSet,
"[Sheet1$]");
dt = DtSet.Tables[0];
MyConnection.Close();
//Check datatable have records
if (dt.Rows.Count > 0)
{
GridView1.DataSource = dt;
GridView1.DataBind();
}
//Delete temporary Excel file from the Server path
if (System.IO.File.Exists(path))
{
System.IO.
File.Delete(path);
}
use munlti headers also......... sort out ASAP.
Thanks
Munikumar
Hi Munikumar,
Please! Explain your question clearly; what are you trying to ask? Check out following helpful link...
https://www.mindstick.com/blog/294/how-to-import-or-export-sql-server-table-data-in-ms-excel-sheet-using-c-sharp-code
https://www.mindstick.com/forum/373/how-to-import-data-from-excel-to-datatable-in-c-sharp
I hope it might be resolve your problem.