C#导出为Excel UTF-8问题
我想出口索姆数据到XLS ,但似乎有UTF-8编码的问题。 它输出正常,我得到Excel文件应该看,但是当插入的数据有特殊字符,在这种情况下,丹麦字符如æ , ø和å ,那么该文件是有点结果。 当我尝试在Excel中打开它时,出现读取文件时出现问题的消息,并将问题提及为设置spreadseet 。
我在几个通过validation的在线validation服务中运行了结果XMLvalidation。 当我在Notepad ++ XMLvalidation器中运行它时,在第一个发现的特殊字符(它是Å )中出现错误:XX行的XMLparsing错误:input不正确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:
我现在将导出代码更改为以下string编码为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:
再次改变(由@Luaanbuild议),但仍然是一样的:
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));
<?xml version="1.0"?>
应该
<?xml version="1.0" encoding="utf-8"?>
相反 – 确保XML实际上是UTF-8。 设置Content-Encoding
HTTP标题不会帮助你。
编辑 :您的更新不编码任何东西(事实上,你可能会引入更多的问题,这种方式),你发送它再次作为string。
相反,你想写的byte[]
:
HttpContext.Current.Response.BinaryWrite(UTF8Encoding.UTF8.GetBytes(excel));
尝试使用一些库,可以做到这一点,你会节省很多时间,例如
解决这个问题的简单方法是在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");
- Excel互操作 – 如何停止数字(存储为文本)被“评估”
- 如何使用“LINQ组”?
- 将Excel转换为Memory Stream并通过AngularJS进行下载
- 使用excel和C ++将指数转换为CSV
- 工作线程中Excel Interop性能下降
- 为什么“mscorlib.dll中发生”System.IO.IOException“types的未处理的exception(由某个.exelocking)”发生?
- 如何将excel工作簿转换为pdf而不使用excel互操作库?
- 为什么openfiledialog导致excel被添加到任务pipe理器进程?
- Excel的AddOLEObject函数不能在窗口2008 R2上工作