views:

1556

answers:

10

I'm confused as hell with all the bazillion ways to read/write/create excel files. VSTO, OLEDB, etc, but they all seem to have the requirement that office must be installed.

Here is my situation: I need to develop an app which will take an excel file as input, do some calculations and create a new excel file which will basically be a modification of the first excel file. All with the constraint that the machine that runs this may not have office installed. (Don't ask why...)

Any ideas?

I need to support all excel formats. The only saving grace is that the formats spreadsheets themselves are really simple. Just a bunch of columns and values, nothing fancy. And unfortunately no CSV as the end user might not even know what a CSV file is.

+5  A: 

read and write csv files instead. Excel reads them just fine and they're easier to use. If you need to work against .xls files then try having support for OpenOffice as well as Excel. OpenOffice can read and write excel files.

RHicke
The OpenOffice API can be very useful here: http://api.openoffice.org/
Jweede
Problem here is 0 formatting.
Kyle Rozendo
+7  A: 

Without Office installed you'll need something designed to understand the Excel binary file format (unless you only want to open Office 2007 .xlsx files).

The best I've found (and that I use) is SpreadsheetGear, which in addition to being .NET native, is much faster and more stable then the COM/OLE solutions (which I've used in the past)

David
A: 

Excel files are in a proprietary format so (afaik) you're not going to be able to do this without having the office interop available. Some third party tools exist (which presumably licence the format from MS?) but I've not used them myself to comment on their usefulness.

I assume that you can't control the base file format, i.e. simple CSV or XML formats aren't going to be possible?

annakata
+18  A: 

write your excel in HTML table format:

<html>
<body>
  <table>
    <tr>
    <td style="background-color:#acc3ff">Cell1</td>
    <td style="font-weight:bold">Cell2</td>
    </tr>
  </table>
</body>
</html>

and give your file an xls extension. Excel will convert it automatically

Gregoire
you're freaking kidding me.
eviljack
I've used this method several times, simple, works great. But in the case of web apps, just set the content type/mime type to excel (cant remember the exact mime type off the top of my head)
Neil N
And, depending on your version of excel, it usually picks up formatting as well. Bolded table cell? Excel carries it over.. Background color? copies that too...
Neil N
I use this all the time to export xls files from web apps.
recursive
That is the coolest thing i've seen this week. I've spent hours trying to find a nice way to export XLS from PHP
Neil Aitken
A: 

I used to use a very nice library called CarlosAg, which uses Excel XML format. It was great (and Excel recognizes the format), and also incredibly fast. Check it out here.

Oh, as a side note, we used to use this for the very same reason you need it. The servers that generated these files were not able to have Excel installed.

Kyle Rozendo
A: 

If you cannot work with CSV files as per @RHicke's suggestion, and assuming you are working on a web app, since a desktop app would be guaranteed to have XL installed as per requirements.

I'd say, create your processing app as a webservice, and build an XL addin which will interact with your webservice directly from XL.

John MacIntyre
+1  A: 

Did you consider way number bazillion and one: using the Open XML SDK? You can retain styles and tweak it to your liking. Anything you can do in an actual file is possible to achieve programatically. The SDK comes with a tool called Document Reflector that shows the underlying XML and even shows LINQ statements that can be used to generate them. That is key to playing around with it, seeing how the changes are made, then recreating that in code.

The only caveat is this will work for the new XML based formats (*.xlsx) not the older versions. There's a slight learning curve but more material is making its way on blogs and other sites.

Ahmad Mageed
A: 

Excel XLSX files "just" XML files - more precisely ZIP files containing several XML files. Just rename a Excel file Test.xslx to Test.zip and open it with your favourit ZIP program. XML schemas are, afaik, standardized and availiable. But I think it might not be that easy to manipulate them only using primitive XML processiing tools and frameworks.

Daniel Brückner
A: 

For XLSX files, look at using http://www.codeplex.com/ExcelPackage. Otherwise, some paid 3rd party solutions are out there, like the one David suggested.

Josh
+1  A: 
Chris Persichetti
After trying out some of the free alternatives, my team decided on using Aspose.Cells as well. It's very intuitive to use and the code is easy to maintain.
dawntrader