We use an application which uses Crystal Reports to display some reports.
The application also has a button which exports the report to Microsoft Excel. However, this does only work if Excel is installed on the same machine as the program is executed.
We cant install Excel on the server for licensing reasons.
From the error message Error 429: ActiveX component can't create object I assume that the application is using COM/ActiveX to communicate with Excel.
Is it possible to write a C# program to emulate the COM interface of excel?
The Crystal runtime can export to excel without any need for Excel to be installed. This means the application you use is probably doing something extra with Excel. Consider contacting the company that developed the software and ask them to address your question.
Related
I have created a merge excel program in my application using Microsoft.Office.Interop.Excel. But when deployed on IIS server I am getting error. Regarding that error I searched many articles and tried but got no successfs. So please can you verify this?
It is required.
But, you should not use Office interop on a server. The problem is that Office apps require a graphical user session to work properly. On the other hand, Windows service apps and IIS run without one. This is officially not supported and not recommended. If you do, you will encounter random crashes and resource leaks.
If you want to generate or modify Excel files, use OpenXML SDK.
EDIT: You may want to use Spreadsheet Gear. I can recommend it from personal experience in a web-app that manipulated existing Excel sheets with complex formulas.
I have had previous experience working with an Excel Add-In vsto COM object. Specifically using the Excel.Interop dll to create a worksheet and populate it with data. As well as, interact with the worksheet by using the worksheet change events to update data, thru c#. I wanted to explore Excel DNA and possibly use it. I am completely new to excel dna, but while researching it I only found examples of creating functions and a ribbon with buttons, but I haven't seen a way to create a worksheet and populate it from a datatable.
Is Excel DNA only used for the creation of the excel add in and if I wanted to create a worksheet, populate it with data and have events I either need to use excel.interop dll or OpenXML?
Excel-DNA is meant to allow you to run .NET code from within Excel. If you want, for example, to have a custom Ribbon in Excel, with a button that, when clicked, will run some .NET code to create a new Workbook and populate some data, etc. then yes... Excel-DNA is a great tool for that.
If you want to create Excel files outside of Excel, for example, in a Console App or Windows Service, then Excel-DNA is not the right tool for that, and you should look at using the Excel.Interop if you know your app will run on a machine with Excel installed, or other alternatives such as ClosedXml and other OpenXml-compatible tools, that will generate Excel files without requiring Excel installed on the machine.
You have full access to the Excel COM Object Model from within your Excel-DNA add-in. One important step is that you have to get hold of the correct Application root object for the Excel instance that is hosting your add-in. (Just calling new Application() might get hold of another Excel instance.) To get hold of the Application object you call ExcelDnaUtil.Application - that return the COM object.
From there you can use the dynamic support in C# to talk to the object model. But better is to reference the Interop assemblies, giving you IntelliSense and early-binding.
A convenient way of referencing a set of interop assemblies (corresponding to the Excel 2010 object model) is to install the ExcelDna.Interop package from NuGet. With the 'Embed Interop Types' feature in .NET 4 (which is set true by default), you need not redistribute anything special and your code will be compatible with all Excel versions, as long as the object model parts you use are supported there.
As an entry point into running the COM code, you can make a macro, shortcut ribbon or context menu. From the object model you could also hook COM events.
A simple example with detailed instructions for making a Ribbon button that then runs some COM code is available on GitHub.
Note that the VSTO wrappers on top of the COM object model (everything in a Microsoft.Office.Tools.Excel namespace) are not compatible with your Excel-DNA add-in, so you'd have to implement that functionality yourself based on the native COM object model (the types in the Microsoft.Office.Interop.Excel namespace.)
I am trying to insert and Excel object into and Autocad file (DWG), I need to do this by running some sort of script that will open the DWG file, import he Excel Sheet and then save the DWG file.
I need to do this using a web based programming language eg PHP, C#, Python etc.
If anyone could please point me in the right direction or even confirm that this can be done that would be a great help.
Similar question here:
https://stackoverflow.com/a/23471957/474702
It is not advisable to run instances of applications that expect direct user interaction from web based software, particularly if you expect the AutoCAD interop to be hosted on the server.
You may have to use a windows form or WPF application to work with AutoCAD. With this option, you can use C# or visual basic.Net. I have worked extensively with the AutoCAD interop for creating drawings, complete with BOMs.
AutoCAD.Application interop may not allow for non-visual interaction.
Is it possible to 'drive' excel via a c# application (i.e. select something from a excel gui dropdown list, press a button and read the content of particular cells)?
This is not really for testing but for data scrapping similarly to selenium where you can drive a browser via C#.
Is it possible to 'drive' excel via a c# application (i.e. select something from a excel gui dropdown list, press a button and read the content of particular cells)?
Yes, you can use COM Automation from C#. Create a C# project and add a reference in the COM section of the dialog. You should reference Microsoft Excel 14.0 Object Library. This is the Excel 2010 version and if you use another version you should reference the version you have installed on your machine.
You can then automate Excel in a similar way as you would do using VBA except you now are using C#. On MSDN you have Getting Started with VBA in Excel 2010 that among other things explain how you can use the macro recorder to create a VBA subroutine from actions you perform in Excel. You then have to translate the VBA into similar C# code.
I've got a pretty simple web form which lets users pick reports, one specific report they want in xls with pretty formatting instead of CSV. I've whipped up the report using COM INTEROP with excel 2007. It works on my local machine and can write the file, but on the web server it fails with this error:
Microsoft Office Excel cannot open or save any more documents because there is not enough available memory or disk space.
• To make more memory available, close workbooks or programs you no longer need.
• To free disk space, delete files you no longer need from the disk you are saving to.
There is plenty of disk space on the server, so I don't think it's that - could this be a permissions issue? I've escalated ASPNET and NETWORK SERVICE accounts to have write and modify access to the folder we're saving reports to - but still no joy. Any ideas?
Do I need to invoke the com object with a specific user account, or maybe elevate the interop calls to administrator account? Any idea how to do this?
Sorry guys, sorted it.
Serge you are halfway there. I ended up running Excel as administrator:
in component services, select excel application and choose admin as the user that runs it. Seems to work okay for the week of use it'll get :)
Thanks all!
SpreadsheetGear for .NET will let you save xls and xlsx workbooks directly to a response stream with IWorkbook.SaveToStream. It is all safe managed C# code so there is no need to run anything as admin and you will not run into the problems that Excel COM Interop bring on an ASP.NET server.
You can see some simple ASP.NET Excel Reporting samples (C# and VB) using SpreadsheetGear here and download the free trial here.
Disclaimer: I own SpreadsheetGear LLC
you might want to use impersonation for your ASPNET process; here's some info and example on how to do this: killing a win process from a C# console application: How do I set permissions? more info on impersonation How to implement impersonation in an ASP.NET application