我在 Excel 2010 中使用 Visual Studio 2010 Professional。以下代码在新表的字段行上创建了一个名为“Hourly_Burn_Data_By_Plant”的表,而不是“按工厂的每小时烧录数据”和一个具有相同名称的命名范围。这会在向表中添加数据时产生错误。做错了什么?
Dim ConStrTemp As String
ConStrTemp = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & "C:\Studies\NewHourlyFuel\Book1.xlsx" & _
";Extended Properties=""Excel 12.0;HDR=Yes;""" 'Mode=ReadWrite;ReadOnly=False;"""
Dim ConnTemp As New OleDbConnection(ConStrTemp)
Globals.OpenConn(ConnTemp)
Dim ExistingTables() As String
ExistingTables = Globals.GetTableNamesFromConn(ConnTemp)
Dim i As Int16
For i = 0 To ExistingTables.GetUpperBound(0)
Debug.Print(ExistingTables(i))
Next
Dim NametoCreate As String = "Hourly Burn Data By Plant"
Dim CreateSQL As String = "Create Table [" & NametoCreate & "] ([Plant] char(50), [RDate] Date, [Period 1] float, " & _
"[Period 2] float, [Period 3] float, [Period 4] float, [Period 5] float, [Period 6] float, [Period 7] float, [Period 8] float, [Period 9] float, " & _
"[Period 10] float, [Period 11] float, [Period 12] float, [Period 13] float, [Period 14] float, [Period 15] float, [Period 16] float, [Period 17] float, " & _
"[Period 18] float, [Period 19] float, [Period 20] float, [Period 21] float, [Period 22] float, [Period 23] float, [Period 24] float, [Daily Total] float)"
Dim cmd As New OleDb.OleDbCommand(CreateSQL, ConnTemp)
Try
cmd.ExecuteNonQuery()
MsgBox("susccess")
ExistingTables = Globals.GetTableNamesFromConn(ConnTemp)
For i = 0 To ExistingTables.GetUpperBound(0)
Debug.Print(ExistingTables(i))
Next
Globals.CloseConn(ConnTemp)
Catch ex As Exception
MsgBox("Failure " & ex.Message)
Globals.CloseConn(ConnTemp)
End Try