gpt4 book ai didi

vb.net - 在 Excel.Chart 中导出 VB.NET Charting.Chart

转载 作者:行者123 更新时间:2023-12-04 04:39:58 26 4
gpt4 key购买 nike

假设我有一个 Charting.Chart :

Chart with some properties

我想导出到 Excel.Workbook.Worksheet以便我稍后可以“播放”数据(例如在 Excel 图表上拖放更多数据等):

Don't mind the differences of the curves

请不要介意第二张图表的差异,尽可能接近第一张图表的东西将是最佳解决方案

有没有什么简单的方法可以导出第一个保留 的图表?它的所有属性或至少 Excel 接受的属性 ,还是我必须通过每一个属性(property)?例如。:

myCht.Title = myUserFormChart.Titles(0).Text
mySeries = myCht.Chart.SeriesCollection.NewSeries()
mySeries.Name = myUserFormChart.Series(0).Name
[...]

最佳答案

基本上你需要某种导出功能。您想将应用程序的图表导出到 Excel 工作表。你需要一个简单的方法来做到这一点。根据 System.Windows.Forms.DataVisualization.Charting.Chart class documentationexcel chart object's documentation没有一种简单的方法可以进行转换,它们有一些相似之处,但您必须遍历每个属性。

但是,如果我是你,我会使用像 EPPlus 这样的外部 3rd 方开源库。它支持许多 excel 功能(包括图表),以便在我的应用程序中创建可靠的“导出到 excel”功能。

它并不像看起来那么难,而且在各种图表类型和/或数据中进行调整可能相对容易。示例代码如下(不要忘记安装 EPPlus nuget 包):

Private Sub ExportToExcel_Click(sender As Object, e As EventArgs) Handles Button5.Click
Using theExcel As OfficeOpenXml.ExcelPackage = New OfficeOpenXml.ExcelPackage
Dim theDataWorkSheet As OfficeOpenXml.ExcelWorksheet = theExcel.Workbook.Worksheets.Add("Data")

With theDataWorkSheet
'Create the data cells here...
'You can get the data from your in program arrays or directly from your chart class...
.Cells("A1").Value = 10
.Cells("A2").Value = 20
.Cells("A3").Value = 30

.Cells("B1").Value = 1
.Cells("B2").Value = 2
.Cells("B3").Value = 3
End With

Dim theChartWorkSheet As OfficeOpenXml.ExcelWorksheet = theExcel.Workbook.Worksheets.Add("Chart")
Dim theChart As OfficeOpenXml.Drawing.Chart.ExcelRadarChart = theChartWorkSheet.Drawings.AddChart("Chart", OfficeOpenXml.Drawing.Chart.eChartType.Radar)

With theChart
'Manipulate the excel chart here...

.SetPosition(1, 0, 2, 0)
.SetSize(400, 400)
.Series.Add(theDataWorkSheet.Cells(1, 1, 3, 1), theDataWorkSheet.Cells(1, 2, 3, 2))
.Title.Text = "Chart title"

'all the other required excel chart properties...
End With

'Finally save the excel file...
theExcel.SaveAs(New IO.FileInfo("c:\test.xlsx"))
End Using
End Sub

有关详细信息,请参阅 EPPlus 文档。
  • Getting started
  • Shapes, Pictures and Charts

  • 希望这可以帮助。

    关于vb.net - 在 Excel.Chart 中导出 VB.NET Charting.Chart,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48518743/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com