如何使用VB.Net在excel中设置系列的XValues

时间:2015-07-09 22:59:09

标签: vb.net excel vba excel-vba

我正在尝试使用两列数据创建XY-Scatter图表。第一列是从单元格A2到A30的X,第二列是从Excel单元格中的单元格B2到B30的Y.

我能够创建图表,但它正在绘制两个系列的数据,它将col X作为一个系列,col Y作为另一个系列。因为我不知道vb.net中的语法是如何工作的,而且我无法在vb.net中找到有关如何执行此操作的文档,我从vba文档中得到了一些想法(可以在vba中定义如下:< / p>

    xlApp.ActiveChart.SeriesCollection(1).XValues = xlWorkSheet.Range("$A$2", "$A$30")
    xlApp.ActiveChart.SeriesCollection(1).Values = xlWorkSheet.Range("$B$2", "$B$30")

并生成以下行。

所以我尝试用行

设置系列的XValues
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

但它向我抛出错误:COMException在上面的行上没有处理。我不确定我做错了什么,所以请帮忙。

这是生成图表的代码块。基本上,它在Excel文件中读取,然后使用文件中的数据创建图表。

{{1}}

由于

2 个答案:

答案 0 :(得分:1)

请尝试这样

xlApp.ActiveChart.SeriesCollection(1).XValues = "={""tes1"",""Test2""}"

MyVal="{""" & xlWorkSheet.range("A2") & """"
MyVal=MyVal & ",""" & xlWorkSheet.range("A30") & """}"
xlApp.ActiveChart.SeriesCollection(1).XValues = MyVal

答案 1 :(得分:1)

XValues实际上可以是一个数组,例如:

Dim MyXVal() as string={"123","345","567"}
xlApp.ActiveChart.SeriesCollection(1).XValues = MyXVal