I have a windows app that opens excel. The user can then decide to save it.
This uses Interop and is very slow when we have over 100 rows. I'm experimenting with EPPlus and see how to save directly to a file. But I don't want to do that, I need to open Excel from the app. So I'm thinking to write out the worksheet using EPPlus (thus saving time) and then copying the whole worksheet into Excel and open it. Does that make sense? Are there examples of this?
First off, I've used EPPlus and it's a great tool.
I'm a little confused by your question, so let me ask some clarifying questions.
Why are you launching excel from your app? Is it to edit some kind of file your program generates, or just random files the user wants to edit?
Once the user is finished editing the file, do you need to read it back into your app? If so, that can also be done with EPPlus.
Once I know more about what exactly you're trying to do, I'll be glad to help you.
--John
I am developing an application which must produce a complex well-formatted Excel report. I have previously successfully used OpenXML for filling in PowerPoint and Word templates by first reflecting code with OpenXML Productivity Tool and then passing a model into the reflected code and making the required changes.
I have noticed that some people recommend using ClosedXML and NPOI (e.g. here http://odetocode.com/blogs/scott/archive/2014/07/30/easily-generate-microsoft-office-files-from-c.aspx) for Excel. I've started researching ClosedXML and indeed it seems like a nice solution for creating new Excel files, but I would prefer to fill out the template I already have as it would take a month to write it from scratch.
Is it possible to reflect Excel files into ClosedXML code similarly to like it's done with OpenXML Productivity Tool?
Do you have any examples of ClosedXML code which creates complex Excel files with multiple sheets, thousands of rows, some charts and advanced formatting?
It's quite time-consuming to edit OpenXML code when any significant changes needed. What do you think would be the best technology to use for my task considering also time spent on editing?
Any other tips and tricks on manipulating Excel with C# are appreciated! Thank you for your answers.
ClosedXML answers:
You can't reflect code is with OpenXML, but you can load an Excel template and populate it in ClosedXML.
See the ClosedXML Wiki at https://github.com/ClosedXML/ClosedXML/wiki for examples.
I need to make the information in the database usable by allowing the user to download it as a PDF or Excel spreadsheet (either one works, both is perfect).
I've looked around at a bunch of options, but I really can't decide which one I should use, let alone if any of those options are actually useful. Most of the options I've found revolve around converting already existing HTML files into PDFs which is not what I need. Also, it needs to be free. My bosses haven't given me a budget to spend on this
I'm not sure what other information I should include here.
Well, any help is greatly appreciated. If you have questions about missing information, I'll get it posted ASAP. I'm here all day, so I'll be able to respond to any comments very quickly.
EDIT: Oh wow! Huge thanks, guys, for the massive response! I got a ton of ideas. This is super-helpful. Thanks!
if you want to generate an Excel (or also a Word) you can use openXml. You can create a new document exactly the way you want from pure code.
OpenXml SDK page
The solution I usually propose to my clients in this situation is to use Sql Server Reporting Services (SSRS). You can use the ReportViewer control included with it in order to generate PDF's, Excel spreadsheets, XML files, CSV files, and others. If you need ad hoc reporting, there is a Report Builder available as well.
Barring that, you can use OpenXml to generate Excel spreadsheets and there are a host of PDF toolkits available.
Have you looked into the reportviewer control, which is part of Visual Studio?
It allows you to export the report in PDF or Excel format.
http://www.carlosag.net/tools/excelxmlwriter/sample
check this might be useful for you
There are lots of reporting solutions out there such as SQL Server Reporting Services(for which you might already have a license). Take a look at Reporting (free || open source) Alternatives to Crystal Reports in Winforms which can likely be applied to the web with a bit of serialization.
I would suggest thinking about rolling your own depending on the situation. You could use pdfsharp for the pdf export and EPPlus for excel. They are both very easy to use and, I'm pretty sure, available in nuget with a couple of clicks.
If you want to go the Excel route, i'd recommend this article from Stephen Walther entitled ASP.NET MVC Tip #2 - Create a custom Action Result that returns Microsoft Excel Documents. This uses an old trick of writing an HTML document with an Excel mime type. This is different than streaming a native Excel file. And it's fairly easy to change the to rendering a CSV file if you want to strip it down, and make it a more universal file. Just remember to double-quote all the fields if there's a possibility of commas showing up.
If what your doing isn't too complicated you can use CSV files. CSV stands for comma separated values, and it is what it sounds like. You can create simple tables and columns using commas. For example paste the following lines into a text file:
heading1,heading2,heading3
info1,info2,info3
info1,info2,info3
Save the text file as a .csv file and voila - an excel spreadsheet. Obviously it is extremely easy to build these looping object collections. Mind you if you need any complicated text formatting etc then it is not really the best option.
I want to export my data from grid view to the excel. Unfortunately i came to know that there is no excel software on server side. Is there any way that i can still export data in excel format without having excel software on server side.
Thanks.
you can create a CSV format text file, which excel can understand.
This question also outlines some other options you might be able to use, with pros and cons of both.
This must be one of the most asked questions on Stackoverflow.
See Create Excel (.XLS and .XLSX) file from C# For lots of suggestions and a whole list of linked questions.
One trick you can use it to export the html table to the client with the extension of .xls. When the user opens the document with Excel and resaves it will become a real excel document. The fun part is it will retain most of the formatting from the HTML view. This works with HTML/TABLES, I'm not sure about DIV/SPAN/CSS.
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.