我想创建一个包含两列数据的XY散点图。第一列是从单元格A2到A30的X,第二列是从Excel单元格中的B2到B30的Y.如何使用VB.Net在Excel中设置一系列的XValues
我可以创建图表,但它是绘制两个系列的数据,它将col X作为一个系列,并将col Y作为另一个系列。因为我不知道vb.net中的语法是如何工作的,并且我无法找到关于如何在vb.net中执行此操作的文档,所以我从vba文档中获得了一些想法(可以在vba中这样定义它:
。 Charts("Chart1").SeriesCollection(1).XValues =_Worksheets("Sheet1").Range("B1:B5")
,并产生以下行
于是,我就用线
xlApp.ActiveChart.SeriesCollection(1).XValues = xlWorkSheet.Range("$A$2", "$A$30")
xlApp.ActiveChart.SeriesCollection(1).Values = xlWorkSheet.Range("$B$2", "$B$30")
设置一系列的XValues但它是在我抛出的错误:收到COMException是以上行未处理。我不确定我做错了什么,所以请帮忙。
以下是生成图表的代码块。基本上,它是在Excel文件中读取数据,然后使用文件中的数据创建图表。
Private Sub Create_Chart_Click(sender As Object, e As EventArgs) Handles Create_Chart.Click
Dim xlApp As Excel.Application
Dim xlWorkBook As Excel.Workbook
Dim xlWorkSheet As Excel.Worksheet
xlApp = New Excel.ApplicationClass
'~~> Add a New Workbook
xlWorkBook = xlApp.Workbooks.Open("C:\Test_data.xlsx")
'Display Excel
xlApp.Visible = True
'~~> Set the relebant sheet that we want to work with
xlWorkSheet = xlWorkBook.Sheets("Sheet1")
With xlWorkSheet
'~~> Inserting a Graph
.Shapes.AddChart.Select()
'~~> Formatting the chart
With xlApp.ActiveChart
'~~> Make it a Line Chart
.ApplyCustomType(Excel.XlChartType.xlXYScatterSmoothNoMarkers)
'~~> Set the data range
xlApp.ActiveChart.SeriesCollection(1).Name = "X-Y"
xlApp.ActiveChart.SeriesCollection(1).XValues = xlWorkSheet.Range("$A$2", "$A$30")
xlApp.ActiveChart.SeriesCollection(1).Values = xlWorkSheet.Range("$B$2", "$B$30")
'~~> Fill the background of the chart
xlApp.ActiveChart.ChartArea.Format.Fill.ForeColor.ObjectThemeColor = _
Microsoft.Office.Core.MsoThemeColorIndex.msoThemeColorBackground1 '<~~ Grey
xlApp.ActiveChart.ChartArea.Format.Fill.ForeColor.TintAndShade = 0
xlApp.ActiveChart.ChartArea.Format.Fill.ForeColor.Brightness = -0.150000006
xlApp.ActiveChart.ChartArea.Format.Fill.Transparency = 0
xlApp.ActiveChart.ChartArea.Format.Fill.Solid()
xlApp.ActiveChart.SeriesCollection(1).Trendlines.Add()
xlApp.ActiveChart.SeriesCollection(1).Trendlines(1).Type = Microsoft.Office.Interop.Excel.XlTrendlineType.xlPolynomial
xlApp.ActiveChart.SeriesCollection(1).Trendlines(1).Order = 2
'xlApp.ActiveChart.SeriesCollection(1).Format.Line.Visible = Microsoft.Office.Core.MsoTriState.msoFalse
'~~> Make the corners of the Chart Rount
'.Parent.RoundedCorners = True
'~~> Removing lines and the back color so plot area shows char's background color
With .PlotArea
.Format.Fill.Visible = Microsoft.Office.Core.MsoTriState.msoFalse
.Format.Line.Visible = Microsoft.Office.Core.MsoTriState.msoFalse
End With
'~~> Removing the major gridlines
'.Axes(Excel.XlAxisType.xlValue).MajorGridlines.Format.Line.Visible = Microsoft.Office.Core.MsoTriState.msoFalse
'~~> Making the series line smooth
'.SeriesCollection(1).Smooth = True
'~~> Formatting the legend
With .Legend
With .Format.TextFrame2.TextRange.Font.Fill
.Visible = Microsoft.Office.Core.MsoTriState.msoTrue
.ForeColor.RGB = RGB(0, 0, 0)
.Transparency = 0
.Solid()
End With
With .Format.Fill
.Visible = Microsoft.Office.Core.MsoTriState.msoTrue
.ForeColor.ObjectThemeColor = Microsoft.Office.Core.MsoThemeColorIndex.msoThemeColorBackground1
.ForeColor.TintAndShade = 0
.ForeColor.Brightness = -0.25
.Transparency = 0
.Solid()
End With
End With
'~~> Change the format of Y axis to show $ signs
'.Axes(Excel.XlAxisType.xlValue).TickLabels.NumberFormat = "#,##0.00"
'~~> Underline the Chart Title
' .ChartTitle.Format.TextFrame2.TextRange.Font.UnderlineStyle = _
' Microsoft.Office.Core.MsoLineStyle.msoLineSingle
End With
End With
End Sub
感谢
您将这些属性设置为公式。 '.Values =“='Sheet1'!$ A $ 2:$ A $ 30”'和'.Values =“='Sheet1'!$ B $ 2:$ B $ 30”'。 – TnTinMn
@TnTinMn,你可以为'Values'和'XValues'提供'Range'对象。创建这种方式时,图表是否实际上具有“系列”?我通常会通过“ChartObjects.Add”然后通过“SeriesCollection.NewSeries”来获取系列。如果是这种情况,我想'.Name'会先失败。无论如何,这里是工作的C#代码来做到这一点。 http://stackoverflow.com/questions/30590070/generate-scatter-graph-in-vb-net/30598519#30598519。可能想验证你的Range是否正确创建。在杂散的线上尝试'Range.Select',以确保它是正确的。 –
谢谢。它完美的作品。 – bill