我正在 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