Is there any chance to read .xlsx files in C# without having Excel installed on the client, and with no OLEDB connection available?
The application is a Winforms project written in VS2005 C# .NET Framework 2.0.
How about this one here:
EPPlus-Create advanced Excel 2007 spreadsheets on the server
EPPlus is a .net library that reads and writes Excel 2007 files using the Open Office Xml format (xlsx).
EPPlus supports ranges, cell styling, charts, pictures, shapes, named ranges, autofilters and a lot of other stuff.
I decided to use the the Excel Data Reader on CodePlex.
Koogra can read .xlsx files.
Have a look at excellibrary. For now it supports only xls, but xlsx support is planned for the future.
This question looks to be similar to this one no?
Have you checked NPOI ?
Best
We use NPOI successfully in our projects.
There might be some 3rd party components that can help you.
Here's one
Here's another
NPOI's latest version support both xsl and xslx. Download it from
NPOI 2.0
and use NPOI.XSSF.UserModel.XSSFWorkbook fro xlsx.
Related
What is the best way/library to read Excel 2003 and 2007 files using C#? I need to do some heavy parsing.
Do you need to parse the file, or deal with the contents?
For parsing the file, you'd better hope it's in Open Office XML format, because the previous binary version is not documented at all.
If you just need to deal with the contents, use the Office Interop libraries.
You can try SmartXLS for .Net,it support most features of excel(cell formatting,Charts,formulas,pivot tables etc),and can read/write both the excel97-2003 xls format and the excel2007 openxml format.
I would start by trying to use ADO.NET.
If that doesn't work, I used xlsio by Syncfusion.
If the data is in some kind of table format I'd suggest to try using OleDbConnection and treating the Excel sheet as another data connection. Otherwise Interop is ok if it's not on a server or anything like that.
You can use the MS Office interop assemblies (see here) to access Excel files from .NET applications.
There are a number of 3rd party tools you can use. I would avoid using the Interop libraries as they can be pretty slow. I have used Aspose.Cells before and it works pretty well. It does cost some money though.
This question already has answers here:
How do I create an Excel (.XLS and .XLSX) file in C# without installing Microsoft Office?
(47 answers)
Closed 4 years ago.
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?
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.
Unless you have Excel installed on the Server/PC or use an external tool (which is possible without using Excel Interop, see Create Excel (.XLS and .XLSX) file from C#), it will fail. Using the interop requires Excel to be installed.
Try EPPlus if you use Excel 2007. Supports ranges, cellstyling, charts, shapes, pictures and a lot of other stuff
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.
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:
http://excelpackage.codeplex.com/
http://simpleooxml.codeplex.com/
as I said though, this is the 2007 format, normally if anything, that's the deal-breaker.
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.
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.
What's the way to open a excel workbook and to read excel cells?
In this thread you will get details how to open and handle excel through C#.
Use Visual Studio Tools for Office (VSTO) to extend Office products with your code and here's a good tutorial to get you started in Excel programming
There is an open source .NET library called Koogra for reading Excel files, both BIFF (older Excel) and XSLX (newer Excel). Otherwise you can use Excel automation, but I would avoid that if possible.
You can also use open xml sdk to read (and write) excel files (in open xml format off course). There is an msdn article describing how to use the open xml sdk.
A big advantage is that you don't have to install excel on the server (or client) to read excel files.
I want to know what is the best practice to create a Excel 2007 Workbook using C#, with its datasource being a raw flat file or a table in database.
You can use
"Open XML SDK 2.0 for Microsoft Office"
It's more comfortable than harcore manually hacking OpenXML spec. There are .NET strongly typed wrapper classes so it's not hard to create a simple sheet. You don't need any interop and msoffice installed and it's safe for server soluitions - there are only a few dlls which you can ship in your solution.
I did mail-merge solution and it wasn't so scary.
But as always, when it's possible, I'm prefering plain csv format.
I personally like creating CSV's, which can be opened directly in Excel. It's a lot less work than trying to hack the Office Open XML specification, and you don't need COM interop to Excel (which requires a copy of Excel to work).
You can use the Office Primary Interop Assemblies to completely automate Excel 2007, and create the workbook from within C#.
This gives you the most control, as you have complete control over how you map from DB or flat file -> Excel workbook/worksheet.
I am using
http://www.leniel.net/2009/07/creating-excel-spreadsheets-xls-xlsx-c.html
for creating excel . Seems good so far.
Simple question. Does anyone know an easy way to convert SpreadsheetML (Excel 2003 XML) to the Open Document XML used for Excel 2007 (xlsx) files?
I've got a library that unfortunately doesn't read the XML format, so I need to try and find a way to read the data, that doesn't involve another library.
Any suggestions appreciated.
If you've got deep pockets Aspose.Cells can read/write both formats and should provide for really easy conversion without automation.
http://www.aspose.com/categories/.net-components/aspose.cells-for-.net/default.aspx
Aspose.Cells for .NET
Aspose.Cells for .NET is an award-winning Spreadsheet component that allows .NET developers to embed the ability to read, write and manipulate Excel spreadsheets into their own applications without needing to rely on Microsoft Excel.
Aspose.Cells for .NET is a mature, scalable and feature rich component that offers many functions that are way beyond the simple data exporting capabilities of other vendors. With Aspose.Cells for .NET developers can export data, format spreadsheets to the most granular level, import images, import charts, create charts, apply and calculate complex formulas, stream Excel data, save in various formats and much more - all this without the need to use Microsoft Excel or Microsoft Office Automation.
Pricing starts at $899 per developer for enterprise (internal) development and goes up from there very steeply.
The file format has indeed changed significantly from SpreadsheetML to Office Open XML.
And, since now spreadsheet files are broken into multiple XML files (which are all then zipped), there's no real hope of an easy XLST solution.
The most straightforward course of action, unfortunately, is to automate Excel using a macro to open each SpreadsheetML files and do a "Save As" to the newer format. This can be done in Office 2003 with the Office 2007 file format plug-in. Perhaps this can be relegated to a batch process so the server is not directly involved?
If the data in the spreadsheets are trivial and follow a consistent format, you can write your own parser to import directly from the SpreadsheetML files.
An easy way would be to use Excel's COM Library (Excel 2007), but I think that's not the answer you are looking for.
What's your library capable of? You could use the Open XML SDK 2.0 to write the spreadsheet document based on the output of your library.
Best Regards
Try using JODConverter. JODConverter allows conversion of SpreadsheetML using the OpenOffice.org or Libreoffice engine.
IIRC the Office 2003 format works like OpenDocument format: It's a ZIP file with XML files inside, so (if you have enough time/courage) you can open it, find the XML file that contains the data and finally deal with XML.
I know, this answer is for brave developers ;)
Regards.
check this code static void XlsToXlsx
static void XlsToXlsx (string sourceFile, string destinationFile)
{
Type officeType = Type.GetTypeFromProgID("Excel.Application");
Excel.Application app = new Excel.Application();
app.DisplayAlerts = false;
// Open Excel Workbook for conversion.
Excel.Workbook excelWorkbook = app.Workbooks.Open(sourceFile);
// Save file as CSV file.
//excelWorkbook.SaveAs(destinationFile, Excel.XlFileFormat.xlCSV);
excelWorkbook.SaveAs(destinationFile, Excel.XlFileFormat.xlOpenXMLWorkbook);
// Close the Workbook.
excelWorkbook.Close();
// Quit Excel Application.
app.Quit();
}