;path and file name to open
$FileName = "c:\test.xls"
;create Excel Object
$ObjExcel = CreateObject("Excel.Application")
$ObjExcel.Visible = "True"
;don't display alerts
$ObjExcel.DisplayAlerts = "False"
;to have user interaction: show excel openfile dialog
;$ObjWorkbook = $ObjExcel.GetOpenFilename
;to open a predetermined file if it exists
If Exist($FileName)
;set $ObjWorkbook = the FileName to be opened and open
$ObjWorkbook = $ObjExcel.Workbooks.Open($FileName)
Else
$nul = MessageBox($FileName + ' does not exist and will not be opened', 'Error Opening File,..', 16)
;Exit excel If needed
$nul = $ObjExcel.Quit
EndIf
;identify worksheet where Worksheets(1) identifes the first sheet,
;could be changed to Worksheets(2) to identify the second sheet
$ObjWorksheet = $ObjWorkbook.Worksheets(1)
;** line styles **
;Continuous 1
;Dash -4115
;DashDot 4
;DashDotDot 5
;Dot -4118
;Double -4119
;LineStyleNone -4142
;SlantDashDot 13
$LineStyle = 1
;** line thickness **
;Hairline 1
;Thin 2
;Medium -4138
;Thick 4
$LineThickness = 2
;** border style **
;DiagonalDown 5
;DiagonalUp 6
;EdgeBottom 9
;EdgeLeft 7
;EdgeRight 10
;EdgeTop 8
;InsideHorizontal 12
;InsideVertical 11
;All 13
$BorderStyle = 13
;** horizontal alignment **
;left -4131
;right -4152
;center -4108
$HorizontalAlignment = - 4108
;** vertical alignment **
;top -4160
;bottom -4107
;center -4108
$VerticalAlignment = - 4108
;add a Value to row 1, column 1, wrap text, set to bold, set font size and font name
$ObjWorksheet.Cells(1, 1).Value = "This is the cell we want to wrap text"
$ObjWorksheet.Cells(1, 1).WrapText = "True"
$ObjWorksheet.Cells(1, 1).Font.Bold = "True"
$ObjWorksheet.Cells(1, 1).Font.Size = 8
$ObjWorksheet.Cells(1, 1).Font.Name = "Times New Roman"
;add cell in column 8, set to bold, set font size and turn on autofit for column 8
$ObjWorksheet.Cells(1, 8).Value = "This is the row we want to autofit"
$ObjWorksheet.Cells(1, 8).Font.Bold = "True"
$ObjWorksheet.Cells(1, 8).Font.Size = 18
$nul = $ObjWorksheet.Columns(8).AutoFit = "True"
;add Values to individual cells, HorizontalAlignment and VerticalAlignment
;$ObjWorksheet.Cells(Row,Column).Value = "A"
$ObjWorksheet.Cells(2, 1).Value = "A"
;left
$ObjWorksheet.Cells(2, 1).HorizontalAlignment = - 4131
;top
$ObjWorksheet.Cells(2, 1).VerticalAlignment = - 4160
$ObjWorksheet.Cells(2, 2).Value = "B"
;center
$ObjWorksheet.Cells(2, 2).HorizontalAlignment = - 4108
;center
$ObjWorksheet.Cells(2, 2).VerticalAlignment = - 4108
$ObjWorksheet.Cells(2, 3).Value = "C"
;right
$ObjWorksheet.Cells(2, 3).HorizontalAlignment = - 4152
;bottom
$ObjWorksheet.Cells(2, 3).VerticalAlignment = - 4107
;set row 2 to bold
; $ObjExcel.Rows(2).Font.Bold = "True"
;set column 2 to bold
; $ObjExcel.Columns(2).Font.Bold = "True"
;set a range of cells A2, B2, C2 to bold
$ObjWorksheet.Range("A2", "C2").Font.Bold = "True"
;set all used cells to bold
; $ObjWorksheet.UsedRange.Font.Bold = "True"
;set single cell horizontal alignment
$ObjWorksheet.Cells(1, 1).HorizontalAlignment = $HorizontalAlignment
;set single cell font color
$ObjWorksheet.Cells(1, 1).Font.ColorIndex = 3
;add multiple cells with multiple properties
$index = 1
For $col = 1 to 7
For $row = 3 to 10
;set HorizontalAlignment to center
$ObjWorksheet.Cells($row, $col).HorizontalAlignment = $HorizontalAlignment
;set VerticalAlignment to center
$ObjWorksheet.Cells($row, $col).VerticalAlignment = $VerticalAlignment
;add pattern color
$ObjWorksheet.Cells($row, $col).Interior.ColorIndex = $index
;set cell Value to index color
$ObjWorksheet.Cells($row, $col).Value = $index
;add border
If $BorderStyle = 13
;set all borders
$ObjWorksheet.Cells($row, $col).Borders.LineStyle = $LineStyle
$ObjWorksheet.Cells($row, $col).Borders.ColorIndex = 1
$ObjWorksheet.Cells($row, $col).Borders.Weight = $LineThickness
Else
;set select border
$ObjWorksheet.Cells($row, $col).Borders($BorderStyle).LineStyle = $LineStyle
$ObjWorksheet.Cells($row, $col).Borders($BorderStyle).ColorIndex = 1
$ObjWorksheet.Cells($row, $col).Borders($BorderStyle).Weight = $LineThickness
EndIf
$index = $index + 1
Next
Next
;get used Column count
$UsedColumns = $ObjWorksheet.UsedRange.Columns.Count
;get used Row count
$UsedRows = $ObjWorksheet.UsedRange.Rows.Count
;add a new row of cells
;this will add a new row of cells every time this code is run
$ObjWorksheet.Cells($UsedRows + 1, 1).Value = "New Row Cell 1"
$ObjWorksheet.Cells($UsedRows + 1, 2).Value = "New Row Cell 2"
$ObjWorksheet.Cells($UsedRows + 1, 3).Value = "New Row Cell 3"
;add a new column to existing cells
;this will add a new column of cells every time this code is run
$ObjWorksheet.Cells(3, $UsedColumns + 1).Value = "New Column Cell 1"
$ObjWorksheet.Cells(4, $UsedColumns + 1).Value = "New Column Cell 2"
$ObjWorksheet.Cells(5, $UsedColumns + 1).Value = "New Column Cell 3"
;save workBook as FileName
$nul = $ObjWorkbook.SaveAs($FileName)
;exit excel
$nul = $ObjExcel.Quit