0

我正在尝试使用 vb.net 中的 Microsoft.ACE.OLEDB.12.0 提供程序创建一个 Excel 电子表格,但我似乎无法定义一个允许超过 255 个字符的字段。

这是我当前创建表的代码:

 create table [table_name] ([column_1] VarChar)

我知道 VarChar 限制为 255 个字符,但我无法找出可以容纳更多字符的替代方法。

我试过这个:

 create table [table_name] ([column_1] LongVarChar)

并得到“字段定义中的语法错误”异常。

同样,我尝试过 LongVarWChar、Memo、NText(8000),... 的结果相同。有什么建议么?

编辑:这是我的连接字符串:

 "Provider=Microsoft.ACE.OLEDB.12.0;" &
 "Data Source=" & destinationFile & ";" &
 "Extended Properties=""Excel 12.0 Xml;HDR=YES"";"

编辑2:这是我想要做的基本想法

导入 System.Data.OleDb 导入 System.Text

Public Class Form1
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        dim destinationFile = "c:\users\dell\desktop\test\test_" & Now.ToString("yyyyMMdd_HHmmssfffff") & ".xlsx"
        Dim oleDbConnString =
                "Provider=Microsoft.ACE.OLEDB.12.0;" &
                "Data Source=" & destinationFile & ";" &
                "Extended Properties=""Excel 12.0 Xml;HDR=YES"";"
        Dim oleDbConn as New OleDbConnection(oleDbConnString)
        Dim oleDbComm as New OleDbCommand("create table Sheet1 (column_1 LongText);", oleDbConn)

        oleDbConn.Open
        oleDbComm.ExecuteNonQuery
        oleDbConn.Close

        Dim bigText as New StringBuilder()
        For i = 0 to 255
            bigText.Append(".")
        Next
        oleDbComm.CommandText = "insert into Sheet1(column_1) values ('" & bigText.ToString & "');"

        oleDbConn.Open
        oleDbComm.ExecuteNonQuery
        oleDbConn.Close

        Process.Start(destinationFile)
    End Sub
End Class

Button1_Click 子中的最后一次 ExecuteNonQuery 调用引发异常。代替“LongText”,我尝试了许多不同的数据类型,包括一组对使用 Ace 引擎的 Excel 数据源无效的数据类型。我没有发现任何东西可以让我插入大于 255 个字符的字符串。

4

2 回答 2

1

您收到语法错误,因为您使用的是不存在的数据类型(至少在您尝试使用它们的地方,如果有的话)。您需要将 column_1 的数据类型指定为LONGTEXT

编辑:我给出的解决方案是针对您报告的第一个错误,这是因为您的 create 语句中有未知数据类型。正如您所经历的,使用LONGTEXT而不是摆脱了该错误。但是,此链接:

Excel 无法插入超过 255 个字符?

似乎表明这是 Excel 中的一个限制,这是一个相当古老的问题,但是当我刚刚在 Office 2013 中尝试时遇到了同样的问题。它可能是喷气发动机本身。这里还有一个问题,链接:

c#当字符串数据超过255个字符时插入Excel时出错

选择直接使用 Excel 对象......对我来说,这似乎是更可行的解决方案。当您可以直接与 Excel 对话时,为什么还要使用 OleDb?

于 2016-09-27T23:20:37.563 回答
0

这是对我有用的代码。关键是创建字段类型为 LongText 的表,然后在不关闭连接的情况下插入表中。如果您关闭连接并重新打开它,Ace 驱动程序会扫描前几行以确定数据类型。由于如果您在创建表后关闭连接将不存在任何数据,因此它决定 Text 是正确的类型,而不是预期的 LongText。

Public Class Form1
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim destinationFile = "c:\users\dell\desktop\test\test_" & Now.ToString("yyyyMMdd_HHmmssfffff") & ".xlsx"
        Dim oleDbConnString =
                "Provider=Microsoft.ACE.OLEDB.12.0;" &
                "Data Source=" & destinationFile & ";" &
                "Extended Properties=""Excel 12.0 Xml;HDR=YES"";"
        Dim oleDbConn as New OleDbConnection(oleDbConnString)
        Dim oleDbComm as New OleDbCommand("create table Sheet1 (column_1 LongText);", oleDbConn)

        Dim bigText as New StringBuilder()
        For i = 0 to 255
            bigText.Append(".")
        Next

        oleDbConn.Open
        oleDbComm.ExecuteNonQuery
        'after creating the table with column of datatype LongText,
        'you mustn't close the connection prior to inserting text longer
        'than 255 characters - keep it open!!
        oleDbComm.CommandText = "insert into Sheet1(column_1) values ('" & bigText.ToString & "');"
        oleDbComm.ExecuteNonQuery
        oleDbConn.Close

        Process.Start(destinationFile)
    End Sub
End Class
于 2016-09-28T01:44:37.937 回答