我想将som数据导出到 xls ,但似乎有UTF-8编码问题。 它输出正常,我得到Excel文件,但是当插入的数据有特殊字符时,在这种情况下,丹麦字符为æ,ø和å,然后文件有点破坏。当我尝试在Excel中打开它时,我收到一条消息,说明在阅读文件时出现问题,并将问题提到设置spreadseet 。
我在几个通过验证的在线验证服务中运行了对结果XML的验证。当我在Notepad ++ XML验证器中运行它时,我在først找到的特殊字符(Å)中出现错误,其中显示:第XX行的XML解析错误:输入不正确UTF -8,表示编码!
有人可以指出这是什么吗? 我应该在哪里指明编码?
以下是生成的XML的相关部分:
<?xml version="1.0"?>
<?mso-application progid="Excel.Sheet"?>
<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" xmlns:html="http://www.w3.org/TR/REC-html40">
<DocumentProperties xmlns="urn:schemas-microsoft-com:office:office">
<Created>2006-09-25T13:34:53Z</Created>
<LastSaved>2014-05-20T18:04:40Z</LastSaved>
<Version>12.00</Version>
</DocumentProperties>
<OfficeDocumentSettings xmlns="urn:schemas-microsoft-com:office:office">
<RemovePersonalInformation/>
</OfficeDocumentSettings>
<ExcelWorkbook xmlns="urn:schemas-microsoft-com:office:excel">
<WindowHeight>7875</WindowHeight>
<WindowWidth>14895</WindowWidth>
<WindowTopX>360</WindowTopX>
<WindowTopY>300</WindowTopY>
<ProtectStructure>False</ProtectStructure>
<ProtectWindows>False</ProtectWindows>
</ExcelWorkbook>
<Styles>
...
</Styles>
<Worksheet ss:Name="Ark1">
<Names>
<NamedRange ss:Name="GIS34_Skade_V1_alle_koder" ss:RefersTo="=Ark1!R5C2:R28C2"/>
</Names>
<Table ss:ExpandedColumnCount="16" ss:ExpandedRowCount="34" x:FullColumns="1" x:FullRows="1" ss:StyleID="s16" ss:DefaultRowHeight="15">
<Column ss:StyleID="s16" ss:AutoFitWidth="0" ss:Width="30"/>
<Column ss:StyleID="s16" ss:AutoFitWidth="0" ss:Width="135" ss:Span="2"/>
<Column ss:Index="5" ss:StyleID="s19" ss:AutoFitWidth="0" ss:Width="30"/>
<Column ss:StyleID="s16" ss:AutoFitWidth="0" ss:Width="135"/>
<Column ss:StyleID="s19" ss:AutoFitWidth="0" ss:Width="30"/>
<Column ss:StyleID="s16" ss:AutoFitWidth="0" ss:Width="135"/>
<Column ss:StyleID="s19" ss:AutoFitWidth="0" ss:Span="3"/>
<Row ss:AutoFitHeight="0" ss:Height="30" ss:StyleID="s18">
<Cell ss:StyleID="s33"/>
<Cell ss:StyleID="s126"><Data ss:Type="String">LE34</Data></Cell>
<Cell ss:StyleID="s87"><Data ss:Type="String">Kunde:</Data></Cell>
<Cell ss:StyleID="s32"><Data ss:Type="String">Assens Kommune</Data></Cell>
</Row>
<Row ss:AutoFitHeight="0" ss:Height="30" ss:StyleID="s18">
<Cell ss:StyleID="s33"/>
<Cell ss:StyleID="s128"><Data ss:Type="String">GIS34</Data></Cell>
<Cell ss:StyleID="s87"><Data ss:Type="String">Kodeliste:</Data></Cell>
<Cell ss:StyleID="s32"><Data ss:Type="String">Arkil Århus V2</Data></Cell> <!-- Error occures in this line first because of the **Å** char -->
</Row>
<Row ss:AutoFitHeight="0">
<Cell ss:StyleID="s33"/>
<Cell ss:MergeAcross="6" ss:StyleID="s37"><Data ss:Type="String">Udformning af lagkontrol: </Data></Cell>
<Cell ss:StyleID="s16"/>
<Cell ss:StyleID="s16"/>
<Cell ss:StyleID="s16"/>
<Cell ss:StyleID="s16"/>
</Row>
...
<Row ss:AutoFitHeight="0">
<Cell ss:Index="2" ss:StyleID="s29"><NamedCell ss:Name="GIS34_Skade_V1_alle_koder"/></Cell>
<Cell ss:StyleID="s30"><Data ss:Type="String">Billede 3 (filename)</Data></Cell>
<Cell ss:StyleID="s30"/>
<Cell ss:StyleID="s16"/>
<Cell ss:StyleID="s20"/>
<Cell ss:StyleID="s16"/>
<Cell ss:StyleID="s20"/>
<Cell ss:StyleID="s16"/>
<Cell ss:StyleID="s16"/>
<Cell ss:StyleID="s16"/>
<Cell ss:StyleID="s16"/>
</Row>
</Table>
<WorksheetOptions xmlns="urn:schemas-microsoft-com:office:excel">
<PageSetup>
<Header x:Margin="0.31496062992125984"/>
<Footer x:Margin="0.31496062992125984"/>
<PageMargins x:Bottom="0.74803149606299213" x:Left="0.70866141732283472" x:Right="0.70866141732283472" x:Top="0.74803149606299213"/>
</PageSetup>
<Print>
<ValidPrinterInfo/>
<PaperSizeIndex>9</PaperSizeIndex>
<HorizontalResolution>600</HorizontalResolution>
<VerticalResolution>600</VerticalResolution>
<Gridlines/>
</Print>
<Selected/>
<FreezePanes/>
<FrozenNoSplit/>
<SplitHorizontal>7</SplitHorizontal>
<TopRowBottomPane>7</TopRowBottomPane>
<ActivePane>2</ActivePane>
<Panes>
<Pane>
<Number>3</Number>
</Pane>
<Pane>
<Number>2</Number>
<ActiveRow>26</ActiveRow>
<ActiveCol>13</ActiveCol>
</Pane>
</Panes>
<ProtectObjects>False</ProtectObjects>
<ProtectScenarios>False</ProtectScenarios>
</WorksheetOptions>
<QueryTable xmlns="urn:schemas-microsoft-com:office:excel">
<Name>GIS34 Skade V1_alle koder</Name>
<AutoFormatFont/>
<AutoFormatPattern/>
<QuerySource>
<QueryType>Text</QueryType>
<TextWizardSettings>
<Name x:HRef="C:\Temp\dummy.txt"/>
<Decimal>,</Decimal>
<ThousandSeparator>.</ThousandSeparator>
<TrailingMinusNumbers/>
<FormatSettings>
<FieldType>AutoFormat</FieldType>
</FormatSettings>
<Delimiters>
<Tab/>
</Delimiters>
</TextWizardSettings>
<VersionLastEdit>3</VersionLastEdit>
<VersionLastRefresh>3</VersionLastRefresh>
</QuerySource>
</QueryTable>
</Worksheet>
</Workbook>
以下是我的导出方式:
context.Response.ContentType = "application/excel";
context.Response.AddHeader("Content-disposition", "attachment; filename=\"report.xls\"");
context.Response.ContentEncoding = new System.Text.UTF8Encoding();
context.Response.Write(excel);
更新1:
我现在将导出代码更改为以下内容,我将字符串编码为UTF-8,获得否效果良好:
string excel = header.ToString() + styles.ToString() + title.ToString() + content.ToString() + footer.ToString();
context.Response.ContentType = "application/excel";
context.Response.AddHeader("Content-disposition", "attachment; filename=\"report.xls\"");
//context.Response.ContentEncoding = new System.Text.UTF8Encoding();
byte[] bytes = Encoding.Default.GetBytes(excel);
context.Response.Write(Encoding.UTF8.GetString(bytes));
更新2:
再次对此进行更改(由@Luaan建议),但仍然相同:
string excel = header.ToString() + styles.ToString() + title.ToString() + content.ToString() + footer.ToString();
context.Response.ContentType = "application/excel";
context.Response.AddHeader("Content-disposition", "attachment; filename=\"report.xls\"");
HttpContext.Current.Response.BinaryWrite(UTF8Encoding.UTF8.GetBytes(excel));
答案 0 :(得分:2)
<?xml version="1.0"?>
应该是
<?xml version="1.0" encoding="utf-8"?>
代替 - 并确保XML实际上是UTF-8。设置Content-Encoding
HTTP标头对您没有帮助。
编辑:您的更新不会对任何内容进行编码(实际上,您可能会以这种方式引入更多问题),您将再次将其作为字符串发送。
相反,您想要编写byte[]
:
HttpContext.Current.Response.BinaryWrite(UTF8Encoding.UTF8.GetBytes(excel));
答案 1 :(得分:0)
尝试使用一些能够正确执行此操作的库,您将节省大量时间,例如
答案 2 :(得分:0)
解决这个问题的简单方法是在HttpContext.Current.Response中设置charset,如下所示:
HttpContext.Current..Response.Charset = "UTF-8";
最完整的代码部分:
curContext.Response.Clear();
curContext.Response.Buffer = true;
curContext.Response.Charset = "UTF-8";
curContext.Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
curContext.Response.AddHeader("content-disposition", "attachment;filename=" + FileName + ".xlsx");