2

我正在通过 VB 6 读取 Excel 文件,如下所示

Dim objExcel As Excel.Application
Dim objWorkbook As Excel.Workbook
Dim objWorksheet As Excel.Worksheet
Dim ExlUsedRowCount As Long

Set objExcel = CreateObject("Excel.Application")
Set objWorkbook = objExcel.Workbooks.Open(FileName, , True)
Set objWorksheet = objWorkbook.Sheets(1)
ExlUsedRowCount = objWorksheet.UsedRange.Rows.Count

For RowIndex = 1 To ExlUsedRowCount
    someVariable = Trim$(objWorksheet.Cells(RowIndex, 9))
Next

objWorkbook.Close False
Set objExcel = Nothing
Set objWorkbook = Nothing
Set objWorksheet = Nothing

我有 Excel 2013,此代码在我的机器上每秒读取 270 行 (),而在客户端 (Excel 2010) 上它仅读取 33/秒。我看过进程列表,除了 EXCEL.EXE 之外没有其他进程消耗超过 80% 的 CPU。客户端有 4 台 PC 就是这种情况。

我尝试将此文件保存为 CSV,但有格式为 DD/MM 的日期列,当保存为 CSV 时,YEAR 完全被省略。有什么线索吗?

请指教可能有什么问题

4

1 回答 1

1

尝试这个:

    Dim objExcel As Excel.Application
    Dim objWorkbook As Excel.Workbook
    Dim objWorksheet As Excel.Worksheet
    Dim lngLoop      As Long
    Dim varArrTemp      as Variant
    Dim rngData         as range

    Set objExcel = CreateObject("Excel.Application")
    Set objWorkbook = objExcel.Workbooks.Open(FileName, , True)
    Set objWorksheet = objWorkbook.Sheets(1)

    Set rngData   = objWorksheet.UsedRange.columns(9)
    varArrtemp =rngData
    For lngLoop = Lbound(varArrTemp) To Ubound(varArrtemp)
        varArrtemp (lngLoop,9)= Trim$( varArrtemp (lngLoop,9))
    Next
    rngData   = varArrTemp

    objWorkbook.Close False
    Set objExcel = Nothing
    Set objWorkbook = Nothing
    Set objWorksheet = Nothing
于 2013-06-26T10:45:34.413 回答