Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / General / March 2008

Tip: Looking for answers? Try searching our database.

Export GridView to Excel, Encoding problem

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Asaf - 20 Mar 2008 15:21 GMT
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);
           }
       }
   }
}
Steven Cheng - 21 Mar 2008 04:20 GMT
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@microsoft.com.

This posting is provided "AS IS" with no warranties, and confers no rights.
--------------------
>From: =?Utf-8?B?QXNhZg==?= <AG70@newsgroups.nospam>
>Subject: Export GridView to Excel, Encoding problem
>Date: Thu, 20 Mar 2008 07:21:04 -0700

>Hello,
>
[quoted text clipped - 85 lines]
>                {
>                    
GridViewExportUtil.PrepareControlForExport(gv.FooterRow);
>                    table.Rows.Add(gv.FooterRow);
>                }
[quoted text clipped - 61 lines]
>    }
>}
Asaf - 21 Mar 2008 08:19 GMT
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

> Hi Asaf,
>
[quoted text clipped - 189 lines]
> >    }
> >}
Asaf - 21 Mar 2008 14:42 GMT
Hello Steven,

Problem has been solved, thanks for your support.

Regards,

Asaf

> Hi Asaf,
>
[quoted text clipped - 189 lines]
> >    }
> >}
Steven Cheng - 24 Mar 2008 03:30 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@microsoft.com.

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

--------------------
>From: =?Utf-8?B?QXNhZg==?= <AG70@newsgroups.nospam>
>References:  <B34E4076-3259-4D42-A799-9BFE4F72A590@microsoft.com>
<YC83RKwiIHA.5204@TK2MSFTNGHUB02.phx.gbl>
>Subject: RE: Export GridView to Excel, Encoding problem
>Date: Fri, 21 Mar 2008 06:42:01 -0700

>Hello Steven,
>
[quoted text clipped - 197 lines]
>> >    }
>> >}
Asaf - 24 Mar 2008 08:16 GMT
Hi Steven,

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

Asaf

> Thanks for your followup Asaf,
>
[quoted text clipped - 239 lines]
> >> >    }
> >> >}

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.