Import and Export Excel - What is the best library?

201,868

Solution 1

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

Solution 2

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.

Solution 3

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.

Solution 4

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

Solution 5

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

Share:
201,868

Related videos on Youtube

Jason Kealey
Author by

Jason Kealey

Founder, LavaBlast Software - software for the franchise industry.

Updated on March 07, 2021

Comments

  • Jason Kealey
    Jason Kealey about 3 years

    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.

    • Ken
      Ken over 14 years
      "Given Excel 2007 is an xml-based (and zipped) file format, I am guessing this kind of library should be easy to find" -- hah! That's like saying "Excel 97 is byte-based, so this kind of library should be easy to find". XML doesn't imply simplicity, and OOXML is the poster child for how to make XML underspecified and indecipherably complex. :-)
    • shen
      shen over 13 years
      Actually you not entirely correct in making that assumption. There are tools available that make building a well formed OOXML document at least for XLSX, very easy to do with C#.NET.
    • daviewales
      daviewales about 10 years
      I have created a site proposal to give questions like this an official home away from Stack Overflow. It's called Code Recommendations Help make it a reality by joining and asking questions now!
    • DotNet Programmer
      DotNet Programmer about 8 years
      What about Spire.xls. It is only $800 for the Pro Edition. You get all your conversions as well as your excel import or exporter. e-iceblue.com/Introduce/…
    • NixonUposseen
      NixonUposseen over 4 years
      GemBox.Spreadsheet is another solution that's worth checking out, super fast and has both free and pro versions. Also, the pro version is rather cheap and doesn't have any hidden crappy costs for deployment like most of them.
  • Jason Kealey
    Jason Kealey over 15 years
    Looked good but one comment says it is bad with large files (my scenario)
  • Jason Kealey
    Jason Kealey over 15 years
    Interesting, but requires XML files. Can't read/write xls or xlsx files.
  • Jason Kealey
    Jason Kealey over 15 years
    I agree, but in my case it would be good to support xls(x) files directly. However, do you know of any csv <-> xls(x) tools?
  • Jason Kealey
    Jason Kealey over 15 years
    I can't see that this supports Excel 2007 (xlsx). As xls only supports 64k rows, this is a limitation for me.
  • DucDigital
    DucDigital over 15 years
    No, I've never needed to do xls. Sorry :-/
  • Jason Kealey
    Jason Kealey about 15 years
    Very interesting! Have you tested it using large quantities of data?
  • cdonner
    cdonner about 15 years
    I have not done any performance test. I will mostly charts and and single-page reports, so throughput is not an issue for me. It appears to be as fast as managed code can be, though.
  • Igor Brejc
    Igor Brejc about 15 years
    I tried ExcelPackage, but had to abandon it - it fails when you try put single quotes (') in a cell.
  • Igor Brejc
    Igor Brejc about 15 years
    I tried CSV approach too, but there are several issues with it. For example, what if you want to have a multi-line text in a cell? I couldn't make Excel import such a CSV.
  • Chris
    Chris over 14 years
    Actually, you can have mult-line text in a CSV file. Just try this as your file contents: cell 1,"this\nis\nmultiline",cell 3
  • Pauk
    Pauk about 14 years
    @Jason Kealey - Flexcel does now support Excel 2007 and 2010.
  • Todd Main
    Todd Main about 14 years
    @Jason Kealey: this is really the best answer on this post - export becomes irrelevant with SpreadsheetML. The data is all accessible from within the file. If you need the data in a different format, provide a transform via an XSLT or via Linq.
  • John Scipione
    John Scipione almost 14 years
    CSV has its place but the poster asked about Excel, I assume that he must want Excel, not CSV.
  • NotMe
    NotMe over 13 years
    CSV falls down when exporting columns like 0345. Excel automatically trims this out to 345. Which is not at all helpful when that leading digit is important.
  • Dillie-O
    Dillie-O over 13 years
    I used this library the other day looking into this issue. It is an excellent library!
  • marc_s
    marc_s over 13 years
    Works fine, but it's licensed under GPL - e.g. any software using it must be available in plain source code, too.... not always a good choice...
  • Soner Gönül
    Soner Gönül over 13 years
    i don't think so it's running!!
  • Oskar Duveborn
    Oskar Duveborn over 13 years
    HTML table with an excel file extension seems to work quite well... it will parse some CSS in a peculiar way for stuff like formatting multiple lines, colors and so forth - without actually having to create a native Excel file
  • sam1132
    sam1132 about 13 years
    "Save as"? No. If the customer is using Excel, then why should they save off a second, much more limited file to interact with your product?
  • Admin
    Admin over 12 years
    Export in a native format that is convenient to you, write a prog. that uses EPPlus to convert to Excel, make that free. Make your main prog use that as default, but allow other "plugins", hey presto your real code is free from the GPL.
  • Jagd
    Jagd over 12 years
    I love the irony in the name...
  • Brad R
    Brad R about 12 years
    It appears that this is now licensed under the LGPL, so you are fine to use it as a linked library without the copyleft restrictions.
  • Sanjay Goswami
    Sanjay Goswami about 12 years
    MS office is not required for "Microsoft.Jet.OLEDB.4.0"( for xls) "Microsoft.ACE.OLEDB.12.0 for xlsx". you have to use only Drivers , so no chance to run Excel on server @Jason Kealey
  • quillbreaker
    quillbreaker almost 12 years
    This doesn't work for ".xls" files at all, though.
  • AnthonyVO
    AnthonyVO almost 12 years
    Used it as well and same experience. Works well and is very flexible.
  • philw
    philw over 11 years
    I used this for XLS (not the original requirement), and for completeness... There are some important issues: (1) It's 32-bit only, so you will have to set IIS to allow that. (2) It is dreadfully slow on export. (3) It has poorly documented row and column limits. (4) It insists on "type sniffing" imported columns unless you have registry access, and it generally gets it wrong.
  • aron.sinoai
    aron.sinoai over 11 years
    New release of the library was recently published.
  • phuclv
    phuclv over 10 years
    For anything that should be preserved as text, simply put an ' (apostrophe) at the beginning
  • Kevin Laity
    Kevin Laity over 10 years
    Note that if you need to produce an excel file that has big strings in it, this library has a tendency to randomly produce 'unreadable content' errors in excel.
  • Christian Sauer
    Christian Sauer over 9 years
    Another fun fact: I cannot open a a comma seperated file in a lot of locales, like german. Which makes csv a poor format to share data with international contacts
  • Alex from Jitbit
    Alex from Jitbit over 9 years
    github.com/OfficeDev/Open-XML-SDK it's now opensource and github-hosted (I like where MS is headed recently)
  • user3285954
    user3285954 almost 9 years
    csv is no replacement to Excel/Open Office native format in many ways. I.e. number formatting, cell styling, charts, formulas are not possible. Mutiple sheets in a workbook are also not possible to implement with csv. csv works for very simple use cases but fails in many cases.
  • user3285954
    user3285954 almost 9 years
    Beware that EPPlus leaks memory, isn't really good for large amounts of data.
  • user3285954
    user3285954 almost 9 years
    Open XML SDK has a forward only writer API (similar to XmlWriter) what is fast and uses very little memory so is perfect for large amounts of data. All libraries what follow a DOM-like approach create the xlsx file in memory so if you have to export many rows they will kill your app servers - might not be an issue on standalone desktop applications. blogs.msdn.com/b/brian_jones/archive/2010/06/22/… And yes, like the name says it is for xlsx only, no xls. But the xlsx adapter is free for Excel and is downloaded automatically.
  • AnkitSablok
    AnkitSablok over 8 years
    @Joe Erickson : Can you tell as to how does one go about reading a CSV and then producing an XML out of the CSV just read using Spreadsheet gear and using that XLS producing a resultant XML file which contains that structure? Or can we use Spreadsheetgear to produce an XML from a CSV directly?
  • Hazel Patton
    Hazel Patton over 4 years
    Both links no longer work... but I did find some snippets of it and it looks like it's more Java friendly, not so much for .NET developers. However, I did find another library that's opposite, it is a native .NET library (GemBox.Spreadsheet), that was also ported to Java (GemBox.Spreadsheet for Java).
  • Lynn
    Lynn over 2 years
    This link is dead.