How to programmatically create a "true" Excel file

Go To StackoverFlow.com

7

Possible Duplicate:
Generating an Excel file in ASP.NET

Here's my question: How do you programmatically create a "true" Excel file?

Note: I don't mean a CSV, HTML/CSS, or XML file, I mean a file written in the same format Excel uses to save spreadsheets.

Background: I'm working on an ASP.Net application, which includes Telerik's RadGrid. We're currently using the RadGrid's ExportToExcel() function, but some of our customers need to be able to view these exports using Excel Viewer. Telerik's function exports an HTML/CSS-based file, which the full-version of Excel can open, but Excel Viewer cannot.

I'm sure its possible to programmatically create a "true" Excel file, but I'm not sure where to begin. Is there an SDK which could help me do this?

Any suggestions would be appreciated. Thanks in advance.

2012-04-05 20:21
by ks78
I am also looking for answer. One option is to use Excel by interop services. But this uses an Excel instance which is very slow - Shiplu Mokaddim 2012-04-05 20:26


13

You can use the interop from Excel like RedFilter showed you, but there are also open source alternatives that does not require Excel to be installed on your pc to work. It can be useful if you have a asp.net project deployed on a server that does not have Excel.

EPPlus is a good library. http://epplus.codeplex.com/

Personnally, I use Koogra. http://sourceforge.net/projects/koogra/?_test=b

2012-04-05 20:29
by Amaranth
+1 for EPPlus, I've used it both for creating as well as opening/reading files. Works great - Nick Bork 2012-04-05 20:30
It's for 2007/2010 files, to it probably only handles *.XLSX files, but does not work with XLS file format. Anyway, it's ok as long as you don't need to use them :) - Lukasz M 2012-04-05 20:48
EPPlus looks like it may be what I'm looking for. Thanks, Amaranth - ks78 2012-04-05 20:52


1

You can obtain the Office File Formats specs from Microsoft, but the binary formats are hideously complex in places. You could probably get away with a minimal implementation though. Here is the .xls binary file format spec. I would suggest using the new XML formats used by Office 2007 and 2010 - See this WikiPedia article on the Office Open XML standard.

The only other option that comes to mind is automating Excel and getting it to produce the file.

2012-04-05 20:26
by Bork Blatt
If Excel Viewer could open the new XML formats, I'd be thrilled. --but they don't seem to be supported - ks78 2012-04-05 20:32
If your clients are amenable to updating to the latest Excel Viewer here: http://www.microsoft.com/download/en/details.aspx?displaylang=en&id=10 (which according to the download page can display .xlsx files) then perhaps it is problem solved? I have also updated my answer with a direct link to the .xls binary spec in case there is no avoiding it - Bork Blatt 2012-04-05 20:42


1

You can use interop (as described in another answer), but you it's not officially supported in server environment.

Another solution would be using OpenXML SDK 2.0, which should allow you to create XLSX files. It can be downloaded from here: http://www.microsoft.com/download/en/details.aspx?displaylang=en&id=5124. If you decided to use this solution, I recommend to take a look at OpenXML SDK 2.0 Productivity Tool available there as well.

2012-04-05 20:27
by Lukasz M
I've looked at OpenXML, but can it generate a "true" Excel file? It looks like it generates XML-based files which the full version of Excel can read, but Excel Viewer would not. Am I wrong - ks78 2012-04-05 20:30
I should be capable of creating valid and real XLSX files, which is format used by Office 2007 and newer. I haven't tested this, but it should work correctly with both 2207 (or newer) Excel of Excel Viewer. However, It can't generate XLS files (format used by Office 2003 and earlier) - Lukasz M 2012-04-05 20:34
OpenXML is Microsofts format for XML file - Murph 2012-04-05 20:34
Yes, but it's important to mention that *.XLSX is an OpenXML file: http://en.wikipedia.org/wiki/OpenXML - Lukasz M 2012-04-05 20:40


1

The latest version of the ExcelViewer apparently supports the following file formats:

You can open only Excel files (.xlsx, .xlsm, .xlsb, .xltx, .xltm, .xls, .xlt, .xlm, and .xlw). Even though you can open macro-enabled files (.xlsm, .xltm, and .xlm), you will not be able to run macros.

So... the XML format files are not a problem - and therefore the appropriate solution is to write OpenXML formats. Unfortunately this isn't fun... you can do it, the toolkit lets you reverse engineer stuff but the code is horrid.

So, if it were me (and it has been and will be) I would look at ClosedXML to generate OpenXML documents - its top of my list for next time round.

There are, of course, a number of commercial options too - how suitable depends on your specific use case.

For all that, the "right" answer is to generate OpenXML (.xlsx) documents by whatever means.


Edit: The reference to: ExcelXMLWriter was wrong - that won't help as its ExcelML

2012-04-05 20:56
by Murph
Thanks. I may also give OpenXML a try. Is the XML format it produces the same as "ExcelML"? Telerik can export to that format as well, but Excel Viewer can't open it - ks78 2012-04-05 21:08


1

Apache POI is a popular and reliable Java library for writing Excel files. NPOI is another good option (http://npoi.codeplex.com) to write Excel files with .NET.

2012-04-06 00:40
by bouvierr
Ads