Hi
I am trying to export data from datagrid to excel sheet. My datagrid is having 5 columns and while exporting i would like to see only 3 columns in excel sheet. How can I do that? I appreciate if any one give an idea.
Also while transfering the data from datagrid to excel, I would like display header and footer to the transferred excelsheet.
Thank you
Prasad
Loading
PeroPosted Apr 14, 2010, 4:43 AM
Hi, you can easily convert DataGrid to Excel with GemBox.Spreadsheet Excel C# library.
Here is a code snippet how to do it in your ASP.NET Excel app:
// Get DataTable that DataGrid is bound to.
var dataTable = (DataTable)dataGrid.DataSource;
// Create new ExcelFile.
var ef = new ExcelFile();
// Add new worksheet to the file.
var ws = ef.Worksheets.Add(dataTable.TableName);
// Insert the data from DataTable to the worksheet starting at cell "A1".
ws.InsertDataTable(dataTable, "A1", true);
// Stream file to browser.
Response.Clear();
Response.ContentType = "application/vnd.ms-excel";
Response.AddHeader("Content-Disposition", "attachment; filename=Employee.xls");
ef.SaveXls(Response.OutputStream);
Response.End();
For live web demo see:
http://www.gemboxsoftware.com/WebDemo/Index.aspx
Prasad VempatiPosted Apr 5, 2010, 9:55 PM
Thanx for your reply. It was not the problem of exporting data to excel. It was not the problem of header either. Whatever the header the datagrid is having will be exported along with other data. Probably I should not say header in my question. After exporting into excel sheet, I would like to have the company name and address, phone and fax etc on top of the excel sheet. For example
THE COMPANY NAME
1111 ANYWHERE ST. CITY, STATE ZIPCODE
PH:(111)111-1111 FAX:(111)111-1111
---------------------------------------------------------------------------------------------------------------------------------------------------------
Here comes the data from datagrid
--------------------------------------------------------------------------------------------------------------------------------------------------------
Nikhil MahajanPosted Apr 2, 2010, 5:55 AM
Jitendra PatelPosted Apr 2, 2010, 5:25 AM
You have create HTML table in your code and your 3 column are bind in your html table.
and it will transfer into EXcel. and use above code to exporting HTML table to excel.
Thanks,
Jitendra Patel
Nikhil MahajanPosted Apr 2, 2010, 4:59 AM
code to export datagrid to excel
Code To Export TO Word
Exporting To text file