Excel下载中文打印不需要的字符?

时间:2011-01-17 01:39:31

标签: c# .net asp.net vb.net excel

我正在尝试将数据库中的中文数据下载到excel。但是像Excel这样的数据在Excel中会出现不同的特征

  

œèŽžå¸,诚通计ç®-机技术å'¨è¯¢æœåŠ¡æœ‰é™å...¬A

这是我的下载excel代码。我不明白我的代码中有什么问题。

 dg.AllowPaging = False
        dg.AllowSorting = False
        dg.AllowCustomPaging = False
        dg.AutoGenerateColumns = True

        dg.HeaderStyle.Font.Bold = True
        dg.HeaderStyle.Font.Underline = True

        dg.DataSource = sqlDs.Tables(0)
        dg.DataBind()
        dg.RenderControl(htmlWrite)

        Response.Clear()
        Response.ClearContent()
        Response.AddHeader("content-disposition", "attachment;filename=FullExtract_" & Now.Year.ToString & Now.Month.ToString & Now.Day.ToString & ".xls")
        Response.ContentType = "application/octet-stream"
        'Response.ContentType = "application/excel"
        Response.BinaryWrite(System.Text.UnicodeEncoding.UTF8.GetBytes(stringWrite.ToString()))
        Response.End()

1 个答案:

答案 0 :(得分:1)

没有尝试过中文,但我的土耳其语数据也有类似问题。下面的代码对我来说效果很好。您可以找到详细信息here。希望这会有所帮助。

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

System.IO.StringWriter sw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter hw = new HtmlTextWriter(sw);

FormView1.RenderControl(hw);

Response.Write(sw.ToString());
Response.End();