我想創建一個包含兩列數據的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