views:

327

answers:

3

So this is another exporting to Excel question.

I have a page that has a table with formatting by stylesheet. When I export the page by setting the ContentType to application/excel and Content-Disposition to attachment, I can export the table to Excel (not CSV). However, it loses all formatting. I think it's because Excel does not load CSS and I guess that's reasonable.

So, in a scenario where I have to show the table on the web and also export to Excel, both with similar (even if not exact) formatting, what would be the best approach without using something like NPOI?

I am trying to minimize the work and keep the single template if possible. Is it necessary for me to create two separate templates: one with stylesheet, the other with embedded style in the table itself for Excel?

Having a single template with conditional formatting inside would be very messy.

Any ideas?

A: 

there are several aproach you could instantiate a excel object on your server using VSTO, and then write the document on memory and write to response the native file, but this aproach could be a litle expensive if you create a excel object per request, so you could try to do a singleton object that wraps the excel object instance

Sebastian Marcet
these links tells how to achive thishttp://www.dotnetspider.com/resources/35896-Generating-Exporting-an-Excel-File-ASP-NET.aspxandhttp://www.eggheadcafe.com/articles/20021012.asp
Sebastian Marcet
This is too heavy - and if I wanted to do this, I'd rather use NPOI to generate the Excel document rather than use VSTO/COM.
Jiho Han
A: 

You can create a report (rdlc file) with a similar look to the grid. Then, you can have an action where you instantiate a LocalReport, pass the data you want to it and call its Render method. You then return the byte array returned by the Render method.

uvita
I don't quite follow - your suggestion seems to be about some reporting framework. How does this relate to my requirement where I have to export to Excel?
Jiho Han
Exactly, you can create a reporting services report (without having to connect to a report server). You define the layout in the report and the databindings, Visual Studio has a visual editor for that. When exporting the table you're talking about, you could instead, bind the report with the same data you used to generate the table, and you can generate an excel file or a pdf file for exporting. You have to be a little familiar with reporting services, but it's not that difficult. Did you follow the link I added? You have a little example there.
uvita
A: 

If you not yet solve the problem I'll recommend you to use Open XML SDK 2.0 for Microsoft Office (see http://www.microsoft.com/downloads/details.aspx?FamilyID=c6e744e5-36e9-45f5-8d8c-331df206e0d0&displaylang=en). With this way you will be able create an XLSX file without installing Excel on the server. XLSX file is compressed (like ZIP file) collection of xml files. Open XML SDK 2.0 helps you create and change XLSX file as pure xml files. At the first time if you look at Open XML SDK a lot of things look like strange, but it's only at the beginning. There are so named "Open XML SDK 2.0 Productivity Tool" (a part of Open XML SDK 2.0) which can generate a lot of useful code for you. Moreover you can create a nice Excel document which you can use as a prototype (template) of the document which you will create. So you can solve the problem of complex formating without writing of a lot of code.

Look at http://msdn.microsoft.com/en-us/library/cc850837(v=office.14).aspx for some examples and on http://openxmldeveloper.org/default.aspx. See also http://stackoverflow.com/questions/1012547/creating-excel-document-with-openxml-sdk-2-0 as a start example. You can find also a lot of good stuffs on http://www.codeplex.com about Open XML SDK

Oleg
Thanks for your suggestion. I will definitely take a look at this. I have a concern whether this will be transparent for users who are using older excel versions than 2007 but I think with the compatibility packs, it should be ok. The other thing is I can already do this using NPOI. But I will evaluate both to see which is better for me.
Jiho Han
By the way, the original question isn't so much how do I serve Excel over web, but how can I minimize the work needed to maintain a web and excel version of the "report". I need to support both and the formatting for both should be similar enough. Not sure if this is feasible.
Jiho Han
Than I understood you correct. I use also in my projects both web reports and export of this reports in excel. You wrote, that formating of exported data are imported for you. The same problem I has also, so I decide don't use CSV or a HTML, because of wrong converting of data during import in Excel. The compatibility packs exist for all old version of Office staring with Office 97. At all computers where the exported data will be used the compatibility pack is already installed. So it was the best way for me. Only the first steps was not very easy, but now all works perfect. Best regards.
Oleg