# new-workbook3.ps1
# Creates a new workbook (with just one sheet), in Excel 2007
# Then we show different cell borders plus add username into a cell
# Thomas Lee - tfl@psp.co.uk
# define enum for line type
$XlLineStyle = @{}
$xllinestyle += @{"xlContinuous" = "1"} # Continuous line.
$xllinestyle += @{"xlDash" = '-4115'} # Dashed line.
$xllinestyle += @{"xlDashDot" = ' 4'} # Alternating dashes and dots.
$xllinestyle += @{"xlDashDotDot" = '5'} # Dash followed by two dots.
$xllinestyle += @{"xlDot" = '-4118'} # Dotted line.
$xllinestyle += @{"xlDouble" = ' -4119'} # Double line.
$xllinestyle += @{"xlLineStyleNone" = ' -4142'} # No line.
$xllinestyle += @{"xlSlantDashDot" = ' 13'} # Slanted dashes.
# Create Excel object
$excel = new-object -comobject Excel.Application
# make Excel visible
$excel.visible = $true
# create a new workbook
$workbook = $excel.workbooks.add()
# default workbook has three sheets, remove 2
# nb: these need to be confirmed in Excel. :-(
$S2 = $workbook.sheets | where {$_.name -eq "Sheet2"}
$s3 = $workbook.sheets | where {$_.name -eq "Sheet3"}
$s3.delete()
$s2.delete()
# get sheet and update sheet name
$s1 = $workbook.sheets | where {$_.name -eq 'Sheet1'}
$s1.name = "PowerShell Sample - Borders"
# set cell values
$s1.range("a1:a1").cells=10
$s1.range("a2:a2").cells=10
$s1.range("a3:A3").cells.formula = "=sum(A1,A2)"
$s1.range("b1:B1").cells=21
$s1.range("b2:b2").cells=21
$s1.range("b3:b3").cells.formula = "=sum(B1,B2)"
$s1.range("D1:D1").cells=2
$s1.range("D2:D2").cells=2
$s1.range("D3:D3").cells.formula = "=sum(d1,d2)"
$S1.range("a5:c5").cells="doodah"
# Give Cell ranges nice nameS
$s1.range("A1:A3").name="Dimsum1"
$s1.range("B1:B3").name="Dimsum2"
$s1.range("d1:d3").name="Dimsum3"
# set different borders
$s1.range("dimsum1").borderaround($xllinestyle.xlContinuous)
$s1.range("dimsum2").borderaround($xllinestyle.xlDashDot)
$s1.range("dimsum3").borderaround($xllinestyle.xldashdotdot)
# Set userid into A7
$uid=[system.security.principal.windowsidentity]::getcurrent()
$rng = $s1.range("A7:A7").cells=$uid.name