使用 OpenXML 读取 Excel 文件的性能

发布于 2024-11-07 00:17:34 字数 161 浏览 2 评论 0原文

截至目前,我正在使用 oledbreader 阅读所有版本的 Excel 文件。我引用了一个用于读取 Excel 2010 文件的 dll。但我无法使用 oledbreader 读取某些 excel 2010 文件。所以我想使用 openxml 来读取所有 excel 文件。这有什么性能问题吗? 哪个更好?

As of now im reading all version of excel files using oledbreader. i referred a dll for reading Excel 2010 files. but i cannot read some excel 2010 files using oledbreader. so i would like to use openxml for reading all excel files. is ter any performance issue in this?
which is better?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

尐籹人 2024-11-14 00:17:34

我非常幸运地使用以下代码从 Excel 电子表格中检索表名称(工作表名称)和列名称。

Private Sub GetWorksheetData

  Dim xlBaseConnStr1 As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=XLS;Extended Properties=""Excel 8.0;HDR=Yes"""
  Dim xlBaseConnStr2 As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=XLS;Extended Properties=""Excel 12.0 Xml;HDR=YES"""
  Dim xlName As String
  Dim conStr As String

  Dim oDia As New OpenFileDialog

  oDia.ShowDialog()

  xlName = oDia.FileName

  If xlName = "" Then
     Exit Sub
  End If

  Dim extType As String = Path.GetExtension(xlName)
  Select Case extType
     Case ".xls"
        conStr = xlBaseConnStr1.Replace("XLS", xlName)
     Case ".xlsx"
        conStr = xlBaseConnStr2.Replace("XLS", xlName)
     Case Else
        MessageBox.Show("Unrecognized file type")
        Exit Sub
  End Select

  Dim dtSheets As New DataTable

  Using cn As New OleDbConnection(conStr)
     cn.Open()
     dtSheets = cn.GetSchema("Columns")
  End Using

  DataGrid1.ItemsSource = dtSheets.DefaultView

End Sub

上面的代码块从我放置的随机 Excel 电子表格中返回以下数据。

在此处输入图像描述

您需要导入以下命名空间才能使其工作:

Imports System.Data.OleDb
Imports System.Data
Imports Microsoft.Win32
Imports System.IO

如果您尝试访问的电子表格有宏,上面的代码可能会失败。

I have had very good luck using the following code to retrieve table names(worksheet names) and column names from Excel spreadsheets.

Private Sub GetWorksheetData

  Dim xlBaseConnStr1 As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=XLS;Extended Properties=""Excel 8.0;HDR=Yes"""
  Dim xlBaseConnStr2 As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=XLS;Extended Properties=""Excel 12.0 Xml;HDR=YES"""
  Dim xlName As String
  Dim conStr As String

  Dim oDia As New OpenFileDialog

  oDia.ShowDialog()

  xlName = oDia.FileName

  If xlName = "" Then
     Exit Sub
  End If

  Dim extType As String = Path.GetExtension(xlName)
  Select Case extType
     Case ".xls"
        conStr = xlBaseConnStr1.Replace("XLS", xlName)
     Case ".xlsx"
        conStr = xlBaseConnStr2.Replace("XLS", xlName)
     Case Else
        MessageBox.Show("Unrecognized file type")
        Exit Sub
  End Select

  Dim dtSheets As New DataTable

  Using cn As New OleDbConnection(conStr)
     cn.Open()
     dtSheets = cn.GetSchema("Columns")
  End Using

  DataGrid1.ItemsSource = dtSheets.DefaultView

End Sub

The above chunk of code returns the following data from a random Excel spreadsheet that I had laying around.

enter image description here

You will need to import the following namespaces for this to work:

Imports System.Data.OleDb
Imports System.Data
Imports Microsoft.Win32
Imports System.IO

If the spreadsheet that you are trying to access has macros, the above code may fail.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文