Monday, June 20, 2011

Import Excel To Datagrid ASP.net

Working Effectively with Legacy Code
Learn how to find and create seams in your application, no matter "Legacy" it may be, and over time improve application's design. Read more...

private void btnImportExcelToGrid_Click(object sender,
System.EventArgs e)
{
String strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" +
"Data Source=C:\\Book2.xls;" +
"Extended Properties=Excel 8.0;";

DataSet ds = new DataSet();
//You must use the $ after the object
//you reference in the spreadsheet
OleDbDataAdapter da = new OleDbDataAdapter
("SELECT * FROM [Sheet1$]", strConn);

//da.TableMappings.Add("Table", "ExcelTest");

da.Fill(ds);
DataGrid2.DataSource = ds.Tables[0].DefaultView;
DataGrid2.DataBind();
}

Note: If you want to give client upload facility, than first upload the file to the server. Next call the excel file from the following server location.

Source: http://dotnetguts.blogspot.com

No comments:

Post a Comment