vb.net - 使用 vb.net 创建 3d 饼图
问题描述
我试图创建一个 3d 饼图,但我得到一个错误这里我使用的代码
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
错误 无法将“Microsoft.Office.Interop.Excel.ChartClass”类型的 COM 对象转换为接口类型“Microsoft.Office.Interop.Excel._Chart” 在第 1 行引发
.Shapes.AddChart.Select()
提前致谢
解决方案
我成功了
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
推荐阅读
- javascript - 在位置上使用过渡
- ruby-on-rails - 编辑时Rails价格修改
- c# - 在 ConfigureServices 本身中获取对象实例的正确方法是什么?
- firebase - 无法访问/扩展 Firebase 实时数据
- javascript - display = "show" 是一回事吗?
- c++ - 为 openGL windows 8 Microsoft Visual Studio 2019 64 位安装 GLUT
- spring-el - 我可以遍历 Spinnaker 表达式中的列表吗?
- jquery - 使用 Ajax 过滤器购物并加载更多功能 Laravel
- node.js - 类型文件打字稿错误上不存在属性名称
- visual-studio-code - 如何在 VS-Code 中通过空格水平移动光标?