Set SpreadsheetGear workBookSet to an already open excel file - c#

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.

Related

ActiveWorkbook in NPOI

I want to edit the current active Excel-Worksheet in NPOI.
The Codesample below Shows what I want to do. Problem is I would need it in NPOI and so I am asking whether any of you can help me as I can't find anything.
Microsoft.Office.Interop.Excel.Application excel = (Microsoft.Office.Interop.Excel.Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Excel.Application");
Microsoft.Office.Interop.Excel._Workbook wb = excel.ActiveWorkbook;
NPOI is not a wrapper around Office, it implements it's own access to the files. As such, it cannot be used to get the "Active" instance of Excel. It never directly interops with Excel in the first place. That's one of the features that makes it a powerful library, it doesn't depend on Office being installed and can therefore run on a server in an unattended way.
In case you want to interact directly with Excel, you need to use the Microsoft Primary Interop Assemblies, which will work just fine with the statements you referenced in your Question.

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.

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.

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