Question

Good Afternoon Fellas,

A little advice would be appreciated, I'm approaching a problem by which I need to write an agent which will go to a particular file directory, open an Excel Workbook (which already exists) and will then change the value of two cells (which are always the same cells) to be the current month (at the time of the agent running).

Unfortunately, this is a work project so VSTO is off-limits. ExcelDNA however, I have used before. I'm not looking for someone to 'do my work', but any suggestions of where to look or previous examples would be great.

I've had a poke around myself to no avail, if anyone could point me in a good direction to get started that would be great!

Much appreciated.

Was it helpful?

Solution

Just thinking out loud with excel now having xml backed file format (zipped) it may be easier to make this change by parsing and rewriting the xml, or to use an xml transform [shudder].

Now the more supported way would be to use a VSTO but you say that is not available. If you look further into the parsing the file format be aware that it is a complex schema, you may find source out there that does the understanding for you.

$0.02

OTHER TIPS

Excel-DNA is great if you want to make an Excel add-in in .NET, with user-defined function etc. But it sounds like you just want to automate Excel from an outside executable.

For this the easiest to directly install and reference the Primary Interop Assemblies (PIA) which you can find here: http://www.microsoft.com/download/en/details.aspx?id=3508, or you can use the brilliant version-independent interop assemblies in the NetOffice project.

In both cases you make a VB.NET or C# console app, then add a reference to the interop assemblies you've chosen, and away you go.

In C#, your method using NetOffice might start like this (I think it can be a bit cleaner in C# 4):

static void Test()
{
    // Initialize Api COMObject Support
    LateBindingApi.Core.Factory.Initialize();

    // start excel and turn off msg boxes
    Excel.Application excelApplication = new Excel.Application();
    excelApplication.DisplayAlerts = false;

    // add a new workbook
    Excel.Workbook workBook = excelApplication.Workbooks.Add();
    Excel.Worksheet workSheet = (Excel.Worksheet)workBook.Worksheets[1]; 

    worksheet.get_Range("A1").Value = "XXX";

    // save the book 
    string fileExtension = GetDefaultExtension(excelApplication);
    string workbookFile = string.Format("{0}\\Example01{1}", 
                       Application.StartupPath, fileExtension);
    workBook.SaveAs(workbookFile, Missing.Value, Missing.Value, Missing.Value,
               Missing.Value, Missing.Value, XlSaveAsAccessMode.xlExclusive);

    // close excel and dispose reference
    excelApplication.Quit();
    excelApplication.Dispose();
}

I can suggest OpenXml Sdk if you use Excel 97 or higher

why not using ExcelDNA? This could make the Excel-file more independent from the running agent. If there is a DLL (or even a DNA-file) which will provide the desired values, this two cells could be filled by a simple function. The advantage is, that you don't have to start the agent first, just open the workbook. Disadvantage, the ExcelDNA.xll and everything else you need to provide the desired values, must be deployed on every client-machine that will open the workbook. Or you have to transform the formulas of this 2 cells into real values before you publish it. In this case the interop-solution might be better.

Hope this helps.

Cheers Lothy

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top