我正在编写一个工具,用于在 VB.NET
中将数据库查询结果导入Excel我尝试了以下代码。它不起作用。
With objApp.ActiveSheet.QueryTables.Add(Connection:="ODBC;DSN=Build_statistics;", _
Destination:=objApp.Range("G15"))
.CommandText = "SELECT * from mytable"
.Name = "Query"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = Microsoft.Office.Interop.Excel.XlCellInsertionMode.xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.PreserveColumnInfo = True
**.Refresh(BackgroundQuery:=False)** 'I am getting error here
End With
它在VBA中工作正常,但在VB.NET中没有。
答案 0 :(得分:0)
为什么不将查询结果加载到DataSet中,然后将DataSet表的内容导入Excel?
答案 1 :(得分:0)
该行
**.Refresh(BackgroundQuery:=False)** 'I am getting error here
不正确,应替换为
.Refresh BackgroundQuery:=False