views:

1176

answers:

8

Possible Duplicate:
Create Excel (.XLS and .XLSX) file from C#

I am writing a program that generates excel reports, currently using the Microsoft.Interop.Excel reference. My dev computer has Excel on it, but the end user may or may not have Office installed. Will this tool fail if Office isn't installed on the end users computer, or is this interop service separate from the actual application?

+2  A: 

Unless you have excel installed on the Server/PC, or use an external tool (without using Excel Interop see Create Excel (.XLS and .XLSX) file from C#, it will fail, using the interop requires Excel to be installed.

astander
+1  A: 

An interop calls something else, it's an interoperability assembly, so you're inter-operating with something...in this case Excel, the actual installed Excel.

In this case yes, it will fail to run because it depends on excel, you're just calling excel functions. If they don't have it installed...out of luck.

There are methods to generate without Excel, provided the 2007 file format is ok, here's a few:

as I said though, this is the 2007 format, normally if anything, that's the deal-breaker.

Nick Craver
+1 for pointing out that the "interop" needs something to interoperate with.
John M Gant
+2  A: 

There are a handful of options:

  • NPOI - Which is free and open source.
  • Aspose - Is definitely not free but robust.
  • Spreadsheet ML - Basically XML for creating spreadsheets.

Using the Interop will require that the Excel be installed on the machine from which it is running. In a server side solution, this will be awful. Instead, you should use a tool like the ones above that lets you build an Excel file without Excel being installed.

If the user does not have Excel but has a tool that will read Excel (like Open Office), then obviously they will be able to open it. Microsoft has a free Excel viewer available for those users that do not have Excel.

Thomas
+1 for Aspose. I've used it to great success, and it is very easy to work with.
rmeador
NPOI has worked well for my purposes (e.g. no budget to buy anything). However, it does not support .xslx generation.
Odrade
@rmeadorWell you certainly are lucky. I've had nothing but grief using Aspose. Horrible quality control with numerous undocumented changes, constant silly bugs, questionable api design (NullReferenceException when setting a property to null.. wtf?!) Their only saving grace is fixes come out at a decent rate, but every fix they release means wasted hours spent testing their patches since it's impossible to trust what they give you.
Krypes
@Odrade - Xslx was not mentioned as part of the specification and is not universally readable. However, if xslx is what you want to do, then you can use the Open Office XML format.
Thomas
@Kyrpes - I'm not sure when you last used Aspose.Cells, but it is used in the project on which I'm working for a few years and beyond the high cost, it has worked flawlessly. We've never had one issue with it.
Thomas
A: 

You could use the ExcelStorage Class of the FileHelpers library, it's very easy and simple... you will need Excel 2000 or later installed on the machine.

The FileHelpers is a free and easy to use .NET library to import/export data from fixed length or delimited records in files, strings or streams.

kzen
+1  A: 

Use OleDB, you can create, read, and edit excel files pretty easily. Read the MSDN docs for more info:

http://msdn.microsoft.com/en-us/library/aa288452(VS.71).aspx

I've used OleDB to read from excel files and I know you can create them, but I haven't done it firsthand.

Justin
A: 

If you're interested in making .xlsx (Office 2007 and beyond) files, you're in luck. Office 2007+ uses OpenXML which for lack of a more apt description is XML files inside of a zip named .xlsx

Take an excel file (2007+) and rename it to .zip, you can open it up and take a look. If you're using .NET 3.5 you can use the System.IO.Packaging library to manipulate the relationships & zipfile itself, and linq to xml to play with the xml (or just DOM if you're more comfortable).

Otherwise id reccomend DotNetZip, a powerfull library for manipulation of zipfiles.

OpenXMLDeveloper has lots of resources about OpenXML and you can find more there.

If you want .xls (2003 and below) you're going to have to look into 3rd party libraries or perhaps learn the file format yourself to achieve this without excel installed.

Aren
A: 

Try EPPlus if you use Excel 2007. Supports ranges, cellstyling, charts, shapes, pictures and a lot of other stuff

Jan Källman
A: 

You can try SmartXLS for .Net,it support both xls and xlsx files,it also support create full feature Excel charts and pivot tables from scratch.

liya