c# - Creating a PivotTable programmatically from multiple database tables -
i have problem automating excel cannot solve. want mimic "import external data" feature. works flat tables, not pivottables, when select multiple database tables import.
the manual way
i have access database 2 tables (accounts , transactions), linked foreign key ("account" in transactions table). want import both pivottable. usual way click on "data" -> "external data", select both tables in dialog, choose "pivottable report" , click "ok". pivottable in active sheet , on right side there field list offers fields in hierarchy of imported tables.
this want mimic mentioned database file. have idea how works?
what tried
i have tried record , adapt macro, no avail, records incomplete , flawed code. incomplete because recorded code not feature creation of data model, necessary (?). flawed because code not run, syntax incorrect.
i can create pivottable 1 database table this:
excel.pivotcaches pivotcaches = workbook.pivotcaches(); excel.pivotcache pivotcache = pivotcaches.create(excel.xlpivottablesourcetype.xlexternal, type.missing, type.missing); pivotcache.connection = constr; pivotcache.maintainconnection = true; pivotcache.commandtext = "accounts"; pivotcache.commandtype = excel.xlcmdtype.xlcmdtable; excel.pivottables pivottables = sheet.pivottables(); excel.pivottable pivottable = pivottables.add(pivotcache, sheet.get_range("a3", "c20"), "pivot", type.missing, type.missing);
but fails in each of many attempts tried more 1 database table.
what way go multiple tables?
bear in mind not know table names , join fields, plus not want lose distinction between tables, should preserved in pivottable field list hierarchical choice of fields. furthermore, catch-all sql query serious impairment performance.
please me!
ps: on office 2013 c# language of choice.
/edit1: later want pivottable refresh if database tables change (manually invoking refresh).
/edit2: entirely satisfied way use multiple worksheets sources pivottable, long appear separately in field list.
Comments
Post a Comment