【问题标题】:Create table in Excel 2010 With Underscore instead of spaces along with a named range在 Excel 2010 中使用下划线而不是空格和命名范围创建表
【发布时间】:2013-04-03 17:30:35
【问题描述】:

我在 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

【问题讨论】:

    标签: vb.net ado.net oledb


    【解决方案1】:

    在 excel 中正确的创建表语法是:

    CREATE TABLE `SheetName`  (`ColumnName1` TYPE, `ColumnName2` TYPE)
    

    其中 TYPE = 见下面的链接

    http://support.sas.com/documentation/cdl/en/acpcref/63184/HTML/default/viewer.htm#a003072672.htm

    注意使用 ` 而不是 [] 作为名称标识符...

    【讨论】:

    • 我执行了以下操作: CreateSQL = "Create Table " & "'" & NametoCreate & "'" & " (Section Char(50), RDAte Date, " & "'" & "Period 1 " & "'" & " Float)" 并且我收到“创建 SQL 时出现语法错误”消息。
    猜你喜欢
    • 2013-07-14
    • 2014-01-27
    • 1970-01-01
    • 2021-08-01
    • 2014-02-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多