sql server - Automated export of a table/query from SQL SVR 2012 into Excel 2007 and subsequent VBA formatting macro? -
i’ve been given task of updating code isn’t working after our sql svr 200 sql svr 2012 (don't laugh) conversion. have automated task creates 500ish xls files in line-by-line manner using old sp_oacreate command. in 2000, job takes hours , rickety @ best. needless say, doesn’t work @ in 2012. that's never upgrading?
i rewrote job constructing table, adding info table, & doing bulk export xls (using openrowset). new task ran in less 15 minutes , ecstatic. ops complained new files weren’t formatted…
i looped , tried formatting automation. that’s fell apart.
use openrowset , export “xls template file” has autorun vba code formatting when file opened first time. didn’t work.
use openrowset , export “xlsm template file” has autorun vba code formatting when file opened first time. didn’t work.
use bcp , export “xls template file” has autorun vba code formatting when file opened first time. didn’t work.
use bcp , export “xlsm template file” has autorun vba code formatting when file opened first time. didn’t work.
use bcp/openrowset , export xls/xlsx , try execute vba code workbook…didn’t work.
use bcp/openrowset, export “dummy” xls/xlsx, use bat file recopy file, resave, etc…didn’t work.
use bcp/openrowset, export “dummy” txt/csv, use bat file convert file excel, resave, etc…didn’t work.
etc. didn't work.
i tried every combination, every export method, every filetype, etc. methods allowed me export no formatting. other methods wouldn’t export anything. no method permitted export/format, though.
then discovered “real” problem ==> when export using bcp/openrowset , try open file, “file trying open in different format specified file extension” error (fyi using excel 2007). had sort of ignored error, after days of banging head against wall can see error leading me real problem along. export file (regardless of xls/xlsm/xlsx) not actual excel file; it’s bunch of html tags. why formatting vba code won’t work; it’s not actual excel file! no matter do, it’s still not real excel file. formatting never work because it's not real excel file.
so, need automated method export table sort of excel format (xls/xlsm/xlsx) , execute vba code format (bold, column width, number/date formatting, etc.) newly-exported file. seems such routine task…but see routine <> easy. i’ve seen references npoi , closedxml in forums, can’t believe need additional 3rd party software accomplish task.
you write vba code in excel spreadsheet go data database , formatting vba after. below code should starting point.
please note have add reference microsoft activex data objects library
code function. also, may need change provider in connection string deepening on systems configuration. below code utilizes sql server native client 11.
dim cn new adodb.connection dim sqlrs new adodb.recordset dim sqlcommand string sqlcommand = "select...from..." cn.connectionstring = "provider=sqlncli11;server=xxxxxxx;database=xxxxxx;trusted_connection=yes;" cn.open cn.execute "set nocount on" ''required complex t-sql queries sqlrs.cursorlocation = aduseclient call sqlrs.open(sqlcommand, cn, adopenstatic, adlockbatchoptimistic) activeworkbook.sheets("sheet1").range("a1").copyfromrecordset sqlrs sqlrs.close set sqlrs = nothing cn.close set cn = nothing
Comments
Post a Comment