5

我有一个 Excel 表,如下所示:

Id   Name   Address
-------------------
1    t1     Add1
2           Add2
3

我想获取每列中非空单元格的数量,即:

列 ID → 3 行
名称 → 1 行
地址 → 2 行

如何在 power shell 中执行此操作?我尝试了各种方法,但它们似乎不起作用。

我是 PowerShell 的新手。有没有办法直接获取列的行数?

$SheetName = "Sheet1"
$Excel = New-Object -ComObject "Excel.Application"

$Workbook = $Excel.workbooks.open($XLSDoc)
$Sheet = $Workbook.Worksheets.Item($SheetName)
$objRange = $Sheet.UsedRange
$RowCount = $objRange.Rows.Count
$ColumnCount = $objRange.Columns.Count
Write-Host "RowCount:" $RowCount
Write-Host "ColumnCount" $ColumnCount
$Range1 = $Sheet.Range("A1:A5")
$TestCount = $Range1.Rows.Count
Write-Host $TestCount
4

1 回答 1

12

尝试这样的事情:

$xl = New-Object -COM "Excel.Application"
$xl.Visible = $true

$wb = $xl.Workbooks.Open("C:\path\to\your.xlsx")
$ws = $wb.Sheets.Item(1)

$rows = $ws.UsedRange.Rows.Count

foreach ( $col in "A", "B", "C" ) {
  $xl.WorksheetFunction.CountIf($ws.Range($col + "1:" + $col + $rows), "<>") - 1
}

$wb.Close()
$xl.Quit()
于 2013-06-05T11:27:09.947 回答