我正在使用带有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
答案 0 :(得分:1)
excel中正确的create table语法是:
CREATE TABLE `SheetName` (`ColumnName1` TYPE, `ColumnName2` TYPE)
TYPE =见下面的链接
http://support.sas.com/documentation/cdl/en/acpcref/63184/HTML/default/viewer.htm#a003072672.htm
注意使用`而不是[]作为名称标识符......