通过浏览器本地化导出excel - ASP.Net

时间:2013-02-18 10:45:41

标签: asp.net gridview localization export-to-excel

我有gridview,我正在导出以使用以下代码优化gridview。

Response.ClearContent()
Response.AddHeader("content-disposition", "attachment; 
                       filename=ExcelShipments_" & DateTime.Now.Ticks & ".xls")
Response.ContentType = "application/excel"
Dim sWriter As New StringWriter()
Dim hTextWriter As New HtmlTextWriter(sWriter)
Dim hForm As New HtmlForm()
ucShipmentList.ShipmentGrid.Parent.Controls.Add(hForm)
hForm.Attributes("runat") = "server"
hForm.Controls.Add(ucShipmentList.ShipmentGrid)

hForm.RenderControl(hTextWriter)

Dim sBuilder As New StringBuilder()
sBuilder.Append("<html xmlns:v=""urn:schemas-microsoft-com:vml"" xmlns:o=""urn:schemas-microsoft-com:office:office"" xmlns:x=""urn:schemas-microsoft-com:office:excel"" xmlns=""http://www.w3.org/TR/REC-html40""> <head><meta http-equiv=""Content-Type"" content=""text/html;charset=windows-1252""><!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet><x:Name>ExportToExcel</x:Name><x:WorksheetOptions><x:Panes></x:Panes></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]--></head> <body>")
                sBuilder.Append(sWriter.ToString() & "</body></html>")

Response.Write(sBuilder.ToString())
Response.End()

我在页面默认设置中有荷兰语,因此页面上的网格显示是正确的,但在导出到Excel时,它显示为英文导出。
如何在导出到excel时保留语言?

1 个答案:

答案 0 :(得分:0)

我会使用Open XML SDK并创建一个“真正的”Excel文档。