import openOffice calc in reference in c# - c#

I want to import an excel file into c#. But problem is, when I want to add reference, so I can use these commands
using Microsoft.Office.Interoop;
using Excel=Microsoft.Office.Interop.Excel;
there is no such reference.

If you don't want to use (or don't have) Excel on your machine, use OpenXML for reading Excel content (only new format, xlsx and xlsm). It is a bit more complicated for first look but small and very fast.

Related

Strip Excel file of Macros with C#

I've been asked to strip an Excel file of macros, leaving only the data. I've been asked to do this by converting the Excel file to XML and then reading that file back into Excel using C#. This seems a bit inefficient to me and I was thinking that it would be easier to simply load the source Excel file into C# and then create a new target Excel file and add the sheets from the source back into the target.
I don't know where macros live inside an Excel file, so I'm not sure if this would accomplish the task or not. So, will this work? Will simply copying the sheets from one file to another strip it of it's macros or are they actually stored at the worksheet level?
As always, any and all suggestions are welcome, including alternate suggestions or even "why are you even doing this???". :)
To do this programmatically, you can use the ZipFile class from the System.IO.Compression library in .NET from C#. (.NET Framework 4.5)
Rename the file to add a ".zip" extension, and then open the file as a ZIP archive. Look for an element in the resultant "xl" folder called "vbproject.bin", and delete it. Remove the .zip extension. Macros gone.
Your best bet is to save the workbook as an xlsx, close it, open it, then save as a format of your choice.
This will strip the macros and is robust. It will also work if the VBA is locked for viewing.
Closing and reopening the workbook is necessary otherwise the macros are retained.
If you're needing to use C# to do this, I agree that it would be easier to load the source Excel file into C# and create a new target file only copying over the cells and sheets you need. Especially if you're doing this for a large amount of excel files I would recommend just creating a small console app that, when given an excel sheet, will automatically generate a new excel sheet with just the data for you.
One tool that I've found extremely useful and easy to use for such tasks is EPPlus.

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.

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

How to inject VBA code into Excel .xlsm without using Interop?

My goal is to add macros to an excel workbook without having to enable "Trust Access to the VBA Project Object Module" in the Excel Trust Center. (Enabling access seems a security risk).
Found random pieces of puzzle on my initial fact finding search:
-I see that VBA script is stored in the zipped .xlsm file as vbaProject.bin.
-There are several free/commercial resources that can work with excel files:
Create excel without interop and template with or without row and columnspan
It would be nice to simply have a file of VBA script in the C# project that the C# code pulls from and injects into the Excel document without VBA interop. Any quick/fast/simple/straightforward way to do this or should I play around with the free/commercial resources linked to above?
Using OpenXML SDK 2.0:
Create your macro code and save it in .xlsm format, say snorehorse.xlsm.
Open snorehorse.xlsm in the OpenXML Productivity Toolkit and do a Reflect Code on the tree root.
Find the macro's binary code. It's in a string format and looks like random characters.
In your IDE, add a reference to OpenXML SDK, and programmatically create or open the excel file you want to inject the macro code into.
Copy the macro string found in step #3 into your code.
Add a new vba part to the destination.
Feed the string data into the new vba part.
Save and run and be pleased you bypassed the Trust Center.
Example code:
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Spreadsheet;
private string partData = "...";
public void vbaInjector{
[code to create / open spreadsheet using OpenXML omitted]
VbaProjectPart vbaProjectPart1 = snoreSpreadsheetDoc.WorkbookPart.AddNewPart<VbaProjectPart>("rId8");
System.IO.Stream data = GetBinaryDataStream(partData);
vbaProjectPart1.FeedData(data);
data.Close();
[code to close spreadsheet and cleanup omitted]
}
private System.IO.Stream GetBinaryDataStream(string base64String)
{
return new System.IO.MemoryStream(System.Convert.FromBase64String(base64String));
}
I chose to add the OpenXML SDK dll into the project's local build so the end users won't have to install the SDK themselves.
I think this can be done on a lower level, working with the XML, without using the OpenXML SDK, but I haven't attempted to learn how to do this. If anyone can post the code, I'll accept that Answer over mine.
Also, if one had a programmatic way to convert VBA script, in an embedded resource file, into a binary string of the format excel expects, one could bypass having to copy and paste in a new string of binary data every time you wanted to change the macro code. That would be a superior answer to mine.
Thanks.
If you use EPPlus, you can now include VBA programmatically. See here:
Writing and Executing VBA Macros on Excel without using Excel.Interop

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.

Categories