RE: Export GridView to Excel, Encoding problem
- From: stcheng@xxxxxxxxxxxxxxxxxxxx ("Steven Cheng")
- Date: Mon, 24 Mar 2008 02:30:01 GMT
Thanks for your followup Asaf,
I'm glad that you've resolved the problem. BTW, would you share some info
about how the problem got resolved, that'll also benifit other uses that
may run into the same problem.
Sincerely,
Steven Cheng
Microsoft MSDN Online Support Lead
Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
msdnmg@xxxxxxxxxxxxxx
This posting is provided "AS IS" with no warranties, and confers no rights.
--------------------
From: =?Utf-8?B?QXNhZg==?= <AG70@xxxxxxxxxxxxxxxxx><YC83RKwiIHA.5204@xxxxxxxxxxxxxxxxxxxxxx>
References: <B34E4076-3259-4D42-A799-9BFE4F72A590@xxxxxxxxxxxxx>
Subject: RE: Export GridView to Excel, Encoding problem
Date: Fri, 21 Mar 2008 06:42:01 -0700
document
Hello Steven,
Problem has been solved, thanks for your support.
Regards,
Asaf
""Steven Cheng"" wrote:
Hi Asaf,
From your description, you use ASP.NET web page to expose a Excel
itby GridView. However, you found that the output excel will contain
gibberish sometime when you specify the charset as windows-1255, correct?
Since windows-1255 is a single byte charset(ascii + extended chars...),
pageis probably that some certain characters in the excel(gridview) are not
within windows 1255's charset range and that cause the exported excel to
contain gibberish. Have you tried tried find the exact data that can
always repro the problem? You can try removing the problem data/text by
binary search isolation. Also, as you said that when you try save the
theas html, it will use "windows-1254", have you look at the "Encoding" of
andwebbrowser (via right click context menu) to see whether it is also auto
changed to 1254 for your page. If so, this means that the browser detect
your page to be matching windows-1254 charset.
Best regards,
Steven Cheng
Microsoft MSDN Online Support Lead
Delighting our customers is our #1 priority. We welcome your comments
Pleasesuggestions about how we can improve the support we provide to you.
rights.feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
msdnmg@xxxxxxxxxxxxxx
This posting is provided "AS IS" with no warranties, and confers no
and--------------------
From: =?Utf-8?B?QXNhZg==?= <AG70@xxxxxxxxxxxxxxxxx>
Subject: Export GridView to Excel, Encoding problem
Date: Thu, 20 Mar 2008 07:21:04 -0700
Hello,
When trying to export GridView to Excel using windows-1255 encoding for
Hebrew from ASP.NET version 2.0, sometimes the report is in Gibberish
thatsometimes it is ok.
When it is not ok and I am saving the xls file to HTML and I can see
"application/vnd.xls";it
is using windows-1254 encoding instead of windows-1255 encoding.culture="he-IL"
In Web.Config file I have set "globalization":
<globalization requestEncoding="windows-1255"
responseEncoding="windows-1255" fileEncoding="windows-1255"
uiCulture="he-IL"/>filename={0}",
And In my class for Response I have set:
HttpContext.Current.Response.ContentEncoding =
Encoding.GetEncoding("windows-1255");
HttpContext.Current.Response.Charset = "windows-1255";
Thanks in advanced for any help,
Asaf
Complete GridView export class is:
using System;
using System.Data;
using System.Configuration;
using System.IO;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
/// <summary>
///
/// </summary>
public class GridViewExportUtil
{
/// <summary>
///
/// </summary>
/// <param name="fileName"></param>
/// <param name="gv"></param>
public static void Export(string fileName, GridView gv, bool
UserGridLines)
{
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.ContentEncoding =
Encoding.GetEncoding("windows-1255");
HttpContext.Current.Response.Charset = "windows-1255";
HttpContext.Current.Response.AddHeader(
"content-disposition", string.Format("attachment;
fileName));
HttpContext.Current.Response.ContentType =
asGridViewExportUtil.PrepareControlForExport(gv.HeaderRow);
using (StringWriter sw = new StringWriter())
{
using (HtmlTextWriter htw = new HtmlTextWriter(sw))
{
// Create a form to contain the grid
Table table = new Table();
// add the header row to the table
if (gv.HeaderRow != null)
{
table.Rows.Add(gv.HeaderRow);GridViewExportUtil.PrepareControlForExport(gv.FooterRow);
}
// add each of the data rows to the table
foreach (GridViewRow row in gv.Rows)
{
GridViewExportUtil.PrepareControlForExport(row);
table.Rows.Add(row);
}
// add the footer row to the table
if (gv.FooterRow != null)
{
table.Rows.Add(gv.FooterRow);
}
// render the table into the htmlwriter
if (UserGridLines)
{
table.GridLines = GridLines.Both;
}
table.RenderControl(htw);
// render the htmlwriter into the response
HttpContext.Current.Response.Write(sw.ToString());
HttpContext.Current.Response.End();
}
}
}
/// <summary>
/// Replace any of the contained controls with literals
/// </summary>
/// <param name="control"></param>
private static void PrepareControlForExport(Control control)
{
for (int i = 0; i < control.Controls.Count; i++)
{
Control current = control.Controls[i];
if (current is LinkButton)
{
control.Controls.Remove(current);
control.Controls.AddAt(i, new LiteralControl((current
asLinkButton).Text));
}
else if (current is ImageButton)
{
control.Controls.Remove(current);
control.Controls.AddAt(i, new LiteralControl((current
asImageButton).AlternateText));
}
else if (current is HyperLink)
{
control.Controls.Remove(current);
control.Controls.AddAt(i, new LiteralControl((current
asHyperLink).Text));
}
else if (current is DropDownList)
{
control.Controls.Remove(current);
control.Controls.AddAt(i, new LiteralControl((current
asDropDownList).SelectedItem.Text));
}
else if (current is CheckBox)
{
control.Controls.Remove(current);
control.Controls.AddAt(i, new LiteralControl((current
CheckBox).Checked ? "Ã?â?ºÃ?Å? : "Ã?Å?Ã?Â?"));
}
if (current.HasControls())
{
GridViewExportUtil.PrepareControlForExport(current);
}
}
}
}
.
- Follow-Ups:
- References:
- Export GridView to Excel, Encoding problem
- From: Asaf
- RE: Export GridView to Excel, Encoding problem
- From: "Steven Cheng"
- RE: Export GridView to Excel, Encoding problem
- From: Asaf
- Export GridView to Excel, Encoding problem
- Prev by Date: RE: Forms authentication cookie handling question (C#)
- Next by Date: Re: GridView1_RowUpdated problem
- Previous by thread: RE: Export GridView to Excel, Encoding problem
- Next by thread: RE: Export GridView to Excel, Encoding problem
- Index(es):