Excel Interop - 查找列值的总和

Excel Interop - Find Sum of a Column Values

我有以下调用和使用 Excel 利用 Excel Interop 的方法:

Dim ExcelTestSetOutput, Sheet1TestSetOutput
ExcelTestSetOutput = CreateObject(“Excel.Application”) ' Open Excel
ExcelTestSetOutput.WorkBooks.Add() ' Add a new workbook

'Get the first worksheet.
 Sheet1TestSetOutput = ExcelTestSetOutput.ActiveSheet

Sheet1TestSetOutput.Cells(RowTestSetOutput, 1).Value = "ID"                  ' A
Sheet1TestSetOutput.Cells(RowTestSetOutput, 2).Value = "Parent Path"                     ' B 
Sheet1TestSetOutput.Cells(RowTestSetOutput, 3).Value = "Name"                   ' C
Sheet1TestSetOutput.Cells(RowTestSetOutput, 4).Value = "Last Modified"             ' D
Sheet1TestSetOutput.Cells(RowTestSetOutput, 5).Value = "Script Size"                   ' E

Dim n
For n = 1 To RecSetTestSet.RecordCount
  Sheet1TestSetOutput.Cells(RowTestSetOutput + 1, 1).Value = RecSetTestSet.FieldValue(0)
  Sheet1TestSetOutput.Cells(RowTestSetOutput + 1, 2).Value = RecSetTestSet.FieldValue(1)
  Sheet1TestSetOutput.Cells(RowTestSetOutput + 1, 3).Value = RecSetTestSet.FieldValue(2)
  Sheet1TestSetOutput.Cells(RowTestSetOutput + 1, 4).Value = RecSetTestSet.FieldValue(3)
  Sheet1TestSetOutput.Cells(RowTestSetOutput + 1, 5).Value = RecSetTestSet.FieldValue(4)


  RowTestSetOutput = RowTestSetOutput + 1
  RecSetTestSet.Next()

Next

Dim eTotal As Long

' eTotal = Application.WorksheetFunction.Sum(columns((rReportData.column))
' eTotal = SUM(Sheet1TestSetOutput.Range("E2:E"))
' eTotal = ExcelTestSetOutput.ActiveSheet.Sum("E2:E")
' eTotal = Sheet1TestSetOutput.Range("E:E").FormulaR1C1("sum(R2E1:R100E1)")

一切正常,除了我无法获得 eTotal 的值。

eTotal 需要对 E 列的所有值求和。E 列的最后一行编号可能不同。

我注释掉了 4 行代码,其中我试图对 E 列求和以将值赋给 eTotal 变量,但错误消息指出该函数在 Worksheeet 下不受支持。希望得到您的反馈。

eTotal = ExcelTestSetOutput.Evaluate("=SUM(E:E)")