Displaying charts in Excel using PowerShell
up vote
-2
down vote
favorite
I have read an input CSV file and generated a chart in PowerShell form.
This is my code:
$datasource = Import-Csv "D:UsersjanajaDesktoptest.csv"
[void] [Reflection.Assembly]::loadWithPartialName('System.Windows.Forms')
[void][Reflection.Assembly]::loadWithPartialName('System.Windows.Forms.DataVisualization')
$datasource = "D:UsersjanajaDesktoptest.csv"
$outputXLSX = "D:UsersjanajaDesktoptest.xlsx"
$excel = New-Object -ComObject excel.application
$xlChart=[Microsoft.Office.Interop.Excel.XLChartType]
$workbook = $excel.Workbooks.Add(1)
$worksheet = $workbook.worksheets.Item(1)
$TxtConnector = ("TEXT;" + $datasource)
$Connector = $worksheet.QueryTables.add($TxtConnector,$worksheet.Range("A1"))
$query = $worksheet.QueryTables.item($Connector.name)
$query.TextFileOtherDelimiter = $Excel.Application.International(5)
$query.AdjustColumnWidth = 1
$query.Refresh()
$query.Delete()
$Workbook.SaveAs($outputXLSX,51)
$i = 1
$j=1
while($j -le 20){
$excel.Cells.Item($i, $j).Font.ColorIndex = 3
$excel.Cells.Item($i, $j).Font.Bold = $True
$j++
}
$Chart = New-Object -TypeName
System.Windows.Forms.DataVisualization.Charting.Chart
$Chart.Size = '600,750'
$ChartArea = New-Object -TypeName
System.Windows.Forms.DataVisualization.Charting.ChartArea
$ChartArea.AxisX.Title = 'VM Name'
$ChartArea.AxisY.Title = 'CPU and Memory Utilization'
$ChartArea.AxisX.lnterval = '1'
$ChartArea.AxisX.LabelStyle.Enabled = $true
$ChartArea.AxisX.LabelStyle.Angle = 90
$Chart.ChartAreas.Add($ChartArea)
$Chart.Series.Add('Memory')
$Chart.Series.Add('CPU')
$Chart.Series['Memory'].ChartType =
[System.Windows.Forms.DataVisualization.Charting.SeriesChartType]::Bar
$Chart.Series['CPU'].ChartType =
[System.Windows.Forms.DataVisualization.Charting.SeriesChartType]::Bar
Foreach($VM in $datasource)
{
$var1 = $Chart.Series['Memory'].Points.AddXY($VM.VMName,$VM.MemoryGB)
}
Foreach($CPU in $datasource)
{
$var2 = $Chart.Series['CPU'].Points.AddXY($VM.VMName,$VM.CPU)
}
$Title = New-Object -TypeName
System.Windows.Forms.DataVisualization.Charting.Title
$Chart.Titles.Add($Title)
$Chart.Titles[O].Text = 'VM details'
$Chart.Anchor = [System.Windows.Forms.AnchorStyles]::Bottom -bor
[System.Windows.Forms.AnchorStyles]::Right -bor
[System.Windows.Forms.AnchorStyles]::Top -bor
[System.Windows.Forms.AnchorStyles]::Left
$Form = New-Object Windows.Forms.Form
$Form.Text= "VM Memory and CPU usage Chart"
$Form.Width = 600
$Form.Height = 600
$Form.controls.add($Chart)
$Form.Add_Shown({$Form.Activate()})
$Form.ShowDialog()
$Chart.Savelmage($Env:USERPROFILE + "DesktopChart.png", "PNG")
This code converts the file from CSV to Excel format and then reads the CSV file data to display a chart on the Powershell Form.
How do I Generate the same chart which takes only 3 column values out of 14 columns and display a clustered bar chart?
winforms powershell csv charts
add a comment |
up vote
-2
down vote
favorite
I have read an input CSV file and generated a chart in PowerShell form.
This is my code:
$datasource = Import-Csv "D:UsersjanajaDesktoptest.csv"
[void] [Reflection.Assembly]::loadWithPartialName('System.Windows.Forms')
[void][Reflection.Assembly]::loadWithPartialName('System.Windows.Forms.DataVisualization')
$datasource = "D:UsersjanajaDesktoptest.csv"
$outputXLSX = "D:UsersjanajaDesktoptest.xlsx"
$excel = New-Object -ComObject excel.application
$xlChart=[Microsoft.Office.Interop.Excel.XLChartType]
$workbook = $excel.Workbooks.Add(1)
$worksheet = $workbook.worksheets.Item(1)
$TxtConnector = ("TEXT;" + $datasource)
$Connector = $worksheet.QueryTables.add($TxtConnector,$worksheet.Range("A1"))
$query = $worksheet.QueryTables.item($Connector.name)
$query.TextFileOtherDelimiter = $Excel.Application.International(5)
$query.AdjustColumnWidth = 1
$query.Refresh()
$query.Delete()
$Workbook.SaveAs($outputXLSX,51)
$i = 1
$j=1
while($j -le 20){
$excel.Cells.Item($i, $j).Font.ColorIndex = 3
$excel.Cells.Item($i, $j).Font.Bold = $True
$j++
}
$Chart = New-Object -TypeName
System.Windows.Forms.DataVisualization.Charting.Chart
$Chart.Size = '600,750'
$ChartArea = New-Object -TypeName
System.Windows.Forms.DataVisualization.Charting.ChartArea
$ChartArea.AxisX.Title = 'VM Name'
$ChartArea.AxisY.Title = 'CPU and Memory Utilization'
$ChartArea.AxisX.lnterval = '1'
$ChartArea.AxisX.LabelStyle.Enabled = $true
$ChartArea.AxisX.LabelStyle.Angle = 90
$Chart.ChartAreas.Add($ChartArea)
$Chart.Series.Add('Memory')
$Chart.Series.Add('CPU')
$Chart.Series['Memory'].ChartType =
[System.Windows.Forms.DataVisualization.Charting.SeriesChartType]::Bar
$Chart.Series['CPU'].ChartType =
[System.Windows.Forms.DataVisualization.Charting.SeriesChartType]::Bar
Foreach($VM in $datasource)
{
$var1 = $Chart.Series['Memory'].Points.AddXY($VM.VMName,$VM.MemoryGB)
}
Foreach($CPU in $datasource)
{
$var2 = $Chart.Series['CPU'].Points.AddXY($VM.VMName,$VM.CPU)
}
$Title = New-Object -TypeName
System.Windows.Forms.DataVisualization.Charting.Title
$Chart.Titles.Add($Title)
$Chart.Titles[O].Text = 'VM details'
$Chart.Anchor = [System.Windows.Forms.AnchorStyles]::Bottom -bor
[System.Windows.Forms.AnchorStyles]::Right -bor
[System.Windows.Forms.AnchorStyles]::Top -bor
[System.Windows.Forms.AnchorStyles]::Left
$Form = New-Object Windows.Forms.Form
$Form.Text= "VM Memory and CPU usage Chart"
$Form.Width = 600
$Form.Height = 600
$Form.controls.add($Chart)
$Form.Add_Shown({$Form.Activate()})
$Form.ShowDialog()
$Chart.Savelmage($Env:USERPROFILE + "DesktopChart.png", "PNG")
This code converts the file from CSV to Excel format and then reads the CSV file data to display a chart on the Powershell Form.
How do I Generate the same chart which takes only 3 column values out of 14 columns and display a clustered bar chart?
winforms powershell csv charts
3
There is now way of displaying a chart in a csv file. Only text is possible.
– TobyU
Nov 19 at 12:23
3
please share your code here , instead of image
– Ahmed Sunny
Nov 19 at 12:30
This should not be receiving close votes, guys. "You can't do that" is absolutely a valid answer to a question. The only thing wrong here is posting code as an image.
– Bacon Bits
Nov 19 at 13:44
@BaconBits I CV'd because of the code screenshot.
– Ansgar Wiechers
Nov 19 at 14:37
add a comment |
up vote
-2
down vote
favorite
up vote
-2
down vote
favorite
I have read an input CSV file and generated a chart in PowerShell form.
This is my code:
$datasource = Import-Csv "D:UsersjanajaDesktoptest.csv"
[void] [Reflection.Assembly]::loadWithPartialName('System.Windows.Forms')
[void][Reflection.Assembly]::loadWithPartialName('System.Windows.Forms.DataVisualization')
$datasource = "D:UsersjanajaDesktoptest.csv"
$outputXLSX = "D:UsersjanajaDesktoptest.xlsx"
$excel = New-Object -ComObject excel.application
$xlChart=[Microsoft.Office.Interop.Excel.XLChartType]
$workbook = $excel.Workbooks.Add(1)
$worksheet = $workbook.worksheets.Item(1)
$TxtConnector = ("TEXT;" + $datasource)
$Connector = $worksheet.QueryTables.add($TxtConnector,$worksheet.Range("A1"))
$query = $worksheet.QueryTables.item($Connector.name)
$query.TextFileOtherDelimiter = $Excel.Application.International(5)
$query.AdjustColumnWidth = 1
$query.Refresh()
$query.Delete()
$Workbook.SaveAs($outputXLSX,51)
$i = 1
$j=1
while($j -le 20){
$excel.Cells.Item($i, $j).Font.ColorIndex = 3
$excel.Cells.Item($i, $j).Font.Bold = $True
$j++
}
$Chart = New-Object -TypeName
System.Windows.Forms.DataVisualization.Charting.Chart
$Chart.Size = '600,750'
$ChartArea = New-Object -TypeName
System.Windows.Forms.DataVisualization.Charting.ChartArea
$ChartArea.AxisX.Title = 'VM Name'
$ChartArea.AxisY.Title = 'CPU and Memory Utilization'
$ChartArea.AxisX.lnterval = '1'
$ChartArea.AxisX.LabelStyle.Enabled = $true
$ChartArea.AxisX.LabelStyle.Angle = 90
$Chart.ChartAreas.Add($ChartArea)
$Chart.Series.Add('Memory')
$Chart.Series.Add('CPU')
$Chart.Series['Memory'].ChartType =
[System.Windows.Forms.DataVisualization.Charting.SeriesChartType]::Bar
$Chart.Series['CPU'].ChartType =
[System.Windows.Forms.DataVisualization.Charting.SeriesChartType]::Bar
Foreach($VM in $datasource)
{
$var1 = $Chart.Series['Memory'].Points.AddXY($VM.VMName,$VM.MemoryGB)
}
Foreach($CPU in $datasource)
{
$var2 = $Chart.Series['CPU'].Points.AddXY($VM.VMName,$VM.CPU)
}
$Title = New-Object -TypeName
System.Windows.Forms.DataVisualization.Charting.Title
$Chart.Titles.Add($Title)
$Chart.Titles[O].Text = 'VM details'
$Chart.Anchor = [System.Windows.Forms.AnchorStyles]::Bottom -bor
[System.Windows.Forms.AnchorStyles]::Right -bor
[System.Windows.Forms.AnchorStyles]::Top -bor
[System.Windows.Forms.AnchorStyles]::Left
$Form = New-Object Windows.Forms.Form
$Form.Text= "VM Memory and CPU usage Chart"
$Form.Width = 600
$Form.Height = 600
$Form.controls.add($Chart)
$Form.Add_Shown({$Form.Activate()})
$Form.ShowDialog()
$Chart.Savelmage($Env:USERPROFILE + "DesktopChart.png", "PNG")
This code converts the file from CSV to Excel format and then reads the CSV file data to display a chart on the Powershell Form.
How do I Generate the same chart which takes only 3 column values out of 14 columns and display a clustered bar chart?
winforms powershell csv charts
I have read an input CSV file and generated a chart in PowerShell form.
This is my code:
$datasource = Import-Csv "D:UsersjanajaDesktoptest.csv"
[void] [Reflection.Assembly]::loadWithPartialName('System.Windows.Forms')
[void][Reflection.Assembly]::loadWithPartialName('System.Windows.Forms.DataVisualization')
$datasource = "D:UsersjanajaDesktoptest.csv"
$outputXLSX = "D:UsersjanajaDesktoptest.xlsx"
$excel = New-Object -ComObject excel.application
$xlChart=[Microsoft.Office.Interop.Excel.XLChartType]
$workbook = $excel.Workbooks.Add(1)
$worksheet = $workbook.worksheets.Item(1)
$TxtConnector = ("TEXT;" + $datasource)
$Connector = $worksheet.QueryTables.add($TxtConnector,$worksheet.Range("A1"))
$query = $worksheet.QueryTables.item($Connector.name)
$query.TextFileOtherDelimiter = $Excel.Application.International(5)
$query.AdjustColumnWidth = 1
$query.Refresh()
$query.Delete()
$Workbook.SaveAs($outputXLSX,51)
$i = 1
$j=1
while($j -le 20){
$excel.Cells.Item($i, $j).Font.ColorIndex = 3
$excel.Cells.Item($i, $j).Font.Bold = $True
$j++
}
$Chart = New-Object -TypeName
System.Windows.Forms.DataVisualization.Charting.Chart
$Chart.Size = '600,750'
$ChartArea = New-Object -TypeName
System.Windows.Forms.DataVisualization.Charting.ChartArea
$ChartArea.AxisX.Title = 'VM Name'
$ChartArea.AxisY.Title = 'CPU and Memory Utilization'
$ChartArea.AxisX.lnterval = '1'
$ChartArea.AxisX.LabelStyle.Enabled = $true
$ChartArea.AxisX.LabelStyle.Angle = 90
$Chart.ChartAreas.Add($ChartArea)
$Chart.Series.Add('Memory')
$Chart.Series.Add('CPU')
$Chart.Series['Memory'].ChartType =
[System.Windows.Forms.DataVisualization.Charting.SeriesChartType]::Bar
$Chart.Series['CPU'].ChartType =
[System.Windows.Forms.DataVisualization.Charting.SeriesChartType]::Bar
Foreach($VM in $datasource)
{
$var1 = $Chart.Series['Memory'].Points.AddXY($VM.VMName,$VM.MemoryGB)
}
Foreach($CPU in $datasource)
{
$var2 = $Chart.Series['CPU'].Points.AddXY($VM.VMName,$VM.CPU)
}
$Title = New-Object -TypeName
System.Windows.Forms.DataVisualization.Charting.Title
$Chart.Titles.Add($Title)
$Chart.Titles[O].Text = 'VM details'
$Chart.Anchor = [System.Windows.Forms.AnchorStyles]::Bottom -bor
[System.Windows.Forms.AnchorStyles]::Right -bor
[System.Windows.Forms.AnchorStyles]::Top -bor
[System.Windows.Forms.AnchorStyles]::Left
$Form = New-Object Windows.Forms.Form
$Form.Text= "VM Memory and CPU usage Chart"
$Form.Width = 600
$Form.Height = 600
$Form.controls.add($Chart)
$Form.Add_Shown({$Form.Activate()})
$Form.ShowDialog()
$Chart.Savelmage($Env:USERPROFILE + "DesktopChart.png", "PNG")
This code converts the file from CSV to Excel format and then reads the CSV file data to display a chart on the Powershell Form.
How do I Generate the same chart which takes only 3 column values out of 14 columns and display a clustered bar chart?
winforms powershell csv charts
winforms powershell csv charts
edited Nov 20 at 5:29
asked Nov 19 at 12:21
janani janagiraman
61
61
3
There is now way of displaying a chart in a csv file. Only text is possible.
– TobyU
Nov 19 at 12:23
3
please share your code here , instead of image
– Ahmed Sunny
Nov 19 at 12:30
This should not be receiving close votes, guys. "You can't do that" is absolutely a valid answer to a question. The only thing wrong here is posting code as an image.
– Bacon Bits
Nov 19 at 13:44
@BaconBits I CV'd because of the code screenshot.
– Ansgar Wiechers
Nov 19 at 14:37
add a comment |
3
There is now way of displaying a chart in a csv file. Only text is possible.
– TobyU
Nov 19 at 12:23
3
please share your code here , instead of image
– Ahmed Sunny
Nov 19 at 12:30
This should not be receiving close votes, guys. "You can't do that" is absolutely a valid answer to a question. The only thing wrong here is posting code as an image.
– Bacon Bits
Nov 19 at 13:44
@BaconBits I CV'd because of the code screenshot.
– Ansgar Wiechers
Nov 19 at 14:37
3
3
There is now way of displaying a chart in a csv file. Only text is possible.
– TobyU
Nov 19 at 12:23
There is now way of displaying a chart in a csv file. Only text is possible.
– TobyU
Nov 19 at 12:23
3
3
please share your code here , instead of image
– Ahmed Sunny
Nov 19 at 12:30
please share your code here , instead of image
– Ahmed Sunny
Nov 19 at 12:30
This should not be receiving close votes, guys. "You can't do that" is absolutely a valid answer to a question. The only thing wrong here is posting code as an image.
– Bacon Bits
Nov 19 at 13:44
This should not be receiving close votes, guys. "You can't do that" is absolutely a valid answer to a question. The only thing wrong here is posting code as an image.
– Bacon Bits
Nov 19 at 13:44
@BaconBits I CV'd because of the code screenshot.
– Ansgar Wiechers
Nov 19 at 14:37
@BaconBits I CV'd because of the code screenshot.
– Ansgar Wiechers
Nov 19 at 14:37
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
You cannot embed pictures in CSV files the way you probably intend, because CSV is a plaintext format. You could embed a path to a picture file, or you could encode the picture (e.g. using base64 encoding) and include the encoded data. But even with the latter you'd still need a viewer that would decode the encoded data and render them as a picture.
Essentially, what you're asking is not possible.
However, you could use the ImportExcel to create an aribtrary Excel spreadsheet from an existing CSV file. The module supports creating Excel charts as well, which should be able to reproduce a similar chart to the one you're creating with your image. You may also be able to just embed the image file, but I'm not sure if that's possible. You may need to use the much older Excel COM API that ships with Office to do that.
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
You cannot embed pictures in CSV files the way you probably intend, because CSV is a plaintext format. You could embed a path to a picture file, or you could encode the picture (e.g. using base64 encoding) and include the encoded data. But even with the latter you'd still need a viewer that would decode the encoded data and render them as a picture.
Essentially, what you're asking is not possible.
However, you could use the ImportExcel to create an aribtrary Excel spreadsheet from an existing CSV file. The module supports creating Excel charts as well, which should be able to reproduce a similar chart to the one you're creating with your image. You may also be able to just embed the image file, but I'm not sure if that's possible. You may need to use the much older Excel COM API that ships with Office to do that.
add a comment |
up vote
1
down vote
You cannot embed pictures in CSV files the way you probably intend, because CSV is a plaintext format. You could embed a path to a picture file, or you could encode the picture (e.g. using base64 encoding) and include the encoded data. But even with the latter you'd still need a viewer that would decode the encoded data and render them as a picture.
Essentially, what you're asking is not possible.
However, you could use the ImportExcel to create an aribtrary Excel spreadsheet from an existing CSV file. The module supports creating Excel charts as well, which should be able to reproduce a similar chart to the one you're creating with your image. You may also be able to just embed the image file, but I'm not sure if that's possible. You may need to use the much older Excel COM API that ships with Office to do that.
add a comment |
up vote
1
down vote
up vote
1
down vote
You cannot embed pictures in CSV files the way you probably intend, because CSV is a plaintext format. You could embed a path to a picture file, or you could encode the picture (e.g. using base64 encoding) and include the encoded data. But even with the latter you'd still need a viewer that would decode the encoded data and render them as a picture.
Essentially, what you're asking is not possible.
However, you could use the ImportExcel to create an aribtrary Excel spreadsheet from an existing CSV file. The module supports creating Excel charts as well, which should be able to reproduce a similar chart to the one you're creating with your image. You may also be able to just embed the image file, but I'm not sure if that's possible. You may need to use the much older Excel COM API that ships with Office to do that.
You cannot embed pictures in CSV files the way you probably intend, because CSV is a plaintext format. You could embed a path to a picture file, or you could encode the picture (e.g. using base64 encoding) and include the encoded data. But even with the latter you'd still need a viewer that would decode the encoded data and render them as a picture.
Essentially, what you're asking is not possible.
However, you could use the ImportExcel to create an aribtrary Excel spreadsheet from an existing CSV file. The module supports creating Excel charts as well, which should be able to reproduce a similar chart to the one you're creating with your image. You may also be able to just embed the image file, but I'm not sure if that's possible. You may need to use the much older Excel COM API that ships with Office to do that.
edited Nov 19 at 13:51
community wiki
2 revs, 2 users 75%
Ansgar Wiechers
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53374531%2fdisplaying-charts-in-excel-using-powershell%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
3
There is now way of displaying a chart in a csv file. Only text is possible.
– TobyU
Nov 19 at 12:23
3
please share your code here , instead of image
– Ahmed Sunny
Nov 19 at 12:30
This should not be receiving close votes, guys. "You can't do that" is absolutely a valid answer to a question. The only thing wrong here is posting code as an image.
– Bacon Bits
Nov 19 at 13:44
@BaconBits I CV'd because of the code screenshot.
– Ansgar Wiechers
Nov 19 at 14:37