答案 0 :(得分:1)
如果你想坚持使用powershell。试试这个..
$objExcel = New-Object -ComObject Excel.Application
$objExcel.Visible = $False
$WorkBook = $objExcel.Workbooks.Open($filepath)
$WorkSheet = $objExcel.WorkSheets.item($sheetname)
$WorkSheet.activate()
[int]$lastRowvalue = ($WorkSheet.UsedRange.rows.count + 1) - 1
$lastrow = $WorkSheet.Cells.Item($lastRowvalue, 1).Value2
write-host $lastrow
$objExcel.Quit()
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($objExcel) | out-null
我假设您已指定$filepath
和$sheetname
。每次运行后都不要忘记关闭excel。
答案 1 :(得分:0)
这样做,只需更改实际工作表的Sheet1:
LastRow = Sheet1.Cells(Sheet1.Rows.Count, "A").End(xlUp).Row
'This will find the last used row on column A of Sheet1
value = Sheet1.cells(LastRow,1).value
'Place the value of that cell into a variable
Msgbox value
'Display the variable to the user in a msgbox
答案 2 :(得分:0)
Shanayl提供的方法确实可以正常工作,但请记住,它使用的count方法不会计算空白。以下一项:
$filepath = "F:\Drives.xlsx"
$xl=New-Object -ComObject "Excel.Application"
$wb=$xl.Workbooks.open($filepath)
$ws=$wb.ActiveSheet
$cells=$ws.Cells
$Lastrowno =$Ws.UsedRange.SpecialCells(11).row