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've just realised I can record a macro to record what happens when I use the GUI:
    With ActiveSheet.ListObjects.Add(SourceType:=0, Source:= _
        "ODBC;..." _
        , Destination:=Range("$A$1")).QueryTable
        .CommandText = Array("SELECT * FROM myTable")
        .RowNumbers = False
        .FillAdjacentFormulas = False
        .PreserveFormatting = True
        .RefreshOnFileOpen = False
        .BackgroundQuery = True
        .RefreshStyle = xlInsertDeleteCells
        .SavePassword = False
        .SaveData = True
        .AdjustColumnWidth = True
        .RefreshPeriod = 0
        .PreserveColumnInfo = True
        .ListObject.DisplayName = "Table_myTable"
        .Refresh BackgroundQuery:=False
    End With
However, I can't seem to replicate it via NetOffice and get the same result?
            var sheet = (_excel.ActiveSheet as Excel.Worksheet);
            var rng = sheet.Range("$A$1");

            var lo = sheet.ListObjects.Add(
                0,
                "ODBC;...",
                Missing.Value,
                Excel.Enums.XlYesNoGuess.xlNo,
                rng);

            var qt = lo.QueryTable;
            qt.CommandText = new[] { "SELECT TOP 1 * FROM myTable" };
            qt.RowNumbers = false;
            qt.FillAdjacentFormulas = false;
            qt.PreserveFormatting = true;
            qt.RefreshOnFileOpen = false;
            qt.BackgroundQuery = true;
            qt.RefreshStyle = Excel.Enums.XlCellInsertionMode.xlInsertDeleteCells;
            qt.SavePassword = false;
            qt.SaveData = true;
            qt.AdjustColumnWidth = true;
            qt.RefreshPeriod = 0;
            qt.PreserveColumnInfo = true;
            qt.ListObject.DisplayName = "Table_myTable";
            qt.Refresh();

Viewing all articles
Browse latest Browse all 1741

Latest Images

Trending Articles



Latest Images

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