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();