Reading Excel Files in C# - 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.

Related

Set SpreadsheetGear workBookSet to an already open excel file

I am trying to use SpreadsheetGear to access an excel file that is downloaded and opened from a website. I haven't been able to find a way to set an already open and active excel file to a SpreadsheetGear workbook. Saving and then opening from memory is not an option in this circumstance.
I have been using this code to access the application before I started working with SpreadsheetGear:
xl.Application excelApp = (xl.Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Excel.Application");
However, none of the interfaces I've found for spreadsheetGear are compatible with that variable.
This is not possible with SpreadsheetGear for .NET, as it does not interact with Microsoft Excel in any way (it's a totally separate product, built from the ground up using only the .NET Framework and has no dependencies on Microsoft Excel).
To open a file in SpreadsheetGear, you'll need to use one of the following methods to do so...
SpreadsheetGear.Factory.GetWorbook(pathToFile)
SpreadsheetGear.IWorkbooks.Open(pathToFile) / OpenFromMemory(byteArray) / OpenFromStream(streamObj)
...which means you'll need to save the file from Excel first.

Merging two excel files in c# without using interop

I have to merge two excel files containing one sheet in each of them and I have to generate a third file containing two sheets corresponding to the two original sheets.
This task can be done using "interop" and the code works but when the same code is run in a system that does not contain MS Office, the process fails and an error comes up.
Can you please guide me as to what dll files to be included or how this merging could be done without using interop?
Thanks in advance.
From what I've experienced, there is unfortunately no framework way of doing this (without writing your own excel file reader). I happened across this interesting library which does just that.
http://exceldatareader.codeplex.com/
So far it has worked for our needs and requires no interop.
You should use an external component to work with excel files. I use the syncfusion xslIo.
If you only have raw data (no formulas, etc) you could also just save the files using the XML Spreadsheet 2003 (*.xml) format (its very easy to read) and process the data using standard XML tools.

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!

Best tools to create valid XML files from an Excel file

I need to create a script that extracts some data from a complex Excel 2003 file (with multiple sheets and different tables inside a single sheet) and produces different XML files that need to be validated against a given XSD file.
My preferred language is Python;
to create and validate XML files i would go with lxml.
What do you suggest for parsing XLS files?
Is xlrd the right tool to use for complex Excel files?
Or do i need to convert all the sheets in CSV manually, and read files line by line, splitting and getting data?
I accept C#, VB6, VBA suggestions too.
[disclaimer: I'm the author of xlrd]
xlrd is quite suited for this kind of job. Get the latest version from PyPI. Get the flavour from the tutorial found here. XLSX support is in alpha test; e-mail me if you need it. The awkwardness and lossiness of the save-as-CSV approach was one of the things that prompted me to write xlrd.
Xlrd is OK. We use it extensively to import XLS files full of references and formulas with multiple sheets and data presented in custom (not Latin-1) encoding.
I am convinced the most simple solution for this task is using Excel VBA together with MSXML parser. Look here for some links how to use the MSXML parser in VBA for reading XML files; you can adopt this easily for writing XML files, I think.
I cant answer whether xlrd/python is the right tool for the job - as I don't know python well enough.
But there are many ways to access the excel data...in the main you have VBA built directly in to Excel.
Then you have Ado.net See David Hayden's article here which allows you to access the data via any DotNet language...even IronPython

Dynamic Excel Spreadsheets

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/

Categories