Run/drive Excel via own application - 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)?
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.

Related

Emulating ActiveX communication with Excel

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.

Excel DNA and Excel Add In COM

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.)

Automate inserting OLE into Autocad drawings using web based programming language

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.

Get the list of User Defined Functions in Excel using C#

I am writing an Excel Addin in VSTO using C#, with some UDFs. There are other addins installed to the workbook which expose their own UDFs. I want to find out the list of all User Defined Functions and not the ones that come along with Excel. Is there any way I could do that?

is there any way to programatically download data list from sharepoint into excel

we have some data (list) stored in sharepoint. i can manually click "Actions"-> "Export to Spreadsheet" and then run a bunch of code on the Excel output.
I now want to do this on a daily basis from a C# application. Is there anyway to programatically automated that download steps that i am doing now. (the site DOES require authentication)
the "Export to Spreadsheet" fires off some javascript (so doesn't directly just point to a URL so i can read in a URL directly (i dont think)
I would recommend getting the data using SharePoint web services and then using NPOI to convert the data into an excel spreadsheet; Use web services so that you are not limited to running the app on the server and NPOI for reading/writing to excel.
if you are not limited to excel and can use other office products, MS-Access 2007 has a very good integration with sharepoint. you can write your macro and vb scripts to run on that with minor adjustments.
you can also use it as a transfer application, if you build a simple EXPORT TO EXCEL macro, and then set it to run periodically.
Basic approach:
One you have created the spreadsheet with "Export to Spreadsheet" in SharePoint, the excel file holds a list which is linked to the sharepoint list.
This is a list object in Excel which allows for refresh from the same SharePoint list at a later date, make changes in excel and update the SharePoint list from these. So updating your download is really a refresh of the list which you originate in Excel

Categories