提问人:Noldor130884 提问时间:1/30/2018 更新时间:11/11/2018 访问量:1845
在 Excel.Chart 中导出 VB.NET Charting.Chart
Export VB.NET Charting.Chart in Excel.Chart
问:
假设我有一个:Charting.Chart
我想导出到一个,以便我以后可以“玩”数据(例如,在Excel图表上拖放更多数据等):Excel.Workbook.Worksheet
请不要介意第二张图表中的差异,尽可能接近第一张图表将是最佳解决方案
有没有简单的方法可以导出第一个图表,保留其所有属性或至少是 Excel 接受的属性,或者我是否必须遍历每个属性?例如:
myCht.Title = myUserFormChart.Titles(0).Text
mySeries = myCht.Chart.SeriesCollection.NewSeries()
mySeries.Name = myUserFormChart.Series(0).Name
[...]
答:
0赞
ChD Computers
11/11/2018
#1
基本上,您需要某种导出功能。您希望将应用程序的图表导出到 Excel 工作表。你需要一种简单的方法来做到这一点。根据 System.Windows.Forms.DataVisualization.Charting.Chart 类文档和 excel 图表对象的文档,没有一种简单的方法可以进行转换,它们有一些相似之处,但您必须遍历每个属性。
但是,如果我是你,我会使用外部第三方开源库,如 EPPlus,它支持许多 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 文档。
希望这会有所帮助。
评论