开发者

Problem with encoding in export to Excel from C# WebPart

I have simple C# WebPart (written for WSS 3.0). Ones of functionality is exporting data to excel. Everything is working but I have problem with displaying polish letters. I try to change almost everything, read a lot of forums and blogs and still don't know what I'm doing wrong. Hear is my code:

HttpContext.Current.Response.Clear();            
HttpContext.Current.Response.AddHeader("content-disposition", string.Format("attachment; filename={0}.xls", fileName));            
HttpContext.Current.Response.ContentType = "application/ms-excel";
HttpContext.Current.Response.ContentEncoding = 
    System.Text.Encoding.GetEncoding(1250); // this is correct encoding code

using (StringWriter sw = new StringWriter开发者_JAVA技巧())
{                
    using (HtmlTextWriter htw = new HtmlTextWriter(sw))
    {                 
        Table table = new Table();
        TableRow row = new TableRow();

        // (...) fills header row

        table.Rows.Add(row);

        foreach (User user in userLists)
        {
            TableRow row1 = new TableRow();

            // (...)  fills row with data

            table.Rows.Add(row1);
        }

        table.RenderControl(htw);

        HttpContext.Current.Response.Write(sw.ToString());
        HttpContext.Current.Response.End();
    }
}

I used similar method for exporting data to CSV file and there is no problem with displaying polish signs. Have anybody know what I'm doing wrong??


Sometimes when I have issues like this when importing data, it helps to reset the data formats for the parts that are screwed up. You might want to try resetting the language for the doc just to make sure the issue isn't your code but Excel defaulting to English for some obscure reason. Good luck.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜