datatable - Refresh Excel ListObject from a Commandline application -
i'm trying update information of listobject hosted in workbook created excel addin (example code1) commandline application.
i've tried creating instance of excel , accessing @ listobjects getvstoobject returns null objects. think security problem, don't know how resolve it. (example code2).
i've tried serverdocuments don't have cacheddata, , it's not possible use @ application add-in level. suggestion?
thanks in advance.
code1
using system; using system.data; using excel = microsoft.office.interop.excel; using office = microsoft.office.core; using microsoft.office.tools.excel; using microsoft.office.tools.excel.extensions; namespace exceladdin2{ public partial class thisaddin{ private void thisaddin_startup(object sender, system.eventargs e){ datatable thedatatable = getdatatable(); workbook workbook = globals.thisaddin.application.activeworkbook.getvstoobject(); worksheet worksheet = ((excel.worksheet)workbook.worksheets[1]).getvstoobject(); excel.range range = worksheet.range[worksheet.cells[1, 1], worksheet.cells[1, 1]]; excel.listobject interoplist = worksheet.listobjects.add(microsoft.office.interop.excel.xllistobjectsourcetype.xlsrcrange, type.missing, type.missing, microsoft.office.interop.excel.xlyesnoguess.xlno,range); listobject list = interoplist.getvstoobject(); list.name = "mylistobject"; list.datasource = thedatatable; workbook.savecopyas(@"c:\theexcel.xlsx"); } private datatable getdatatable() { /*datatable example, i'm using implementation of oledbcommand it*/ datatable table = new datatable("mytable"); datacolumn columnid = new datacolumn(); columnid.datatype = system.type.gettype("system.string"); columnid.columnname = "id"; datacolumn columnname = new datacolumn(); columnname.datatype = system.type.gettype("system.string"); columnname.columnname = "name"; table.columns.add(columnid); table.columns.add(columnname); datarow row; (int = 0; <= 2; i++){ row = table.newrow(); row["id"] = i; row["name"] = "name " + i; table.rows.add(row); } return table; } ..etc..
code2 references: microsoft.office.interop.excel, microsoft.office.tools.common.v9.0, microsoft.office.tools.excel.v9.0, microsoft.office.tools.v9.0, microsoft.visualstudio.tools.applications.runtime.v9.0, system.windows.forms (it needs ¿?)
using system; using system.data; using excel = microsoft.office.interop.excel; using microsoft.office.tools.excel; using microsoft.office.tools.excel.extensions; using system.reflection; namespace consoleapplication1{ class program { static void main(string[] args) { excel.application app = new excel.applicationclass(); excel.workbook excelworkbook = app.workbooks.open(@"c:\theexcel.xlsx", type.missing, true, type.missing, type.missing, type.missing, type.missing, type.missing, type.missing, type.missing, type.missing, type.missing, type.missing, type.missing, type.missing); microsoft.office.tools.excel.worksheet worksheet = ((excel.worksheet)excelworkbook.worksheets[1]).getvstoobject(); foreach (listobject list in worksheet.listobjects) { list.datasource = getnewdatatable(); } app.quit(); } private static datatable getnewdatatable() { datatable table = new datatable("mytable"); datacolumn columnid = new datacolumn(); columnid.datatype = system.type.gettype("system.string"); columnid.columnname = "id"; datacolumn columnname = new datacolumn(); columnname.datatype = system.type.gettype("system.string"); columnname.columnname = "name"; table.columns.add(columnid); table.columns.add(columnname); datarow row; (int = 0; <= 2; i++) { row = table.newrow(); row["id"] = i; row["name"] = "new name " + i; table.rows.add(row); } return table; } } }
Comments
Post a Comment