forum

Home / DeveloperSection / Forums / when i upload excel file dynamically display dropdowns in header of gridview columns.

when i upload excel file dynamically display dropdowns in header of gridview columns.

Munikumar G248719-Apr-2012

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


Updated on 20-Apr-2012
Good softwaresolution

Can you answer this question?


Answer

Liked By