0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

テキストファイルに記載された文字列をExcelから検索して、隣のセルの値を取得するプログラムをpowershellで書く

0
Last updated at Posted at 2023-03-30
$searchString = "検索する文字列"

# Excelファイルのパスを指定
$excelFilePath = "C:\path\to\your\excel\file.xlsx"

# Excelオブジェクトの作成
$excel = New-Object -ComObject Excel.Application

# Excelファイルを開く
$workbook = $excel.Workbooks.Open($excelFilePath)

# 検索を実行するシートを指定
$worksheet = $workbook.Sheets.Item("Sheet1")

# テキストファイルのパスを指定
$textFilePath = "C:\path\to\your\text\file.txt"

# テキストファイルを読み込み、行ごとに処理
Get-Content $textFilePath | ForEach-Object {
    # 行の中に検索文字列がある場合
    if ($_ -match $searchString) {
        # 検索文字列が含まれる列の値を取得
        $cellValue = $worksheet.Cells.Item($_.Split(",")[1], $_.Split(",")[2]).Value2
        Write-Output "検索文字列: $searchString が見つかりました。値: $cellValue"
    }
}

# Excelファイルを閉じる
$workbook.Close($false)

# Excelオブジェクトを解放する
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($worksheet) | Out-Null
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($workbook) | Out-Null
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($excel) | Out-Null
Remove-Variable excel
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?