$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
More than 3 years have passed since last update.
テキストファイルに記載された文字列をExcelから検索して、隣のセルの値を取得するプログラムをpowershellで書く
0
Last updated at Posted at 2023-03-30
Register as a new user and use Qiita more conveniently
- You get articles that match your needs
- You can efficiently read back useful information
- You can use dark theme