我正在尝试使用 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 个字符的字符串。