简体   繁体   English

Excel中的散点图通过vb.net从不相邻的列

[英]scatter chart in excel from not adjacent columns through vb.net

I'm trying to draw charts in an excel spreadsheet through a vb.net code. 我正在尝试通过vb.net代码在Excel电子表格中绘制图表。 I want to draw a scatter chart from column A (X-axis) and column D (Y-axis). 我想从A列(X轴)和D列(Y轴)绘制散点图。 With some online help, I have written this code so far: 到目前为止,在一些在线帮助下,我已经编写了以下代码:

Dim xlApp As Excel.Application
Dim xlWorkBook As Excel.Workbook
Dim xlWorkSheet As Excel.Worksheet
xlApp = New Excel.Application

xlWorkBook = xlApp.Workbooks.Open("C:\excel_file.xlsx")

    xlApp.Visible = True

    xlWorkSheet = xlWorkBook.Sheets("Foglio4")

    With xlWorkSheet

        .Shapes.AddChart.Select()

        With xlApp.ActiveChart

            .ApplyCustomType(Excel.XlChartType.xlXYScatterSmoothNoMarkers)

            xlApp.ActiveChart.SeriesCollection(1).Name = "X-Y"
            xlApp.ActiveChart.SeriesCollection(1).XValues = "='Foglio4'!$A$2:$A$1446"
            xlApp.ActiveChart.SeriesCollection(1).Values = "='Foglio4'!$D$2:$D$1446"

        End with

    End with

But I get a chart with the series I want together with other four series, made with the A-column on the X-axis and all the others on the Y-axis, as I have five columns. 但是我得到了我想要的系列以及其他四个系列的图表,这些系列在X轴上使用A列,在Y轴上使用所有其他列,因为我有五列。

Why does it draw all those series? 为什么要绘制所有这些系列? Any idea? 任何想法?

Thanks to the precious comment made by Tim Williams, I've added this part to my code: 感谢Tim Williams的宝贵评论,我将这一部分添加到了我的代码中:

[...]

xlApp.ActiveChart.SeriesCollection(1).Name = "X-Y"
xlApp.ActiveChart.SeriesCollection(1).XValues = "='Foglio4'!$A$2:$A$1446"
xlApp.ActiveChart.SeriesCollection(1).Values = "='Foglio4'!$D$2:$D$1446"

For n = .SeriesCollection.Count To 2 Step -1
    .SeriesCollection(n).Delete
Next n

[...]

This deletes all the unwanted series leaving just the one wanted, that is the #1. 这将删除所有不需要的序列,仅留下一个想要的序列,即#1。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM