Powershell - Excel Reports Automation (I)
Powershell - Excel Reports Automation (I)
(I)
Posted on February 18, 2016
These are the basic powershell sentences and functions needed to start generating
our Excel reports using Powershell scripts:
Contents [hide]
$EXCEL.visible = $false
$WORKBOOK = $EXCEL.workbooks.Add()
Reference a worksheet
By index:
$WORKSHEETS = $WORKBOOK.worksheets
$WORKSHEET = $WORKBOOK.worksheets.Item(1)
By name:
$WORKSHEETS = $WORKBOOK.worksheets
$WORKSHEET = $WORKBOOK.worksheets.Item("MAILBOXES")
$WORKSHEET.cells.item(1,1)="Name 1"
$WORKSHEET.cells.item(1,2)="Value 1"
$WORKSHEET.cells.item(2,1)="Name 2"
$WORKSHEET.cells.item(2,2)="Value 2"
Save
$WORKBOOK.SaveAs("D:\MYEXCEL.xlsx")
Finish
$EXCEL.workbooks.Close()
$EXCEL.Application.Quit()
Example of script EXCEL.ps1 that shows the creation of a new excel document
(D:\MYNEWEXCEL.xlsx) and assings values to 4 cells:
$OUPUTFILE = "D:\MYNEWEXCEL.xlsx"
$EXCEL = New-Object -ComObject Excel.Application
$EXCEL.visible = $false
$WORKBOOK = $EXCEL.workbooks.Add()
$WORKSHEETS = $WORKBOOK.worksheets
$WORKSHEET = $WORKBOOK.worksheets.Item(1)
$WORKSHEET.cells.item(2,1)="Name 1"
$WORKSHEET.cells.item(2,2)="Value 1"
$WORKSHEET.cells.item(3,1)="Name 2"
$WORKSHEET.cells.item(3,2)="Value 2"
Final result:
This is the first post related to basic Excel manipulation using Powershell. Please,
wait for the next
This entry was posted in Excel, Powershell and tagged automate, excel, howto,
powershell, tutorial by Sysadmin SomoIT. Bookmark the permalink
[https://somoit.net/powershell/powershell-excel-spreadsheet-mini-tutorial] .