【问题标题】:How to set the XValues of a series in excel using VB.Net如何使用 VB.Net 在 excel 中设置系列的值
【发布时间】:2015-07-09 22:59:09
【问题描述】:

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

我可以创建图表,但它使用两个系列的数据进行绘图,它将 col X 作为一个系列,将 col Y 作为另一个系列。因为我不知道 vb.net 中的语法是如何工作的,也找不到关于如何在 vb.net 中执行此操作的文档,所以我从 vba 文档中得到了一些想法(它可以在 vba 中这样定义:

     Charts("Chart1").SeriesCollection(1).XValues =_Worksheets("Sheet1").Range("B1:B5")

并生成以下行。

所以我尝试用线设置系列的 XValues

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

但它向我抛出了错误: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

谢谢

【问题讨论】:

  • 您将这些属性设置为公式。 .XValues = "='Sheet1'!$A$2:$A$30".Values = "='Sheet1'!$B$2:$B$30".
  • @TnTinMn,您可以为ValuesXValues 提供Range 对象。以这种方式创建图表时,它实际上是否有Series?我通常通过ChartObjects.AddSeriesCollection.NewSeries 来获取系列。如果是这样的话,我猜.Name 会首先失败。无论如何,这里正在使用 C# 代码来执行此操作。 stackoverflow.com/questions/30590070/…。可能想要验证您的Range 是否被正确创建。在迷途线上尝试Range.Select 以确保它是正确的。
  • 谢谢。效果很好。

标签: vb.net vba excel


【解决方案1】:

请这样尝试

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

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

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      【解决方案3】:

      将单元格括在引号中,使其成为 Jamil 建议的数组就可以了。如下例程可以帮助您:

      Private Function WrapUp(ByVal Cells As Range) As String
          Dim TmpWrap As String = ""
          For Each Cell As Range In Cells
              TmpWrap &= """" & Cell.Value & ""","
          Next
      
          Return "{" & Left(TmpWrap, Len(TmpWrap) - 1) & "}"
      End Function
      

      现在你可以使用这样的东西了:

      xlApp.ActiveChart.SeriesCollection(1).XValues = WrapUp(_Worksheets("Sheet1").Range("B1:B5"))
      xlApp.ActiveChart.SeriesCollection(1).XValues =WrapUp(Globals.ThisAddIn.Application.Selection)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-04-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-11-17
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多