ASP.NET MVC Excel导出文件格式错误 [英] ASP.NET MVC Excel export file format error

查看:1218
本文介绍了ASP.NET MVC Excel导出文件格式错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在写一个ASP.NET MVC 5控制器操作的一些数据导出到Excel文件(使用一些code,我发现这里)。它的工作原理...居多。它输出一个Excel文件,我可以打开,但显示以下错误消息在此之前:

I am currently writing an ASP.NET MVC 5 controller action to export some data to an Excel file (using some code I found here). It works...mostly. It outputs an Excel file, which I can open, but not before displaying the following error message:

的文件格式和'Export.xls不匹配的扩展名,该文件可能已损坏或不安全的。除非你相信它的来源,不要打开它,你要打开它?

"The file format and extension of 'Export.xls' don't match. The file could be corrupted or unsafe. Unless you trust it's source, don't open it. Do you want to open it anyway?"

我去上选择是,然后将其打开。我可以重新保存在我的机器上,并打开该文件,我没有得到错误。否则它工作正常,唯一的其他奇怪的是,该文件的网格线的格式不同于一个Excel文件是平常,几乎更像一个HTML表格不是Excel工作表。然而,奇怪的错误消息不是东西,是可以接受的在这里,所以我必须要修复它。

I go on to select "Yes" and then it opens. I can resave it on my machine and open that file and I don't get the error. It otherwise works fine, the only other oddness is that the file's gridlines are formatted differently than is usual for an Excel file, almost more like an HTML table than an Excel sheet. However, the weird error message isn't something that would be acceptable here, so I have to fix it.

下面是我的code:

public void ExportExcel()
{
    // DataObject is a class that fetches the data for this method
    DataObject dataObj = new DataObject();
    var grid = new GridView();

    // dataObj.GetDataList returns a List<T> of data model class objects
    grid.DataSource = dataObj.GetDataList();
    grid.DataBind();

    Response.ClearContent();
    Response.Buffer = true;
    Response.AddHeader("content-disposition", "attachment; filename=Export.xls");
    Response.ContentType = "application/vnd.ms-excel";
    Response.Charset = "";

    StringWriter sw = new StringWriter();
    HtmlTextWriter htw = new HtmlTextWriter(sw);

    grid.RenderControl(htw);

    byte[] byteArray = Encoding.ASCII.GetBytes(sw.ToString());
    MemoryStream s = new MemoryStream(byteArray);
    StreamReader sr = new StreamReader(s, Encoding.ASCII);

    Response.Write(sr.ReadToEnd());
    Response.End();

}

我已经尝试过Response.ContentType为其他值(应用程序/ EXCEL,应用程序/ MS-EXCEL),设置无济于事。我有点新的ASP.NET和C#一般,所以有可能是什么我失踪或在这里做错了;我更习惯于PHP和Zend。任何见解,或帮助您能给将不胜AP preciated;谢谢!

I have already tried setting the Response.ContentType to other values ("application/excel", "application/ms-excel"), to no avail. I'm a little new to ASP.NET and C# in general, so there might be something I'm missing or doing wrong here; I'm more used to PHP and Zend. Any insight or help you could give would be greatly appreciated; thanks!

推荐答案

您正在编写一个HTML表格为Excel文件。基本上,你正在做文字与此:

You're writing an HTML table as an Excel file. Basically, you're taking text with this:

<table>
    <tr>
        <td>
            Stuff
       </td>
    </tr>
</table>

和写作,犹如的.xls 扩展名的文本文件。 Excel是聪明足够(如果你能叫的话)打开文件,并正确显示,尽管它会提醒您该文件并不是一个真正的 XLS 文件第一。

and writing it as a text file with a .xls extension. Excel is "smart" enough (if you can call it that) to open the file and display it properly, although it alerts you that the file isn't actually an xls file first.

您需要或者处理它(不是一个很好的解决方案),在表中的数据转换为CSV和发送CSV(一个更好的解决方案),或使用Excel库创建一个实际的Excel文件并发送。其中,该CSV可能是最容易的。

You need to either deal with it (not a good solution), convert the data in the table to a csv and send a CSV (a much better solution) or use an Excel library to create an actual Excel file and send that. Of those, the CSV is probably the easiest.

这篇关于ASP.NET MVC Excel导出文件格式错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆