You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

VB.NET中CSV/Excel导入DataGridView的跨系统兼容故障排查

故障原因分析:ANYCPU模式下CSV/Excel导入异常

运行代码

Dim fd As OpenFileDialog = New OpenFileDialog()
Dim strFileName As String
Dim excelconstring As String
'Dim excelcon As New OleDbConnection
Dim excelcon As New OdbcConnection
Button5.Enabled = False
fd.Title = "Open File Dialog"
fd.InitialDirectory = "C:\Users\Admin\Desktop\chrome\allotment\2022\feb 2022"
fd.Filter = "Excel file (*.xls)|*.xlsx|All files (*.*)|*.*"
fd.FilterIndex = 3
fd.RestoreDirectory = True

If fd.ShowDialog() = DialogResult.OK Then
    strFileName = fd.FileName
   
    Dim fileext As String = Path.GetExtension(strFileName)

    If fileext = ".csv" Then
        '------------------------------------------------------------------------------
        Dim csvFileFolder As String = Path.GetDirectoryName(strFileName)
        Dim csvFileName As String = Path.GetFileName(strFileName)
        Dim connString As String = "Driver={Microsoft Text Driver (*.txt; *.csv)};Dbq=" _
            & csvFileFolder & ";Extended Properties=""Text;HDR=No;FMT=Delimited"""

        Dim conn As New Odbc.OdbcConnection(connString)

        'Open a data adapter, specifying the file name to load
        Dim daa As New Odbc.OdbcDataAdapter("SELECT * FROM [" & csvFileName & "]", conn)
        'Then fill a data table, which can be bound to a grid
        Dim dt As New DataTable
        daa.Fill(dt)

        DataGridView1.DataSource = dt

    ElseIf fileext = ".xls " OrElse fileext = ".XLS" OrElse fileext = ".xlsx" Then
        '-----------------------------------------------------------------excel file--------------------------------------------------------------------------'
        excelconstring = "Driver={Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb)};DBQ=" & strFileName & ""
        excelcon = New OdbcConnection(excelconstring)
        excelcon.Open()

        Dim da As New OdbcDataAdapter("select * from [STOCK_ISSUE_REGISTER_REPORT$]", excelcon)
        Dim ds As New DataSet

        da.Fill(ds)

        DataGridView1.DataSource = ds.Tables(0)

    End If
End If

问题背景

上述代码在32位Windows开发机上可正常导入CSV、Excel文件到DataGridView;但以ANYCPU模式发布后,64位Windows 7仅能导入Excel、无法导入CSV,Windows 10则两者均无法导入。已在所有目标机安装Interop组件和AccessDatabaseEngine。

故障原因

  • ODBC驱动位数不兼容:
    代码中读取CSV的Microsoft Text Driver (*.txt; *.csv)仅存在32位版本,微软未发布对应64位驱动。ANYCPU模式下,程序在64位系统会以64位进程启动,无法找到匹配的64位文本驱动,导致CSV导入失败。而Excel的ODBC驱动在AccessDatabaseEngine中有64位版本,因此Windows 7上能正常读取Excel。
  • ANYCPU模式的进程位数切换:
    在32位开发机上,ANYCPU会以32位进程运行,可调用32位ODBC驱动;但到64位系统时,ANYCPU自动切换为64位进程,与32位驱动完全不兼容。
  • Windows 10的兼容性限制:
    Windows 10对旧版ODBC驱动的兼容性要求更严格,即使安装了AccessDatabaseEngine,也可能因驱动签名验证、系统权限限制或默认禁用旧驱动等原因,导致64位进程无法调用Excel的ODBC驱动,最终两者都无法导入。

解决方案建议

  • 强制32位运行:修改项目属性,将目标平台从ANYCPU改为x86,让程序在64位系统上也以32位进程启动,可正常调用32位文本驱动和Excel驱动。
  • 替换CSV读取方式:放弃依赖ODBC驱动,改用.NET原生的TextFieldParser类或第三方CSV处理库读取CSV文件,彻底摆脱系统驱动限制。
  • 检查驱动安装:若坚持使用64位进程,需安装64位版本的AccessDatabaseEngine,但注意CSV文本驱动仍无64位版本,CSV部分仍需替换读取方式。

内容的提问来源于stack exchange,提问作者Goventhran Palanisamy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.11 02:10:17