Création d'un excel chart [Résolu]

Messages postés
3258
Date d'inscription
jeudi 26 novembre 2009
Statut
Membre
Dernière intervention
3 décembre 2019
-
Bonjour,
J'essaie de créer un excel pie3d chart via le vb.net

Public Sub ShowChart(ByVal mylist As List(Of Classdayhag))
        Dim celltable As excel.Range
        Dim indexcol As Integer
        Dim indexrow As Integer
        Try

            With xlworksheet
                celltable = .Range("A1", "B" & (mylist.Count - 1))
                indexcol = 1
                indexrow = 1
                For Each item As Classdayhag In mylist
                    .Cells(indexrow, indexcol) = item.dayname
                    .Cells(indexrow, indexcol + 1) = item.count
                    indexrow += 1
                Next
                celltable.Select()
                Dim mychart As New excel.Chart
                With mychart
                    .Shapes.AddChart.Select()
                    .ChartType = excel.XlChartType.xl3DPie
                    .SetSourceData(Source:=celltable)
                    .ApplyLayout(6)
                End With
            End With

            objexcel.Visible = True
            RaiseEvent isshown()
            objexcel = Nothing
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            objexcel = Nothing
        End Try
    End Sub


j'obtiens l'erreur à la ligne .Shapes.AddChart.Select()
Unable to cast COM object of type 'Microsoft.Office.Interop.Excel.ChartClass' to interface type 'Microsoft.Office.Interop.Excel._Chart

merci d'avance
Afficher la suite 

1 réponse

Messages postés
3258
Date d'inscription
jeudi 26 novembre 2009
Statut
Membre
Dernière intervention
3 décembre 2019
46
0
Merci
j'ai réussi

 Public Sub ShowChart(ByVal mylist As List(Of Classdayhag), ByVal title As String)

        'create chart

        Dim xlCharts As excel.ChartObjects
        Dim myChart As excel.ChartObject
        Dim celltable As excel.Range
        Dim indexcol As Integer
        Dim indexrow As Integer

        Try
            xlCharts = CType(xlworksheet.ChartObjects, excel.ChartObjects)
            myChart = xlCharts.Add(10, 80, 400, 400)
            With xlworksheet
                celltable = .Range("A1", "B" & (mylist.Count))


                indexcol = 1
                indexrow = 1
                For Each item As Classdayhag In mylist
                    .Cells(indexrow, indexcol) = item.dayname
                    .Cells(indexrow, indexcol + 1) = item.count
                    indexrow += 1
                Next
                celltable.Select()


                With myChart
                    .Chart.ChartType = excel.XlChartType.xl3DPie
                    .Chart.HasTitle = True
                    .Chart.ChartTitle.Text = title
                    .Chart.ChartTitle.Font.Name = "Arial"
                    .Chart.ChartTitle.Font.Size = 12
                    .Chart.ChartTitle.Font.Bold = True
                    .Chart.SetSourceData(Source:=celltable)
                    .Chart.ApplyLayout(6)
                
                End With
            End With

            objexcel.Visible = True
            RaiseEvent isshown()
            objexcel = Nothing
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            objexcel = Nothing
        End Try

    End Sub
Commenter la réponse de cs_ShayW