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

Total Post:9

Points:63
Posted on    April-19-2012 5:51 AM

 C# C# 
Ratings:


 1 Reply(s)
 1479  View(s)
Rate this:

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



Arun Singh

Total Post:68

Points:476
Posted on    April-20-2012 3:15 AM


Don't want to miss updates? Please click the below button!

Follow MindStick