开发者

ASP.net MVC Export To Excel

I am currently exporting to Excel using the old HTML trick, where I set the MIME type to application/ms-excel. This gives the added benefit of nicely formatted tables, however the negative of the excel document not being native Excel format.

I could export it as CSV, but then this would not be formatted.

I have read brief snippets that you can export it as XML to create the Excel document, but cannot find too much information on this. Does anybody know of any tutorials and/or benefits of this? Can it be fo开发者_如何学Pythonrmatted tables using this method?

Thanks.


Easiest way, you could parse your table and export it in Excel XML format, see this for example: http://blogs.msdn.com/b/brian_jones/archive/2005/06/27/433152.aspx

It allows you to format the table as you whish (borders, fonts,colors, I think even formulas), and Excel will recognize it as native excel format. As a plus, you can use other programs that can import Excel XML (ie.Open office, Excel viewer,etc) and you do not need to have Office components installed on the server.


Check out ExcelXmlWriter.

We've been using it for some time and it works well. There are some downsides to the xml format however. Since it's unlikely your end users will have the .xml extension associated with Excel, you end up having to download files as .xls with an Excel mime type. When a user opens a file downloaded in this way they get a warning that the file is not in xls format. If they click through it, the file opens normally.

The only alternative is a paid library to generate native Excel files. That's certainly the best solution but last time we looked there were no good, free libraries (may have changed)


Bill Sternberger has blogged a very simple solution here: export to excel or csv from asp.net mvc


Just today I had to write a routine that exported data to excel in an MVC application. Here's the details so someone may benefit in the future, first the user had to select some date ranges and areas for the report. On the post back, this method was in place, with TheModelTypeList containing the data from LINQ/Entity Framework/SQL Query returning strong types:

        if (ExportToExcel) {
            var stream = new MemoryStream();
            var serializer = new XmlSerializer(typeof(List<SomeModelType>));
            serializer.Serialize(stream, TheModelTypeList);
            stream.Position = 0;
            FSR = new FileStreamResult(stream, "application/vnd.ms-excel");
        }

The only catch on this one was the file type was not known when opening so the system prompted for the application to open it... this is a result of the content being XML.... I'm still working on that.


I am using Spreadsheet Light, an Open-Source library that provides ridiculously easy creation, manipulation and saving of an Excel sheet from C#. You can have an MVC / WebAPI Controller do the work of creating the file and either

  1. Return a URL link to the saved Excel file to the page and invoke Excel to open it with an ActiveX object
  2. Return a Data Content Stream to the page
  3. Return a URL link to the calling page to force an Open / Save As dialog

http://spreadsheetlight.com/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜