Export GridView to Excel, Encoding problem

A

Asaf

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;
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);
}
}
}
}
 
S

Steven Cheng

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:
(e-mail address removed).

This posting is provided "AS IS" with no warranties, and confers no rights.
--------------------
From: =?Utf-8?B?QXNhZg==?= <[email protected]>
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;
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);
}
}
}
}
 
A

Asaf

Hello Steven,

Problem is that I have tried to produce the same data for my Excel export
report and sometimes it generates 1255 and sometimes 1254 for the same data.

On the Windows 2003 server side if I run my ASP.NET website locally the
Excel file is generated correct any time for all data.

I have tried several client computers and problem rose at all of them.

I have also tried to set Encoding to 65001 but no luck.

Is there a way I can check exactly from where the problem rises as I really
don't know where to start from?

Thanks,
Asaf

"Steven Cheng" said:
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:
(e-mail address removed).

This posting is provided "AS IS" with no warranties, and confers no rights.
--------------------
From: =?Utf-8?B?QXNhZg==?= <[email protected]>
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;
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);
}
}
}
}

 
A

Asaf

Hello Steven,

Problem has been solved, thanks for your support.

Regards,

Asaf


"Steven Cheng" said:
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:
(e-mail address removed).

This posting is provided "AS IS" with no warranties, and confers no rights.
--------------------
From: =?Utf-8?B?QXNhZg==?= <[email protected]>
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;
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);
}
}
}
}

 
S

Steven Cheng

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:
(e-mail address removed).

This posting is provided "AS IS" with no warranties, and confers no rights.

--------------------
From: =?Utf-8?B?QXNhZg==?= <[email protected]>
References: <[email protected]>
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" said:
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:
(e-mail address removed).

This posting is provided "AS IS" with no warranties, and confers no rights.
--------------------
From: =?Utf-8?B?QXNhZg==?= <[email protected]>
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;
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);
}
}
}
}


 
A

Asaf

Hi Steven,

It was just an adjustment to set Hebrew language to my server.

Asaf


"Steven Cheng" said:
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:
(e-mail address removed).

This posting is provided "AS IS" with no warranties, and confers no rights.

--------------------
From: =?Utf-8?B?QXNhZg==?= <[email protected]>
References: <[email protected]>
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" said:
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:
(e-mail address removed).

This posting is provided "AS IS" with no warranties, and confers no rights.
--------------------
From: =?Utf-8?B?QXNhZg==?= <[email protected]>
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;
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);
}
}
}
}


 

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

Output a formula 0
export to excel 1
export to excel 0
export to excel 0
exporting gridview to excel issue 7
export to excel 1
Adding Text to ListView Excel Export 0
gridview to excel 6

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,190
Members
46,736
Latest member
zacharyharris

Latest Threads

Top