본문 바로가기
파워쉘(Powershell)/파일,폴더

파워쉘 - 엑셀(Excel)에 쓰기

by 예배파 2024. 4. 19.

 

엑셀 실행후 데이터 넣기

$excel = New-Object -ComObject Excel.Application
$excel.Visible = $true
$workbook = $excel.Workbooks.add()
$sheet = $workbook.worksheets.Add()
$sheet.Name = "First"
$sheet.Cells.Item(1,1) = '1,1'
$sheet.Cells.Item(1,2) = '1,2'
$sheet.Cells.Item(2,1) = '2,1'

 

 

 

Cell 색깔 바꾸기

$sheet.Cells.Item(1,1) = '1,1'
$sheet.Cells.Item(1,1).Interior.ColorIndex = 44

 

 

 

Color Index

 

 

 

사용예

$excel = New-Object -ComObject Excel.Application
$excel.Visible = $true
$workbook = $excel.Workbooks.add()
$sheet = $workbook.worksheets.Add()
$sheet.Name = "First"

1..5 | %{
    $i = $_
    1..4 | %{
        $j = $_
            $value = "({0},{1})" -f $i, $j
            $sheet.Cells.Item($i,$j) = $value
            if($i % 2 -eq 0){
                $sheet.Cells.Item($i,$j).Interior.ColorIndex = 44
            }
    }
}