Reputation: 55
I'm trying to pull from a total of 6 columns from an excel worksheet. I'm using a modified script from another post on stack, it looks like this.
$strPath = "C:\Users\User\Documents\EXEL\fj.xls"
$AssetInv = "C:\Users\User\Documents\EXEL\fj.txt"
$objExcel = New-Object -ComObject Excel.Application
$objExcel.Visible = $false
$WorkBook = $objExcel.Workbooks.Open($strPath)
$worksheet = $workbook.sheets.item("Daily Price Quote - Better Of -")
$intRowMax = ($worksheet.UsedRange.Rows).count
#$StartRow = 2
#$site = 1
#$state = 3
#$retailprice = 18
#$yourprice = 20
#$SavingsTotal = 21
Write "`Site City State retailprice yourprice savingstotal" | Out-File $AssetInv
Write "--------------- ------------------- -------------------- -----------------------------" |
Out-FIle $AssetInv -Append
Write-Host "Processing: " $intRowMax "rows"
for ($intRow = 7 ; $intRow -le $intRowMax ; $intRow++) {
$site = $worksheet.cells.item($intRow, 10).value2
$city = $worksheet.cells.item($intRow, 2).value2
$state = $worksheet.cells.item($intRow, 3).value2
$retailprice = $worksheet.cells.item($intRow, 18).value2
$yourprice = $worksheet.cells.item($intRow, 20).value2
$SavingsTotal = $worksheet.cells.item($intRow, 21).value2
if (($site -ge 1 )) {
"{0, -15} {1, -30} {2, -25} {3, -25}" -f $site, $city, $state, $retailprice, $yourprice, $Savingstotal |
Out-File $AssetInv -Append
}
}
$objexcel.quit()
Currently I am pulling no data, earlier I had it working for a maximum of 3 records. Any insight as to what I am doing wrong?
Upvotes: 3
Views: 18049
Reputation: 36332
Ok, we'll start with your code to get the worksheet we want. Then we'll select the UsedRange to get the range of cells that have data. Then we'll run that through a ForEach loop, skipping the first 5 rows since they have garbage/header info. For each row we'll make a new object, with properties set to the associated cell (kind of like what you do with all your variables). All of those objects will be collected in an array, which we output to a CSV file. If you don't like the CSV, you could pipe it to Format-Table, and pipe that to Out-File (might have to pipe to out-string, then out-file... it's not something I do often).
$strPath = "C:\Users\User\Documents\EXEL\fj.xls"
$AssetInv = "C:\Users\User\Documents\EXEL\fj.txt"
$objExcel = New-Object -ComObject Excel.Application
$objExcel.Visible = $false
$WorkBook = $objExcel.Workbooks.Open($strPath)
$worksheet = $workbook.sheets.item("Daily Price Quote - Better Of -")
$UsedRange = $worksheet.usedrange
$Data = ForEach($Row in ($UsedRange.Rows|Select -skip 5)){
New-Object PSObject -Property @{
'Site' = $Row.Cells.Item(1).Value2
'City' = $Row.Cells.Item(2).Value2
'State' = $Row.Cells.Item(3).Value2
'Retail Price' = $Row.Cells.Item(18).Value2
'Your Price' = $Row.Cells.Item(20).Value2
'Total Savings' = $Row.Cells.Item(21).Value2
}
}
$Data | Where{[int]::Parse($_.Site) -ge 1} | Select Site,City,State,'Retail Price','Your Price','Total Savings' | Export-Csv -NoTypeInformation -Path $AssetInv
$objExcel.quit()
The plus side is that you also are left with $Data which is all the data you want to be able to use in case you need to do anything else with it (look for items that you save over X%, or items that cost less than $5 or whatever.
Upvotes: 4