用OLEDB读取EXCEL时,单元格内容长度超过255被截断
我正在使用ACE提供程序读取一个Excel工作表,某些单元格包含大于255字节的数据。我在注册表设置中更改了TypeGuessRows,并且也在连接字符串中设置了相同的值。但我仍然在代码中得到截断的值。我无法重新结构化Excel工作表或使用其他提供程序。我运行的是64位Windows。我的Office版本是2013。(有一个小疑问,这是否是原因)。
这是我的连接字符串;对于那些数据长度小于255字节的单元格,它工作得很好。
var connectionString = string.Format("provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + fileName + ";Extended Properties=\"Excel 12.0;IMEX=1;HDR=YES;TypeGuessRows=0;ImportMixedTypes=Text\"");
我也在使用 Microsoft.ACE.OLEDB.12.0
在64位Windows 7上。
我发现 TypeGuessRows
在连接字符串中没有效果。
但是增加 TypeGuessRows
在以下注册表位置可以解决问题:
HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Office\12.0\Access Connectivity Engine\Engines\Excel
关于类似错误的更多信息(尽管您可能已经知道,因为您已经在尝试更改TypeGuessRows)
2 个回答
我也在使用 Microsoft.ACE.OLEDB.12.0
在64位Windows 7上。
我发现 TypeGuessRows
在连接字符串中没有效果。
但是增加 TypeGuessRows
在以下注册表位置可以解决问题:
HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Office\12.0\Access Connectivity Engine\Engines\Excel
关于类似错误的更多信息(尽管您可能已经知道,因为您已经在尝试更改TypeGuessRows)
解决这个问题的方法非常简单。 只需将包含此大量数据的列在Excel工作表中从“常规”更改为“文本”。
现在我感觉自己像个菜鸟。
关键是创建带有LongText类型字段的表,然后在不关闭连接的情况下插入到表中。如果关闭连接并重新打开它,Ace驱动程序将扫描前几行以确定数据类型。由于如果在创建表之后关闭连接,将不存在任何数据,因此它确定Text是正确的类型,而不是预期的LongText。
使用Microsoft.ACE.OLEDB.12.0导出到Excel - VarChar限制为255个字符-需要更长的替代选项,最多可包含8000个字符-腾讯云开发者社区-腾讯云
https://cloud.tencent.com/developer/ask/sof/104798874
Public Class Form1Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.ClickDim 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 255bigText.Append(".")NextoleDbConn.OpenoleDbComm.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.ExecuteNonQueryoleDbConn.CloseProcess.Start(destinationFile)End Sub
End Class
Microsoft.ACE.OLEDB.12-16驱动的改进分析Microsoft.ACE.OLEDB.16.0-CSDN博客
https://blog.csdn.net/xiaoyao961/article/details/148248694
推荐使用第三方库:https://github.com/ExcelDataReader/ExcelDataReader
---------------------------------- 附连接字符串 Microsoft ACE OLEDB 12.0 Provider=Microsoft.ACE.OLEDB.12.0;Data Source=c:\myFolder\myExcel2007file.xlsx; Extended Properties="Excel 12.0 Xml;HDR=YES"; 驱动下载Microsoft Access Database Engine 2016 Redistributable
Download Microsoft Access 2016 数据库引擎可再发行程序包 from Official Microsoft Download Center
https://www.microsoft.com/zh-cn/download/details.aspx?id=54920