3

我正在 VB.NET 中构建一个应用程序来从 Excel 文件中读取行并将它们填充到DataTable.

dtRow = dataTable.NewRow()
Dim startTime As DateTime = DateTime.Now

dtRow("name") = suppliers.CellValue("A", rowCount)
/* SNIP - just more string retrieval */
dtRow("statistics") = suppliers.CellValue("P", rowCount)

dataTable.Rows.Add(dtRow)

Dim endTime As DateTime = DateTime.Now

Debug.Print(String.Format("Time elapsed to retrieve '{0}': {1} ms", rowCount, (endTime - startTime).ToString("fffffff")))

CellValue是我自己的创作——但它是一个小功能,我已经测量了它的运行时间。它相当快。

但是,当我打开一个 10,000 行的 Excel 文件(填充相同的数据)时,处理时间会慢得多。

3,000 行

Time elapsed to retrieve '2': 0510051 ms
Time elapsed to retrieve '3': 0500050 ms
Time elapsed to retrieve '4': 0340034 ms
Time elapsed to retrieve '5': 0350035 ms
Time elapsed to retrieve '6': 0340034 ms
Time elapsed to retrieve '7': 0340034 ms
Time elapsed to retrieve '8': 0350035 ms

6,000 行

Time elapsed to retrieve '2': 0710071 ms
Time elapsed to retrieve '3': 0760076 ms
Time elapsed to retrieve '4': 0620062 ms
Time elapsed to retrieve '5': 0670067 ms
Time elapsed to retrieve '6': 0750075 ms
Time elapsed to retrieve '7': 0750075 ms
Time elapsed to retrieve '8': 0700070 ms

10,000 行

Time elapsed to retrieve '2': 0920092 ms
Time elapsed to retrieve '3': 0920092 ms
Time elapsed to retrieve '4': 1790179 ms
Time elapsed to retrieve '5': 1810181 ms
Time elapsed to retrieve '6': 1930193 ms
Time elapsed to retrieve '7': 2240224 ms
Time elapsed to retrieve '8': 1820182 ms

在此处输入图像描述

为什么会出现这种情况?我可以修复它吗?

编辑:suppliers是我创建的用于处理 Excel 文件的类,具有以下构造函数:

Public Sub New(ByVal doc As SpreadsheetDocument, ByVal sheetName As String)
    pWorkbookPart = doc.WorkbookPart

    Dim sheet As Sheet = pWorkbookPart.Workbook.Descendants(Of Sheet).Where(Function(s) s.Name = sheetName).FirstOrDefault()

    pWorksheetPart = CType(pWorkbookPart.GetPartById(sheet.Id), WorksheetPart)

    pSharedStringTable = pWorkbookPart.GetPartsOfType(Of SharedStringTablePart).FirstOrDefault()
End Sub

CellValue

Public Function CellValue(ByVal column As String, ByVal row As Integer) As String
    Dim cellAddress As String = column & row
    Dim cell As Cell = pWorksheetPart.Worksheet.Descendants(Of Cell).Where(Function(c) c.CellReference = cellAddress).FirstOrDefault()

    Dim index As Integer
    Dim returnValue As String

    If cell IsNot Nothing Then
        If cell.DataType IsNot Nothing Then
            index = Integer.Parse(cell.InnerText)
            returnValue = pSharedStringTable.SharedStringTable.ElementAt(index).InnerText
        Else
            returnValue = CStr(cell.InnerText)
        End If
    End If

    Return returnValue
End Function
4

2 回答 2

6

一个可能的问题是,如果您的字符串表变得非常大,那么ElementAt对于遍历SharedStringTable. 由于此表相对于您的处理是静态的,因此我建议删除该部分并使用List<string>or 数组存储它:

' Use this instead of pSharedStringTable
' Dim sharedStringTable As New List(Of String)

' Initialize your string table
sharedStringTable.AddRange( _
    From xml In pSharedStringTable.SharedStringTable _
    Select xml.InnerText)

' Now you can use sharedStringTable.ElementAt(index) and enjoy optimization
' Or you can use sharedStringTable(index)

另一个可能的问题是通过引用对单元格进行恒定线性搜索。相反,您应该将其转换为字典:

' Dim cells As New Dictionary(Of String, Of Cell)
For Each cell In pWorksheetPart.Worksheet.Descendants(Of Cell)
    cells.Add(cell.CellReference.InnerText, cell)
Next cell
' Only one round-trip to Excel for cells using this method

在每种情况下,您都会用记忆换取时间,在这两种情况下,我都认为这符合您的最佳利益:

' Revised lookup using data structures optimized for common access
If cells.TryGetValue(cellAddress, cell) Then
    If cell.DataType IsNot Nothing Then
        index = Integer.Parse(cell.InnerText)
        returnValue = sharedStringTable(index)
    Else
        returnValue = CStr(cell.InnerText)
    End If
End If
于 2012-07-05T20:03:37.650 回答
4

这条线看起来很可疑:

Dim cell As Cell = pWorksheetPart.Worksheet.Descendants(Of Cell).Where(Function(c) c.CellReference = cellAddress).FirstOrDefault()

如果 .Where() 条件针对电子表格中的每个单元格执行。随着行数的增加,单元地址比较的数量增加(行 x 列)。即使单元格引用比较操作非常简单,它加起来也很快。

如果显示的 OpenXML 或 Workbook 类不提供方便的 x,y 单元格寻址,您可能必须创建自己的索引。遍历所有单元格以将它们添加到您自己的列列表中,然后您可以通过 x,y 放弃索引。x = 列列表中列列表的索引,y = 列列表中的索引以获取单元格。

于 2012-07-05T20:05:39.210 回答