Saturday, October 14, 2006

Import Excel To Datagrid ASP.net

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. Reference: http://www.dotnetspider.com/kb/Article361.aspx

4 comments:

Unknown said...

Hi,

Here you can find the fast and easy way to import or export DataGrid to XLS, XLSX, HTML or CSV files by using GemBox.Spreadsheet .NET component:
http://www.gemboxsoftware.com/LDataGrid.htm

Here you can find free version of the component that you can use even in commercial applications:
http://www.gemboxsoftware.com/GBSpreadsheetFree.htm

Mario

Kedar said...

I saw the component and have limitation as below

Maximum number of rows per sheet is 150.
Maximum number of sheets per workbook is 5.

Sudha said...

Hi,

I want to import the excel which has 6 tabs.i want to import the data into 6 different gridviews. Each tab contains more than 1000 records. without affecting the performance how to achieve this?

Please help me.

Thanks,
Ambu

Prateek Shukla said...

Thanks
I've been searching such a easy code for a long time

Most Recent Post

Subscribe Blog via Email

Enter your email address:



Disclaimers:We have tried hard to provide accurate information, as a user, you agree that you bear sole responsibility for your own decisions to use any programs, documents, source code, tips, articles or any other information provided on this Blog.
Page copy protected against web site content infringement by Copyscape