RE: Export GridView to Excel, Encoding problem



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>
References: <B34E4076-3259-4D42-A799-9BFE4F72A590@xxxxxxxxxxxxx>
<YC83RKwiIHA.5204@xxxxxxxxxxxxxxxxxxxxxx>
Subject: RE: Export GridView to Excel, Encoding problem
Date: Fri, 21 Mar 2008 06:42:01 -0700


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
document
by 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...),
it
is 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
page
as html, it will use "windows-1254", have you look at the "Encoding" of
the
webbrowser (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
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>
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
and
sometimes it is ok.

When it is not ok and I am saving the xls file to HTML and I can see
that
it
is using windows-1254 encoding instead of windows-1255 encoding.

In Web.Config file I have set "globalization":

<globalization requestEncoding="windows-1255"
responseEncoding="windows-1255" fileEncoding="windows-1255"
culture="he-IL"
uiCulture="he-IL"/>

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={0}",
fileName));

HttpContext.Current.Response.ContentType =
"application/vnd.xls";

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)
{

GridViewExportUtil.PrepareControlForExport(gv.HeaderRow);
table.Rows.Add(gv.HeaderRow);
}

// 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)
{

GridViewExportUtil.PrepareControlForExport(gv.FooterRow);
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
as
LinkButton).Text));
}
else if (current is ImageButton)
{
control.Controls.Remove(current);
control.Controls.AddAt(i, new LiteralControl((current
as
ImageButton).AlternateText));
}
else if (current is HyperLink)
{
control.Controls.Remove(current);
control.Controls.AddAt(i, new LiteralControl((current
as
HyperLink).Text));
}
else if (current is DropDownList)
{
control.Controls.Remove(current);
control.Controls.AddAt(i, new LiteralControl((current
as
DropDownList).SelectedItem.Text));
}
else if (current is CheckBox)
{
control.Controls.Remove(current);
control.Controls.AddAt(i, new LiteralControl((current
as
CheckBox).Checked ? "Ã?â?ºÃ?Å? : "Ã?Å?Ã?Â?"));
}

if (current.HasControls())
{
GridViewExportUtil.PrepareControlForExport(current);
}
}
}
}









.