Exporting dataset to Excel file with multiple sheets in ASP.NET - c#

In C# ASP.NET 3.5 web application, I need to export multiple datatables (or a dataset) to an Excel 2007 file with multiple sheets, and then provide the user with 'Open/Save' dialog box, WITHOUT saving the Excel file on the web server.
I have used Excel Interop before. I have been reading that it's not efficient and is not the best approach to achieve this and there are more ways to do it, 2 of them being: 1) Converting data in datatables to an XML string that Excel understands 2) Using OPEN XML SDK 2.0.
It looks like OPEN XML SDK 2.0 is better, please let me know. Are there any other ways to do it? I don't want to use any third-party tools.
If I use OPEN XML SDK, it creates an excel file, right? I don't want to save it on the (Windows 2003) server hard drive (I don't want to use Server.MapPath, these Excel files are dynamically created, and they are not required on the server, once client gets them). I directly want to prompt the user to open/save it. I know how to do it when the 'XML string' approach is used.
Please help.
Thank you.

Is Excel 2007 support absolutely required?
We have used NPOI with great success, and it supports all the features we want (multiple worksheets, formatting, formulas). It is also pretty stable.
The files it produces are in Excel 2003 format though, so they are binary, not OOXML.
This question has been asked before, see here for a better discussion.

You can easily stream the xml response to the user as an XML Excel file.
Any Page:
Open excel Report
Report.aspx:
Response.Clear();
Response.ContentType = "application/vnd.ms-excel";
Response.AddHeader("Content-Disposition"
, "attachment;filename=" & _fileName & ".xml");
Response.Write("<?xml version=""1.0""?>");
Response.Write(excelXML);

I too had come across similar requirement to export dataset into excel. I used this open source library. It's based on Open XML standards and doesn't use Office Interop. It met my requirement. But, my requirement was basic. So, check if it fulfills your requirement.

Related

Manipulate .ods (OpenOffice) file by using c#

We have one existing ODS file (Open office spreadsheet) comprise of multiple sheets. I am intending to read the data from each sheet on want to update some cells on each sheet using c#
I have googled a lot but not able to find any perfect solutions. I went through open office website also but got no success.
Can any one please name some free api or sample code
Alternate approach
Open the file with Open Office, save it as an XLS or XLSX file and then use one of the Excel editing libraries out there.
Open Office should be capable of reading/writing the XLS or XLSX files.
I had no issues editing these formats with EPPlus so far, but there are also other libraries.
If anyone else is reading now, you can manipulate ODS files as XMLs as seen here. There are NuGet packages available for easier manipulation as well, but this is the bare-bones approach

C# - delete Excel-row wihout using the Interop namespace

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

How can I unzip Office Open XML file in C# and modify its data and re-zip again?

From a console application written in C#, how can I :
extract an Office Open XML file,
Obtain the data part of it modify
the data and re-zip it again
My motivation is to save an excel file with the formats and use it to populate cells via a console application.
Is this possible to achieve, do I need a specific library to that provides Excel files manipulation (unzipping it, modifying it etc.)
For the zip-unzip part i think you can find easily many examples here.
To edit the excel file, I'd suggest you to have a look at Open XML SDK. With it, you can easily edit office files programmatically.
Hope it helps

How do I generate a PDF/Excel file from an SQL database using C# and MVC 2?

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.

Transforming Excel 2010 documents?

I am interested in writing an application that will take in an excel document of a specific format, massage the data and create a new Excel document that has different formatting.
I am curious if anyone can recommend a good place to start on this.
My first thought was to write something my self in C#. I came across this tool on codeplex:
http://excelwrapperdotnet.codeplex.com/wikipage?title=Usage%20-%20Example&referringTitle=Documentation
But it appears to only be for Excel 2007.
Is there a best practice for doing this type of thing for Excel 2010 documents? Do I even need to program something custom to do this or does Excel offer something that might handle this?
Another nice library to modify Excel 2007/2010 documents (.xlsx) is EPPlus. It gives you a nice object model on your spreadsheets.
Excel files (.xslx) are archived XML files. They use 'Open XML', take a look here MICROSOFT Open XML
That should get you going on the right path.

Categories