Quantcast
Channel: NetOffice - MS Office in .NET
Viewing all articles
Browse latest Browse all 1741

New Post: Excel: Apply a TableStyle to a QueryTable

$
0
0
I am trying to get a data from a database into Excel and apply a table style to it in the most quickest time; whilst still using NetOffice for the interoperability it offers. I assume that using Excel's inbuilt query functions will be far faster so I am using that (please correct me if I am wrong on this)

I can use a query table:
var sheet = (_excel.ActiveSheet as Excel.Worksheet);
var rng = sheet.Range("A1");

var qt = sheet.QueryTables.Add("ODBC;...", rng, "SELECT * FROM myTable");
qt.Refresh();
and this will import the data correctly (e.g. dates actually display as dates etc...). But when I try and access the ListObject to apply a TableStyle I get an exception.

Now if I add a list object like:
var sheet = (_excel.ActiveSheet as Excel.Worksheet);

var qt = sheet.ListObjects.Add(
    Excel.Enums.XlListObjectSourceType.xlSrcQuery,
    "ODBC;...",
    null,
    Excel.Enums.XlYesNoGuess.xlNo,
    rng,
    "TableStyleMedium9").QueryTable;

qt.CommandType = Excel.Enums.XlCmdType.xlCmdSql;
qt.CommandText = "SELECT * FROM myTable";
qt.Refresh();
The the dates in the query display as decimal numbers and not dates...

I could just format the columns afterwards, but the problem is that I won't actually know the query that is being run as the user types this at runtime, so I would prefer to get Excel to do this.

So essentially, what I want, is to use the first bit of code and apply a TableStyle to it.

Can anyone help?

Viewing all articles
Browse latest Browse all 1741

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>