Pages

Friday, August 21, 2015

OpenXML teaser

If you want to be able to create an Excel document from a batch job or on a machine where Excel is not installed, you can use the Open XML standard to create the file without ever opening the Excel application.

You would of course need some X++ wrappers, but I have learned today that these are actually already created for some Russian localizations.

Check this out:
static void OpenXML_Excel_Demo(Args _args)
{
    Counter                 c;
    XMLExcelDocument_RU     excelDocument;
    str                     templateFilename = @'F:\SomeTemplate.xlsx';
    str                     newFilename = @'F:\OpenXML_Excel_Demo.xlsx'; 

    // Create a new document
    // This implementation works with template files, so I have just created an empty spreadsheet to act
    // as template
    excelDocument = XMLExcelDocument_RU::newFromFile(templateFilename, newFilename);
    
    // Create some column headers
    for (c = 1; c != 10; c++)
    {
        excelDocument.insertValue(
            ComExcelDocument_RU::numToNameCell(c, 1), 
            strFmt("Column %1", c));
    }

    // Close and save the document
    excelDocument.closeDocument(true);
}

Use the cross reference tool to see more usage of this in AX.

5 comments:

Martin DrĂ¡b said...

Good to know about!
And for people like me, it's easier to extend solutions based on .NET rather than COM (e.g. OXMLWorkbook_RU vs SysExcelWorkbook).

Kevin said...

There is also a free library that does not use Excel

http://epplus.codeplex.com/

James Adrewson said...

Geat Stuff!

Igor Balakirev said...

this is Client code,
how to make it work on Server?

George Frantskevich said...

Very strange. This class not use Excel application, but works with the same low speed, as ComExcelDocument.
Trolling from Microsoft :)

Program version copy/paste forever.