Displaying charts in Excel using PowerShell











up vote
-2
down vote

favorite
1












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?










share|improve this question




















  • 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















up vote
-2
down vote

favorite
1












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?










share|improve this question




















  • 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













up vote
-2
down vote

favorite
1









up vote
-2
down vote

favorite
1






1





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?










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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














  • 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












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.






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    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

























    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.






    share|improve this answer



























      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.






      share|improve this answer

























        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.






        share|improve this answer














        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.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 19 at 13:51


























        community wiki





        2 revs, 2 users 75%
        Ansgar Wiechers































            draft saved

            draft discarded




















































            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.




            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            "Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

            Alcedinidae

            RAC Tourist Trophy