powershell excel获取第一行(标题)列计数

powershell excel获取第一行(标题)列计数,excel,powershell,Excel,Powershell,我试图计算第一行(A1-D1)的单元格编号,这被称为header,并将该编号作为计数器 一直以来,我们都在使用Usedrange来计算列数,以找到大多数列: $headercolcount=($worksheet.UsedRange.Columns).count 但是UsedRange将捕获整个activesheet中的最大计数,如果标题下有额外的内容数据,这将导致与第一行中的列计数不同 我只想抓住第一排: [] 更新: 为了获得更清晰的视图,下面是一个示例。 由于1F和1G不存在值,因此答

我试图计算第一行(A1-D1)的单元格编号,这被称为header,并将该编号作为计数器

一直以来,我们都在使用
Usedrange
来计算列数,以找到大多数列:

$headercolcount=($worksheet.UsedRange.Columns).count 
但是
UsedRange
将捕获整个activesheet中的最大计数,如果标题下有额外的内容数据,这将导致与第一行中的列计数不同

我只想抓住第一排:

[]

更新: 为了获得更清晰的视图,下面是一个示例。 由于1F和1G不存在值,因此答案应为5,因为它包含数据。那么我应该如何正确地抓住5

[]

这将显示您有多少行,并显示第一行中的所有值,以及您的标题

    Get-Process excel | Stop-Process -Force
# Specify the path to the Excel file and the WorkSheet Name
$FilePath = "C:\temp\A_A.xlsx"
$SheetName = "Blad1" # In english this is probably Sheet1
# Create an Object Excel.Application using Com interface
$objExcel = New-Object -ComObject Excel.Application
# Disable the 'visible' property so the document won't open in excel
$objExcel.Visible = $false
$objExcel.DisplayAlerts = $false
# Open Excel file and in $WorkBook
$WorkBook = $objExcel.Workbooks.Open($FilePath)
# Load WorkSheet 'Blad 1' in variable Worksheet
$WorkSheet = $WorkBook.sheets.item($SheetName)
$xlup = -4162
$lastRow = $WorkSheet.cells.Range("A1048576").End($xlup).row

# get the highest amount of columns
$colMax = ($WorkSheet.UsedRange.Columns).count
# initiatie a counter
$count = $null
# set the column you'd like to count
$row = 1

for ($i = 0; $i -le $colMax; $i++){
    if($worksheet.rows.Item("$row").columns.Item($i+1).text){
        $count++
    }    
}

$count
这应该行得通。它使用最多的列。然后循环直到达到这个数量。在循环过程中,它检查该行上的单元格是否已填充,如果已填充,则添加到计数器

如果你有数百万行,这可能不是最好的方式,但这对我来说很有用

我用excel文件对其进行了测试:


不熟悉PowerShell,但您需要类似于
$headercolcount=$worksheet.UsedRange.Rows(1).Columns.Count的东西
UsedRange
始终是一个矩形区域,因此
$worksheet.UsedRange.Columns.Count
将是等效的这就是我的意思,我还尝试了你之前提出的$headercolcount=$sheet.UsedRange.Rows(1.Columns.Count),但它不起作用,它不会对列进行计数,因此我认为会对字符进行计数,因为这会产生较大的值。不起作用,仍然无法捕获包含数据的列的第一行数。是的,所以“不起作用”->信息量不大。您是否将$Sheetname更改为英文格式?如果你在另一种语言中表现出色,也可以用你的语言。是的,我把它命名为Book1。我试着对上面更新的示例excel文件运行您的代码。包含数据的第一行是5。但是,您的代码显示列数=6作为代码计数,直到1F为止,我认为是这样。对不起,如果有任何不好的解释。这里是结果:
write host”上次使用的行:“$lastRow
上次使用的行:4
write host”列的数量“$amountofcolumns
列的数量6
对于($i=1;$i-le$amountofcolumns;$i++){$sheet.Cells.Item(1,$i).text}
s sd dsd dsd 21321如上所述,UsedRange始终是一个常规的矩形范围-它将包含工作表上的所有数据,并向右延伸到任何行上最右边的条目。它没有每行不同的列数
    Get-Process excel | Stop-Process -Force
# Specify the path to the Excel file and the WorkSheet Name
$FilePath = "C:\temp\A_A.xlsx"
$SheetName = "Blad1" # In english this is probably Sheet1
# Create an Object Excel.Application using Com interface
$objExcel = New-Object -ComObject Excel.Application
# Disable the 'visible' property so the document won't open in excel
$objExcel.Visible = $false
$objExcel.DisplayAlerts = $false
# Open Excel file and in $WorkBook
$WorkBook = $objExcel.Workbooks.Open($FilePath)
# Load WorkSheet 'Blad 1' in variable Worksheet
$WorkSheet = $WorkBook.sheets.item($SheetName)
$xlup = -4162
$lastRow = $WorkSheet.cells.Range("A1048576").End($xlup).row

# get the highest amount of columns
$colMax = ($WorkSheet.UsedRange.Columns).count
# initiatie a counter
$count = $null
# set the column you'd like to count
$row = 1

for ($i = 0; $i -le $colMax; $i++){
    if($worksheet.rows.Item("$row").columns.Item($i+1).text){
        $count++
    }    
}

$count
$row = 1 this will give : 5
$row = 2 this will give : 6
$row = 3 this will give : 7
$row = 4 this will give : 8