Tim Hill
Tim Hill

Reputation: 35

Best way to export a csv file in Excel Addin

I'm a fairly new coder and trying to write a simple add-in to export an Excel table to csv format. Whilst this is easily possible using VBA, I would like to be forward thinking and use the add-in model if I can. I've looked through the API docs, but can't find anything to do it (such as SaveAs)- could anyone please offer me some pointers?

Many thanks

Tim

Upvotes: 0

Views: 964

Answers (1)

Gab Royer
Gab Royer

Reputation: 9806

I'd suggest grabbing the table's range, loading its values and doing the conversion to CSV yourself.

Your code should look something like this :

Excel.run(function (ctx) { 
    var table = ctx.workbook.tables.getItem(tableName);
    var tableRange = table.getRange();
    tableRange.load('values'); 
    return ctx.sync().then(function() {
        ConvertToCSV(tableRange.values);
    });
}).catch(function(error) {
    console.log("Error: " + error);
    if (error instanceof OfficeExtension.Error) {
        console.log("Debug info: " + JSON.stringify(error.debugInfo));
    }
});

Please let me know how this works out for you.

Gabriel Royer - Developer on the Office Extensibility Team, MSFT

Upvotes: 1

Related Questions