[英]To get the value of last cell used in Excel
$objExcel = New-Object -ComObject Excel.Application
$objExcel.Visible = $false
$WorkBook = $objExcel.Workbooks.Open($filepath)
$WorkBook.Sheets | Select-Object -Property Name
$WorkSheet = $WorkBook.Sheets.Item($sheetname)
$worksheetrange = $WorkSheet.UsedRange
$last = $worksheetrange.Rows.Count
我已經使用了 3 行並得到了一個正確的答案 3,我想獲得第 3 行中的值,並且隨着我們繼續添加該行,如何追加?
如果你想堅持使用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。
這樣做,只需為您的實際工作表更改 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
Shanayl 提供的方法工作正常,但請記住,它使用不計算空白的計數方法。 下面一個是:
$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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.