Nol*_*884 9 vb.net charts interop export-to-excel
假设我有一个Charting.Chart:
我想导出到一个Excel.Workbook.Worksheet以便以后可以"播放"数据(例如在Excel图表上拖放更多数据等):
请不要介意第二张图表中的差异,尽可能接近第一张图表是最佳解决方案
是否有任何简单的方法可以导出保留其所有属性的第一个图表,或者至少是Excel接受的图表,或者我是否必须浏览每个属性?例如:
myCht.Title = myUserFormChart.Titles(0).Text
mySeries = myCht.Chart.SeriesCollection.NewSeries()
mySeries.Name = myUserFormChart.Series(0).Name
[...]
Run Code Online (Sandbox Code Playgroud)
基本上你需要某种导出功能。您想要将应用程序的图表导出到 Excel 工作表。您需要一种简单的方法来做到这一点。根据System.Windows.Forms.DataVisualization.Charting.Chart 类文档和Excel 图表对象的文档,没有一种简单的方法来进行转换,它们有一些相似之处,但您必须仔细检查每个属性。
但是,如果我是您,我会使用外部第 3 方开源库(例如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
Run Code Online (Sandbox Code Playgroud)
有关详细信息,请参阅 EPPlus 文档。
希望这可以帮助。
| 归档时间: |
|
| 查看次数: |
661 次 |
| 最近记录: |