Submitted By: Kent Finkle

Adds data to a spreadsheet, then changes the font size, style, and color of that data.

PowerShell
Edit|Remove
$comments = @'
Script name: Add-FormattedData.ps1
Created on: Wednesday, April 18, 2007
Author: Kent Finkle
Purpose: How can I use Windows Powershell to Add Formatted Data to a Spreadsheet?
'@

# -----------------------------------------------------
function Release-Ref ($ref) {
([System.Runtime.InteropServices.Marshal]::ReleaseComObject(
[System.__ComObject]$ref) -gt 0)
[System.GC]::Collect()
[System.GC]::WaitForPendingFinalizers()
}
# -----------------------------------------------------

$objExcel = new-object -comobject excel.application

$objExcel.Visible = $True
$objWorkbook = $objExcel.Workbooks.Add() 
$objWorksheet = $objWorkbook.Worksheets.Item(1) 
$objWorksheet.Cells.Item(1, 1) = "Test value"
$objWorksheet.Cells.Item(1, 1).Font.Bold = $True 
$objWorksheet.Cells.Item(1, 1).Font.Size = 24 
$objWorksheet.Cells.Item(1, 1).Font.ColorIndex = 3

$a = Release-Ref($objWorksheet)
$a = Release-Ref($objWorkbook)
$a = Release-Ref($objExcel)