473,407 Members | 2,629 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

Export Datagrid to Excel in C#

Ugo
Hi,
I am having a problem exporting a Datagrid to excel in asp.net. For
some reason I am getting a blank excel page. I set up in IIS the Mime for
..xls. My code is below any help would be appreciated..

Thanks

CODE:

private void ExportToExcel()
{
Response.Clear();
Response.ContentType = "application/vnd.ms-excel";
Response.ContentEncoding = System.Text.Encoding.Default;
Response.Charset = "";
this.EnableViewState = false;
StringWriter tw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);
DataGrid dg = new DataGrid();
dg.DataSource = (DataSet) Cache["dataset"];
dg.DataBind();
dg.RenderControl(hw);
Response.Write(tw.ToString());
Response.Flush() ;
Response.Close();
}


Nov 16 '05 #1
1 10895
Ugo,

The reason for this is that you are indicating to IE that an Excel sheet
is being sent, but you end up sending HTML. While Excel can handle HTML
files, the mime type you are sending indicates that it is not what it is
expecting.

If anything, I would open a temp Excel sheet on the server, modify the
content (through automation), save it, and then stream the contents of the
temp file back to the user.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Ugo" <ub*******@hartco.com> wrote in message
news:eW**************@TK2MSFTNGP11.phx.gbl...
Hi,
I am having a problem exporting a Datagrid to excel in asp.net. For some reason I am getting a blank excel page. I set up in IIS the Mime for
.xls. My code is below any help would be appreciated..

Thanks

CODE:

private void ExportToExcel()
{
Response.Clear();
Response.ContentType = "application/vnd.ms-excel";
Response.ContentEncoding = System.Text.Encoding.Default;
Response.Charset = "";
this.EnableViewState = false;
StringWriter tw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);
DataGrid dg = new DataGrid();
dg.DataSource = (DataSet) Cache["dataset"];
dg.DataBind();
dg.RenderControl(hw);
Response.Write(tw.ToString());
Response.Flush() ;
Response.Close();
}

Nov 16 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Eric | last post by:
Dear All, I want to export datagrid content to a Excel file, the code just like below: Response.ContentType = "application/vnd.ms-excel" Response.Charset = "" Me.EnableViewState = False Dim...
2
by: Bidarkota | last post by:
Hi, I have a DataGrid in which there are some images and Data are displayed and in the webform.asp page i am using some stylesheets. when i export the datagrid all the images are also exported...
3
by: Bidarkota | last post by:
When i export DataGrid to Excel all the HTML contents are also exporting to excel. i am using stylesheets in the ASPX Page and i am getting an alert message that stylesheets are missing. i need to...
0
by: sunilkumar Reddy via DotNetMonster.com | last post by:
hai all first we have taken binded grid Export to excel data from ultrawebgrid (or) datagrid rows, Each row data Expoting to one worksheet in excel workbook, like as grid row1 data contain...
0
by: kieran | last post by:
Hi, I have an 'export to excel' button on my Datagrid. All works well except that on my Datagrid, I have sub total rows. The subtotal rows use colspan so that the subtotals are given above...
6
by: Sridhar | last post by:
Hi, I have to export data inside the datagrid into an excel worksheet. I have to do this on button click event. I am trying to do like this. First get the DataTable from the datagrid and...
4
by: Frank | last post by:
Hello All, I ham using VS.NET 2003. Have followed instructions from http://gridviewguy.com/ArticleDetails.aspx?articleID=26 along with several other articles to no avail. I am pulling my hair...
7
by: mattmerc | last post by:
Hi all, We have all seen lot of method for exporting datagrids to Excel. I have a slightly different need. I think it should be easy to accomplish but I am not sure how. I would like when a user...
19
by: cj2 | last post by:
#1 Is there a quick way to export a datatable to an excel file? Or delimited file? #2 Where is the appropriate Microsoft monitored group to ask about writing reports in SQL Reporting services...
5
by: Reggie | last post by:
Hi and TIA! I have an export procedure that exports my data from my datagrids to excel. The problem is that I lose leading zero's cause excel removes them and doesn't treat them as text. Is it...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.