Exporting GrifView to Excel?


A

Asaf

Hi below is the code I am using to export GridView to Excel in Hebrew
character set.
Problem is that sometimes text in Hebrew is ok in the Excel and sometimes it
display as gibberish in the Excel.

How can I solve this problem please?

Kind Regards,
Asaf


protected void btnExportToExcel_Click(object sender, EventArgs e)
{
grvReports.AllowPaging = false;
grvReports.AllowSorting = false;
grvReports.DataBind();

Response.Clear();

Response.AddHeader("content-disposition", "attachment;filename=" +
"MyExcelFile.xls");

Response.ContentType = "application/vnd.xls";
Response.Charset = "1255";

System.IO.StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite = new
HtmlTextWriter(stringWrite);

grvReports.RenderControl(htmlWrite);
Response.Write(stringWrite.ToString());

Response.End();
}
public override void VerifyRenderingInServerForm(Control control)
{
//Must set this override function because a bug with the Render
//If not then "btnExportToExcel_Click" will not work!
}
 
Ad

Advertisements

J

Jialiang Ge [MSFT]

Hello,

From your post, my understanding on this issue is: you wonder how to set
the correct charset when a GridView is exported to Excel. If I'm off base,
please feel free to let me know.

The correct charset is determined by the characters to be exported. A
general resolution is to:
1. Type or copy the characters directly into an Excel workbook, and ensure
they are shown correctly in it.
2. Save the workbook as html format (File->SaveAs)
3. Open the html with notepad, and find the charset attribute in its head.

Then, we can use this charset to export the GridView.

If this resolution does not help, would you send me a sample
project/workbook with the characters that cannot be shown correctly? My
mailbox is: (e-mail address removed), remove 'online.'. I am looking
forward to your reply.

Regards,
Jialiang Ge ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
For MSDN subscribers whose posts are left unanswered, please check this
document: http://blogs.msdn.com/msdnts/pages/postingAlias.aspx

Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications. If you are using Outlook Express/Windows Mail, please make sure
you clear the check box "Tools/Options/Read: Get 300 headers at a time" to
see your reply promptly.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top