Skip to content

Instantly share code, notes, and snippets.

@constructor-igor
Created December 2, 2014 16:36
Show Gist options
  • Save constructor-igor/1a87c54f4b512f25c709 to your computer and use it in GitHub Desktop.
Save constructor-igor/1a87c54f4b512f25c709 to your computer and use it in GitHub Desktop.
Powershell to create line chart from EXCEL
#
# http://stackoverflow.com/questions/25331407/powershell-to-create-line-chart-from-excel
#
#Test Data
$Data=("8/15/2014",3091),("8/14/2014",240),("8/13/2014",519),("8/12/2014",622),("8/11/2014",2132),("8/10/2014",1255),("8/9/2014",3240)|ForEach{[PSCustomObject][Ordered]@{'Date_to_Display'=$_[0];'Number_of_Computers'=$_[1]}}
$xlConditionValues=[Microsoft.Office.Interop.Excel.XLConditionValueTypes]
$xlTheme=[Microsoft.Office.Interop.Excel.XLThemeColor]
$xlChart=[Microsoft.Office.Interop.Excel.XLChartType]
$xlIconSet=[Microsoft.Office.Interop.Excel.XLIconSet]
$xlDirection=[Microsoft.Office.Interop.Excel.XLDirection]
$xl = new-object -ComObject Excel.Application
$wb = $xl.workbooks.add()
$ws = $wb.activesheet
$xl.Visible = $true
#Populate test data onto worksheet
$Data |ConvertTo-CSV -NoTypeInformation -Delimiter "`t"| c:\windows\system32\clip.exe
$ws.Range("A1").Select | Out-Null
$ws.paste()
$ws.UsedRange.Columns.item(1).numberformat = "dddd, mmm dd, yyyy"
$ws.UsedRange.Columns.AutoFit() |Out-Null
#Create Chart
$chart=$ws.Shapes.AddChart().Chart
$chart.chartType=$xlChart::xlLine
#modify the chart title
$chart.ChartTitle.Text = "Number of Computers"
$ws.shapes.item("Chart 1").top=40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment