0

What I need to do is to extract the data in the excel row and output them into different rows on Excel. After that, I will need to use the extracted data and perform certain conditions on the extracted data.

This is my current script:

  1. To open excel and apply the formulas
$excel = New-Object -ComObject excel.application
$filepath = 'D:\testexcel.xlsx'
$workbook = $excel.workbooks.open("$filepath")
$worksheet = $workbook.worksheets.item(1)

$excel.Visible = $true

$rows = $worksheet.range("A1").currentregion.rows.count 
$worksheet.range("S1:S$rows").formula = $worksheet.range("S1").formula 
  1. Function to find the row, apply the formula and output it
function test123(){
    param([string]$test123)

    $sourcefile = "D:\testexcel.xlsx"
    $sheetname = "abc"

    $excel = new-object -comobject excel.application
    $excel.Visible = $true
    $excelworkbook = $excel.Workbooks.open($sourcefile, 2, $true) 
    $excelworksheet = $excelworkbook.worksheets.item($sheetname)

    $row = 1
    $column = 1
    $found = $false
    while(($excelworksheet.cells.item($row, $column).value() -ne $null) -and($found -eq $false)){ 
        if(($excelworksheet.cells.item($row, $column).value()).toupper() -eq $test123.ToUpper()){
            write-host $excelworksheet.cells.item($row, $column).value() $excelworksheet.cells.item($row, $column+1).value(), 
            $excelworksheet.cells.item($row, $column +2).value() $found = $true
            }
        $row += 1
    }

    #close workbook
    $excelworkbook.close()
    $excel.quit()
}
test123 -test123 "Test123"

Please guide me and tell me if this is the right way to do it... Thanks

1 Answer 1

4

Please have a look into the ImportExcel module by Douge Finke. This module has the capability to do what you need.

Get it from PowerShell gallery: Install-Module -Name ImportExcel

Github link: https://github.com/dfinke/ImportExcel

you can then do Get-Help Import-Excel -Examples which has pretty good examples.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.