简体   繁体   English

获取 Excel 中使用的最后一个单元格的值

[英]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

我想从图像中读取值 17/11/2017,即 A 列中的最后一行

I have used 3 rows and getting an answer 3 which is true, I wanted to get the value present in 3rd row and as we keep on adding the row, how to append?我已经使用了 3 行并得到了一个正确的答案 3,我想获得第 3 行中的值,并且随着我们继续添加该行,如何追加?

If you want to stick with powershell.如果你想坚持使用powershell。 try this..试试这个..

$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

I'm assuming you already specify $filepath and $sheetname .我假设您已经指定了$filepath$sheetname Don't forget to close the excel after every run.每次运行后不要忘记关闭excel。

This would do it, just change the Sheet1 for your actual Sheet:这样做,只需为您的实际工作表更改 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

The method provided by Shanayl does works fine but bear in mind that its using the count method which doesn't count blanks. Shanayl 提供的方法工作正常,但请记住,它使用不计算空白的计数方法。 Below one does:下面一个是:

$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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM