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?

PowerShellスクリプトでのExcel操作

Last updated at Posted at 2025-04-22

概要

PowerShellスクリプトでのExcel操作方法について
備忘録として記載いたします。

実行環境

windows:Windows11Pro 23H2

注意点

・スクリプト内に日本語の表記が存在しているとエラーとなる場合がある

指定のシートの指定のセルに値が存在するかしないかを判定するプログラム

test.ps1
# Excelアプリケーションを作成
$excel = New-Object -ComObject Excel.Application
$excel.Visible = $false  # Excelを非表示で開く

# Excelファイルを開く
$workbook = $excel.Workbooks.Open("C:\Users\test.xlsx")

# 指定したシートを取得
$sheet = $workbook.Sheets.Item("Sheet1")

# 指定したセルの値を取得
# B3セルをチェック
$cellValue = $sheet.Cells.Item(3,2).Value()  

# 値が存在するか判定
if ($null -eq $cellValue -or $cellValue -eq "") {
    Write-Output "empty"
} else {
    Write-Output "get data >> $cellValue"
}

# Excelを閉じる
$workbook.Close($false)
$excel.Quit()

# COMオブジェクトの解放
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($sheet) | Out-Null
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($workbook) | Out-Null
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($excel) | Out-Null
[GC]::Collect()

ログファイルへの出力

test2.ps1
# ログファイルのパス
$logFilePath = "C:\Users\log.txt"

# Excelアプリケーションを作成
$excel = New-Object -ComObject Excel.Application
$excel.Visible = $false  # Excelを非表示で開く

# Excelファイルを開く
$workbook = $excel.Workbooks.Open("C:\Users\test.xlsx")

# 指定したシートを取得
$sheet = $workbook.Sheets.Item("Sheet1")

# 指定したセルの値を取得(B3セル)
$cellValue = $sheet.Cells.Item(3,2).Value()

# 値が存在するか判定
if ($null -eq $cellValue -or $cellValue -eq "") {
    Write-Output "empty"
} else {
    Write-Output "get data >> $cellValue"

    # ログファイルへ値を記載
    $logEntry = "data : $cellValue"
    
    Add-Content -Path $logFilePath -Value $logEntry
}

# Excelを閉じる
$workbook.Close($false)
$excel.Quit()

# COMオブジェクトの解放
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($sheet) | Out-Null
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($workbook) | Out-Null
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($excel) | Out-Null
[GC]::Collect()

日本語のファイル名やシート名に対応したスクリプト

※下記のスクリプトを実行前に
ファイルの拡張子を.txtにして
メモ帳を開いて「名前を付けて保存」の選択時に「エンコード」の項目から「UTF-8(BOM 付き)」を選択する。
そして再度拡張子を.ps1にして
スクリプトを実行する

test.ps1
# ログファイルのパス
$logFilePath = "C:\Users\log.txt"

# Excelアプリケーションを作成
$excel = New-Object -ComObject Excel.Application
$excel.Visible = $false  # Excelを非表示で開く

# 日本語ファイル名の場合のファイルパス設定
$excelFilePath = "C:\Users\テスト.xlsx"
$workbook = $excel.Workbooks.Open($excelFilePath)

# 日本語のシート名に対応
$sheetName = "データシート"  # 例: 日本語のシート名
$sheet = $workbook.Worksheets($sheetName)

# 指定したセルの値を取得(B3セル)
$cellValue = $sheet.Cells.Item(3,2).Value()

# 値が存在するか判定
if ($null -eq $cellValue -or $cellValue -eq "") {
    Write-Output "empty"
} else {
    Write-Output "get data >> $cellValue"

    # ログファイルへ値を記載
    $timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
    $logEntry = "$timestamp - : $cellValue"
    
    Add-Content -Path $logFilePath -Value $logEntry -Encoding UTF8
}

# Excelを閉じる
$workbook.Close($false)
$excel.Quit()

# COMオブジェクトの解放
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($sheet) | Out-Null
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($workbook) | Out-Null
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($excel) | Out-Null
[GC]::Collect()

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?