views:

484

answers:

3

I am currently using Visual Studio 2008 for my ASP .NET application. I am trying to Export some reports with Japanese Characters to Excel via the Response object. When I try to Export, all the Japanese characters looks garbled. It works fine with Chinese Characters. Here is what I tried:

I tried Installed Japanese Language Pack / Encoding to UTF-8 / UTF-7 / Shift-JIS / Globalization (Web.Config) .. but no luck. Any Ideas how this can be fixed ? Thanks !!

        string attachment = "attachment; filename=PerksPlusReport.xls";
        //Response.Clear();
        Response.ClearContent();
        Response.ClearHeaders();
 Response.AddHeader("content-disposition", attachment);
        //Response.Charset = "UTF-8";
 //Response.Charset = "UTF-7";
 //Response.Charset = "Shift_JIS";
        Response.ContentType = "application/vnd.ms-excel";
        StringWriter sw = new StringWriter();
        HtmlTextWriter htw = new HtmlTextWriter(sw);

        // Create a form to contain the grid
        HtmlForm frm = new HtmlForm();
        ReportGridView.Parent.Controls.Add(frm);
        frm.Attributes["runat"] = "server";
        GridView GridView2 = new GridView();

        ReportView reportDetails = GetReportDetails();
        GridView2.DataSource = GetReportResults(this.ReportId.Value, reportDetails.Sql);

        GridView2.DataBind();
        PrepareGridViewForExport(GridView2);
        frm.Controls.Add(GridView2);
        frm.RenderControl(htw);

        string fileContents = sw.ToString();
        int startSpot = fileContents.IndexOf("<table");
        fileContents = fileContents.Substring(startSpot);
        int endSpot = fileContents.IndexOf("</table>");
        fileContents = fileContents.Substring(0, endSpot + 8);

        try
        {
            // Replace all &lt; and &gt; with < and >
            fileContents = fileContents.Replace("&lt;", "<");
            fileContents = fileContents.Replace("&gt;", ">");
            fileContents = fileContents.Replace("€", "&euro;");

            string RegularExpression = @"<a[^>]*>([^<]*)</a>";
            Regex regex = new Regex(RegularExpression);

            //If match found .. uses the delegate function to replace the whole content with the filtered values 
            if (regex.IsMatch(fileContents))
            {
                regex.Replace(fileContents, delegate (Match m){return fileContents.Replace(m.Captures[0].Value, m.Groups[1].Value);});
            }
        }
        catch (Exception ex2)
        {
            Response.Write(ex2.ToString());
        }

 Response.Write(fileContents);
        Response.End();
A: 

Have you tried setting the ContentEncoding property of the Response or explicitly setting the content-encoding HTTP header ?

Timores
A: 

This is how I got it working ..

Response.Clear();
Response.ClearContent();
Response.ClearHeaders();
Response.AddHeader("content-disposition", "attachment; filename=FileName.xls");
Response.ContentEncoding = System.Text.Encoding.Unicode;
Response.ContentType = "application/ms-excel";
Response.BinaryWrite(System.Text.Encoding.Unicode.GetPreamble());
Kalyan
A: 

hi kalyan.. thank you very much.. its working for me.

Micela