I am searching a C# way to delete (empty) Excel-rows in a worksheet without using the Microsoft.Office.Interop.Excel namespace.
Found many examples with the Interop namespace like C# and excel deleting rows . But is there a way to do it without third-party-tools - only with the .NET?
Thank you for your help!
The options for working with Excel files relying only on standard .NET Framework namespaces is limited. Two possibilities come to mind. The first is "simplest", but only applicable if your main interest is in working with the content as a database. The second allows you to do pretty much "anything" with the Excel workbook, but the learning curve will be steep.
Both of these approaches are suited for working in a server environment (unlike those that require presence of the Excel application) and do not require any licenses.
You can use an OLE DB connection (ACE OLE DB provider) to communicate with the contents of an Excel workbook. It allows connecting to individual worksheets as well as named ranges. Basic SQL functionality is supported.
The file format of Excel 2007 and later versions is Office Open XML (OOXML). These files are "zip packages" containing the files (xml for the most part) that make up a workbook. So any standard tools that can work with Zip packages and XML can be used to open up an Excel workbook, edit the content, then close the workbook back up. In the .NET Framework, these would be the System.IO.Packaging (in WindowsBase.dll, usually needs to be referenced specifically) and System.XML namespaces.
The documentation for the file formats is the ECMA-376 standard (http://www.ecma-international.org/publications/standards/Ecma-376.htm). A useful on-line resource is openxmldeveloper.org.
Note that Microsoft also provides the Open XML SDK, a free download which can be distributed license-free with your solution. The Open XML SDK reduces the "learning curve" as it reduces the amount of knowledge you need about the OOXML file formats. I mention this for the sake of completeness, because I know how challenging trying to work directly with the file format is. Also, since the DLL is freely distributable and can be copied as part of your solution it might meet your requirements.
This stackoverflow post may help - it discusses some libraries that can manipulate excel without needing Office installed.
The question regards VB.NET but I believe the options discussed would work with C# too...
How to process excel file in vb.net without office installed
I am developing an application which primarily imports spreadsheet reads the data and manipulates it.
I was googling all day, unable to find a way to read data from excel files.
It would be great if I could get some help to do the required.
Information :
I am developing Windows Store app using C#/XAML.
Reference libs/apps which read excel :
http://www.componentone.com/SuperProducts/SpreadNET/
http://www.greatwindowsapps.com/app/tile-charts
If you look for a free open source solution, I used in several projects exceldatareader from codeplex lib, very simple and easy to use.
It depends only by SharpZipLib, I think that you have only to recompile both projects as portable libs to make it work on WinRT.
If you're working with .xlsx files there's always Open XML SDK which you can get from NuGet as well. Here's a bunch of tutorials to get you started. Unfortunately the really nice wrapper library ClosedXML doesn't work with Windows Store apps.
There's also a commercial component available, XlsIO from Syncfusion, which you could try out. I don't have any first hand experience with it.
I posted the same the question in the MSDN Blog
http://social.msdn.microsoft.com/Forums/en-US/winappswithcsharp/thread/4fce4765-2d05-4a2b-8d0a-6219e87f3307
Code provided by VT.CrazyAppy works fine for reading & parsing simple spread sheet file.
The code snippet is static , I am working on it to make it more generic.will post the code
once I done with that.
Note : The code will provide a mechanism to read a simple spread sheet file and return them as K,V pair.
You can modify it to suit your need.
I'm looking for any available free Excel "helper" classes that are written for .net (doesn't have to be C#).
I'd like to evaluate what others consider to be useful and generic static (and non static) helper methods.
Can be targeted at VSTO or regular Office automation, although that probably doesn't matter.
I guess I should also point out that this question is NOT asking for "what are good alternatives (or free alternatives) to using Excel object model automation. I don't really want links to SS gear, etc, although they are all great products that's not the purpose of the question.
In case I wasn't clear enough in the previous paragraph: I don't want answers that state - use this free (or not free) 3rd party component, instead of Excel object model code.
So basically what i was asking for was user developed C# code libraries that contain VSTO, Office or Excel helper functions such as static methods for working with menus, ribbons, ranges, workbooks, documents, xml (such as the ones otaku mentioned)...
VSTO Power Tools is a good set of utilities to work with Excel:
http://msdn.microsoft.com/en-us/magazine/dd263100.aspx
ExcelDNA is a very cool project. It provides a lightweight way to consume .NET code from Excel, with a deployment model which is much easier than VSTO. I found it an interesting alternative to VSTO if you want to write UDF in .NET for instance.
In terms of Office automation for Excel, there are some good PowerShell Open XML cmdlets that can be used for PS or ported to C# at Announcing the Release of PowerTools for Open XML V1.1. Eric White's blog, where this link is, has recently done an more in depth look at Excel automation using Open XML, like Table Markup in Open XML SpreadsheetML. Note about Open XML - although it is the preferred method by many, you don't actually have to use the Open XML SDK - you can just use System.IO.Packaging to gain access to Excel 2007/2010 files.
Also, often overlooked, but the Excel snippets available for VSTO, Open XML and Interop development are great. 1, 2, 3
Also, check out the Excel samples for VB # http://msdn.microsoft.com/en-us/library/8x19fbw1(v=VS.90).aspx. These come installed with VS when you also choose VB as a language during the install.
Update: Just discovered the All-In-One Code Framework (Office) which has a lot of great Excel helper classes. You can find it on: http://1code.codeplex.com/releases/view/51868.
For VSTO projects:
http://www.add-in-express.com/
Non-VSTO, non-automation:
http://www.html-to-pdf.net/excel-library.aspx
Note: neither of these libraries are free.
There's one called FileHelpers which enables you to save Excel data as CSV and the FileHelpers library can easily parse the information and so on, sure it may not be fully blown Excel automation, but it is a good headway to parsing. If you're talking about being able to deal with Excel in a native binary fashion, then this article from CodeProject might help, also here, is BIFF parser that understand the underlying excel data storage, and also here on CodeProject.
With .net 4.0 dynamic keyword writing Excel automation easy as hell.
If you want intellisense you can easily write c# wrapper on top of dynamic calls.
UPDATE
We are working with Excel automation since 2001 (developing financial addins).From Excel 2000 up to 2010.
And all we know that standard interop simply does not work when your code should work with all versions and service packs of Office.
We had beaten many times by custom interop libraries, VSTO versions, etc. After a year we found a only one solution that works - call all automation via late bound calls (reflection). i.e. (''notepad code'') Type.GetTypeFromProgId('Excel.Application').GetMethod('Visible').Invoke(....).
But code was very big and ugly, then we simply wrap that ugly code with Object Model copied from Excel 1:1. When we need new method we add it to our Excel Object model and call excel automation via late bound call.
With new dynamic keyword it is possible to live without wrapper especially with Resharper's dynamic support.
I prefer this one:
Excel Package on CodePlex
it uses the Open XML, so no need for an office install on the computer (may or may not factor in), but I found it very easy to use and set up.
I really like NPOI for 2003/2007 formats
http://npoi.codeplex.com/
Totally standalone and easy to use
What is the best way to export objet to excel file in C# (.net framework 3.5)?
Thanks in advance!
If it's tabular data, you could generate HTML tables and let Excel open it up intuitively. Otherwise I'd recommend COM Interop.
I've used EPPlus to generate xlsx files (basically reports - SQL Reporting Services 2008 R2 still doesn't support it natively, just the older xls).
I've heard good things about NPOI, which is a .NET port of the Apache POI project
If you want to do it 'natively' and interop with a real instance of Excel, you can use the classes in Excel's Primary Interop Assembly - look in the microsoft.office.interop.excel namespace
A lot depends on what kind of objects you have already and what you want your intended output to be - if you can specify more of that, we can give a more specific answer.
In the past I've used the clipboard to save objects to multiple formats that can then be pasted into different applications including Excel or Word, tyring to find an example online I stumble across this:
http://support.microsoft.com/kb/306023
Looks Good!
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking us to recommend or find a tool, library or favorite off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it.
Closed 9 years ago.
Improve this question
In one of our ASP.NET applications in C#, we take a certain data collection (SubSonic collection) and export it to Excel. We also want to import Excel files in a specific format. I'm looking for a library I can use for this purpose.
Requirements:
Excel 2007 files (Does Excel 2003 support over 64k rows? I need more than that.)
Does not require Excel on the server
Takes a typed collection and, if it can, tries to put numeric fields as numeric in Excel.
Works well with large files (100k to 10M) - fast enough.
Doesn't crash when exporting GUIDs!
Does not cost a crapload of money (no enterprise library like aspose). Free is always great, but can be a commercial library.
What library do you recommend? Have you used it for large quantities of data? Are there other solutions?
Right now, I am using a simple tool that generates HTML that is loaded by Excel later on, but I am losing some capabilities, plus Excel complains when we load it. I don't need to generate charts or anything like that, just export raw data.
I am thinking of flat CSV files, but Excel is a customer requirement. I can work with CSV directly, if I had a tool to convert to and from Excel. Given Excel 2007 is an xml-based (and zipped) file format, I am guessing this kind of library should be easy to find. However, what matters most to me are your comments and opinions.
EDIT: Ironically, in my opinion and following the answer with the most votes, the best Excel import&export library is no export at all. This is not the case for all scenarios, but it is for mine. XLS files support only 64k rows. XLSX supports up to 1M. The free libraries that I've tried feature bad performance (one second to load one row when you have 200k rows). I haven't tried the paid ones, as I feel they are overpriced for the value they deliver when all you need is a fast XLSX<->CSV conversion routine.
I'm going to throw my hand in for flat csv files, if only because you've got the greatest control over the code. Just make sure that you read in the rows and process them one at a time (reading the document to the end and splitting will eat up all of your memory - same with writing, stream it out).
Yes, the user will have to save-as CSV in excel before you can process it, but perhaps this limitation can be overcome by training and providing clear instructions on the page?
Finally, when you export to the customer, if you set the mime type to text/csv, Excel is usually mapped to that type so it appears to the user to be 'an Excel file'.
I discovered the Open XML SDK since my original answer. It provides strongly typed classes for spreadsheet objects, among other things, and seems to be fairly easy to work with.
I am going to use it for reports in one of my projects. Alas, version 2.0 is not supposed to get released until late 2009 or 2010.
the last version of ExcelPackage that is free under LGPL for commercial projects is, https://www.nuget.org/packages/EPPlus/4.5.3.3
If you need latest and greatest, Commercial license is available here: https://epplussoftware.com/en/LicenseOverview/
I'm still fighting with the export to excel function since my application should export some data to excel-template 2007
this project seems fine to me, and the developer is very responsive to bugs and issues.
I've been using ClosedXML and it works great!
ClosedXML makes it easier for developers to create Excel 2007/2010
files. It provides a nice object oriented way to manipulate the files
(similar to VBA) without dealing with the hassles of XML Documents. It
can be used by any .NET language like C# and Visual Basic (VB).
SpreadsheetGear for .NET reads and writes CSV / XLS / XLSX and does more.
You can see live ASP.NET samples with C# and VB source code here and download a free trial here.
Of course I think SpreadsheetGear is the best library to import / export Excel workbooks in ASP.NET - but I am biased. You can see what some of our customers say on the right hand side of this page.
Disclaimer: I own SpreadsheetGear LLC
NPOI For Excel 2003
Open Source
http://www.leniel.net/2009/07/creating-excel-spreadsheets-xls-xlsx-c.html
I've used Flexcel in the past and it was great. But this was more for programmatically creating and updating excel worksheets.
CSV export is simple, easy to implement, and fast. There is one potential issue worth noting, though.
Excel (up to 2007) does not preserve leading zeros in CSV files. This will garble ZIP codes, product ids, and other textual data containing numeric values.
There is one trick that will make Excel import the values correctly (using delimiters and prefix values with the = sign, if I remember correctly, e.g. ..,="02052",...).
If you have users who will do post-processing tasks with the CSV, they need to be aware that they need to change the format to XLS and not save the file back to CSV. If they do, leading zeros will be lost for good.
For years, I have used JExcel for this, an excellent open-source Java project. It was also .NET-able by using J# to compile it, and I have also had great success with it in this incarnation. However, recently I needed to migrate the code to native .NET to support a 64-bit IIS application in which I create Excel output. The 32-bit J# version would not load.
The code for CSharpJExcel is LGPL and is available currently at this page, while we prepare to deploy it on the JExcel SourceForge site. It will compile with VS2005 or VS2008. The examples in the original JExcel documentation will pretty well move over intact to the .NET version.
Hope it is helpful to someone out here.
I've worked with excel jetcell for a long time and can really recommend it.
http://www.devtriogroup.com/exceljetcell
Commercial product
Excel files XLS & XLSX
Based on own engine in pure net.
The following site demonstrates how to export a DataTable, DataSet or List<> into a "proper" Excel 2007 .xlsx file (rather than exporting a .csv file, and getting Excel to open it).
It uses the OpenXML libraries, so you don't need to have Excel installed on your server.
Mikes Knowledge Base - ExportToExcel
All of the source code is given, free of charge, aswell as a demo application.
It's very easy to add to your own applications, you just need to call one function, passing in an Excel filename, and your data source:
DataSet ds = CreateSampleData();
string excelFilename = "C:\\Sample.xlsx";
CreateExcelFile.CreateExcelDocument(ds, excelFilename);
Hope this helps.
Check the ExcelPackage project, it uses the Office Open XML file format of Excel 2007, it's lightweight and open source...
I've tried CSharpJExcel and wouldn't recommend it, at least not until there is some documentation available. Contrary to the developers comments it is not a straight native port.
I know this is quite late, but I feel compelled to answer xPorter (writing) and xlReader (reading) from xPortTools.Net. We tested quite a few libraries and nothing came close in the way of performance (I'm talking about writing millions of rows in seconds here). Can't say enough good things about these products!
You can use Microsoft.Jet.OLEDB.4.0
We have just identified a similar need. And I think it's important to consider the user experience.
We nearly got sidetracked along the same:
Prepare/work in spreadsheet file
Save file
Import file
Work with data in system
... workflow
Add-in Express allows you to create a button within Excel without all that tedious mucking about with VSTO. Then the workflow becomes:
Prepare/work in spreadsheet file
Import file (using button inside Excel)
Work with data in system
Have the code behind the button use the "native" Excel API (via Add-in Express) and push direct into the recipient system. You can't get much more transparent for the developer or the user. Worth considering.
There's a pretty good article and library on CodeProject by Yogesh Jagota:
Excel XML Import-Export Library
I've used it to export data from SQL queries and other data sources to Excel - works just fine for me.
Cheers
You could try the following library, it is easy enough and it is just a light wrapper over Microsoft's Open XML SDK (you can even reuse formatting, styles and even entire worksheets from secondary Excel file) :
http://officehelper.codeplex.com
Spreadsheetgear is the best commercial library we have found and are using. Our company does a lot of advanced excel import and export and Spreadsheetgear supports lots of advanced excel features far beyond anything you can do with simple CSV, and it's fast. It isn't free or very cheap though but worth it because the support is excellent. The developers will actually respond to you if you run into an issue.
How about the apache POI java library. I havent used it for Excel , but did use it for Word 2007.