导出excel .xls和.xlsx EPPlus .NET

时间:2011-11-19 10:27:20

标签: .net export-to-excel epplus

我有一个代码,当我设置输出文件的扩展(如'.xlsx')时效果很好,但当我将其更改为'.xls'时,我在MSExcel中打开文件之前有窗口该文件不正确(文件格式不正确)然后是很多不好的编码字符(比如日语等)。

有人有这个问题吗?有解决方案吗?

    _currentContext.Response.Clear();
    _currentContext.Response.ClearContent();
    _currentContext.Response.ClearHeaders();
    _currentContext.Response.AddHeader("content-disposition", "attachment; filename=FileName.xlsx");
    _currentContext.Response.ContentEncoding = System.Text.Encoding.UTF8;
    _currentContext.Response.ContentType = "application/ms-excel";
    _currentContext.Response.AddHeader("Content-Transfer-Encoding", "binary");
    _currentContext.Response.BinaryWrite(_package.GetAsByteArray());
    _currentContext.Response.Flush();
    _currentContext.Response.End();

有一刻 - 当我在本地机器上以xls导出它时,一切正常。当我在远程服务器上尝试时 - 我只能正确导出到xlsx扩展中。

2 个答案:

答案 0 :(得分:4)

这是因为AFAIK EPPlus只能导出XLSX(OpenXML格式的Excel 2007及更高版本)... whils XLS是旧的二进制Excel-Formt,因此Excel确实正确地说格式错误......

编辑 - 关于可能的MIME类型,请尝试以下方法:

application/vnd.ms-excel
application/x-msexcel
application/ms-excel
application/msexcel
application/x-excel
application/vnd.openxmlformats-officedocument.spreadsheetml.sheet

答案 1 :(得分:0)

它会提示文件扩展名不正确。但虽然.xls是正确的扩展,不要担心单击是。 您的文件打开平稳,没有数据损坏。

//Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
                Response.ContentType = "application/x-msexcel";
相关问题