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.
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
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.
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.
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
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.