Dynamic Excel Spreadsheets - c#

All,
In the past I created a lot of dynamic Excel (2003) spreadsheets in the following ways:
1.Using Excel COM object,
2.XML representation of spreadsheets,
3.SyncFusion controls for creating Excel Spreadsheets
All the above generated from C#.NET Code
Now my question is what are the best tehcnologies for creating Excel 2007 spreadsheets?
Has anything changed ? I know Excel 2007 is much more advanced than 2003.
Please note my needs have changed now and I need to create even more complex spreadsheets that include pivots etc.
Again I want to create these new spreadsheets using .NET Framework.
Thanks,
Marios

Because 2007 Office files are XML packages, they are created as Packages with FW 3.0.
See http://support.microsoft.com/kb/931866.

Take a look at this question on StackOverflow:
Create Excel (.XLS and .XLSX) file from C#
I think ExcelPackage can be a fit when it comes to Excel 2007.

I can recommend the SpreadsheetGear library - it is commercial but a lot faster than the COM approach.
Another benefit: no dependencies on the client machine, you could use e.g. OpenOffice as viewer
http://www.spreadsheetgear.com/

Related

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 to create excel in my c# application if msoffice is not installed

Hi I am creating an application through which i need to create an excel. I have added Microsoft Excel 12.Object Library as Reference to application. But my server didnt get Msoffice Installed in it. So how can i create Excel in that server.
I can recommend EPPlus because it's simple, powerful and works without having office/excel being installed with Excel 2007 spreadsheets(xlsx-files). It's license model is LGPL.
var excel = new ExcelPackage();
excel.File = new System.IO.FileInfo(#"C:\Temp\AnExcelFile.xlsx");
if (excel.File.Exists)
excel.Load(excel.File.Open(FileMode.Open));
ExcelWorksheet ws = excel.Workbook.Worksheets.Add("Worksheet-Name");//must be unique and less than 31 characters long
ws.Cells[26, 1].LoadFromDataTable(dt, true); //loading from DataTable, the 2.Parameter is PrintHeaders
ws.Cells[26, 1].LoadFromCollection(query, true); //loading by LINQ-Query also possible
excel.Save();
If you need to create new office format documents known as openxml, you can see at http://www.microsoft.com/en-us/download/details.aspx?id=5124
There are a number of third party libraries - I think ComponentOne makes one, for instance - which are capable of creating Excel files, and I think at least some of them are independent of Excel, so they can make Excel files without having Excel installed on the server.
An alternate solution would be to create some other format that Excel can read, for instance CSV. Using CSV will mean that you don't get the fancy formatting provided by Excel, but at least you don't need to license a third party component or install Excel on the server.
There is now way to create ms objects, while MS Office is not installed.
Added library is nothing more than interfaces wrapper.

Excel 2007 Addin - Technology options

We have a number of .xla/.xlam Excel Addins and the time has come to migrate to something easier to version control and maintain.
I'd like to write in C# if possible and the Addins will need to do the following sorts of things:-
Provide User Defined Functions to Excel
Create and manipulate named ranges in the Excel sheet
Pull data from external sources and populate cells in the Excel sheet
Currently all this is possible and simple to do with a .xlam what are the pros/cons of moving to VSTO or creating a C# Addin (I notice VS2010 has a New Project option of creating an Excel 2007 Addin).
Are there any good sources of documentation?
Thanks
Dave
I would recommend you seriously look at Excel DNA (Free) or Addin Express (chargeable). Both provide .Net functions via the .XLL interface together with .COM to .NET interface if you need it.
Performance of both of these makes VSTO look like molasses, and installation is realtively simple.
For easy creation of performing UDFs XLDNA is VERY hard to beat.

Reading Excel Files in C#

How Can I open and read all data in excel file to perform some operations on say write them to a database ...
You can automate Excel using com automation http://support.microsoft.com/kb/302096 , but if you are on a web server you will need to use a third party library like http://sourceforge.net/projects/koogra/
You can use the default library that comes with the .NET framework in order to use the Excel.Application Object and therefore the Workbook and Worksheets objects, so you can access Excel files, read or manipulate them
You can add it to your project by using the Add Reference option, the library is called
Microsoft.Office.Interop.Excel
Hope this helps
Assuming that the Excel files are in a table format, I'd suggest that the best way would be using OleDB. This page has a very basic sample that should show you how to get started.
If you're unable to use OleDB for some reason, then you could use Excel Automation. This is not recommended if it's on a server though and will in general be slower and less stable than OleDB, but you will be able to do pretty much anything you need.
There are several ways:
If *.xslx (the new XML based format) is used, you can open that file and read the XML File
You can read it with Excel COM Interop (not recomended on a Server!)
You can use a ODBC Data Source
Starting with Office 2007, you can use OpenXML to query/manipulate office documents. The .xlsx files (all Office .???x files) are zipped up XML files.
ExcelToEnumerable is a great solution if you want to map Excel data to a list of classes, e.g:
var filePath = "/Path/To/ExcelFile.xlsx";
IEnumerable<MyClass> myClasses = filePath.ExcelToEnumerable<MyClass>();
Disclaimer. I am the author of ExcelToEnumerable.

Export object to excel using C#

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!

Categories