Merging two excel files in c# without using interop - c#

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.

Related

How to update data in excel File using C#?

This Question might be repeated, But I couldn't get solution regarding my problem so far. I'm new to Interop. I'm using excel file (as a database).
Here is data presentation in excel file
in my data If Card ID repeated then I need to increment '1' in Counter in the same row, similarly I need to fetch IP address of same row..
I'm using Interop Excel approach to insert data in excel file..
Kindly tell me how can I perform that update operation to that excel file through C# (WPF)
Sorry for bad English..
Thanks
I recommend using Closed XML
You write to the file directly and don't need Excel. It will need to be the latest version of an Excel file to work (The open xml standard).
Epplus.dll or npoi.dll will also read/write to excel files w/o excel.
Save the data in an XML or JSON file, then when you want to visualize them you create the excel file from these data, so you will have a very light file and easy to read and update if you wish.
I haven't done this specifically through wpf, but you can access powershell cmdlets through .net and powershell has commands for retrieving and writing Excel data.
That said, my experience has been it's very tedious and inconsistent with bugs. I would tell your client that using an Excel file as a database is impossible and certainly prone to failure in practice.
For one thing you will run into read/write restrictions if it is used by anything else.
If you don't mind to use comercial libraries, you can try to use Aspose.Cells. It has rich cells API and able to work without Excel interop API.

How to do Importing and exporting excel using C# and WPF?

I have an Excel file which is generated by a machine. I need to create an application using WPF and C# which can import the excel to see its contents, do some calculations and create some new columns and save back as new excel file. Which is the way to go?
I recommend using Microsoft's Open XML library (as it doesn't require Excel to be installed).
There are various 3rd party libraries that sit on top of it to make it easier to use; such as ClosedXML or Simple OOXML.
Consider ODBC Excel drivers. If your input file is built by machine AND you do not want anything fancy as output, this should work just fine.
You can try Spreadsheet control to open excel http://www.syncfusion.com/products/user-interface-edition/wpf/spreadsheet
Sample location: http://silverlight.syncfusion.com/samples/WPF/Samples/WPFSampleBrowser/UI/Spreadsheet/Spreadsheet.htm

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!

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

Categories