0

List(Of Decimal)是否可以使用 VB 从 Excel 中的值创建绘图?我想避免将数据直接转储到表格中,然后从中创建图表。到目前为止,这是我所拥有的,但是当我尝试分配.XValues和分配.Values给列表时,我遇到了数据类型不匹配的问题。是否需要进行某种转换才能将列表转换为系列?

    Dim Series As Excel.Series
    Dim xVals As New List(Of Decimal)(New Decimal() {1, 3, 4, 5})
    Dim yVals As New List(Of Decimal)(New Decimal() {2, 2, 2, 2})
    xlWorkSheet = CType(Globals.ThisAddIn.Application.Worksheets.Add(), Excel.Worksheet)

    xlWorkSheet.Activate()
    xlWorkSheet.Name = "My Sheet"
    xlCharts = xlWorkSheet.ChartObjects
    xlChartObj = xlCharts.Add(150, 30, 400, 250)
    xlChart = xlChartObj.Chart

    With xlChart
        .ChartType = Excel.XlChartType.xlXYScatterLines
        .HasLegend = True
        .Legend.Position = Excel.XlLegendPosition.xlLegendPositionRight
        .HasTitle = True
        .ChartTitle.Text = "Test Chart"
        For i = 0 To channels.Count - 1
            .SeriesCollection.NewSeries()
            Series = .SeriesCollection(i + 1)
            Series.XValues = xVals
            Series.Values = yvals
        Next
        .Location(Excel.XlChartLocation.xlLocationAsNewSheet)
    End With

更新:

我似乎下面的代码可以工作,但它不是很理想。除了像我一样将数组转换为数组的字符串表示形式之外,还有更好的方法吗?

    Dim Series As Excel.Series
    Dim xVals As New List(Of Decimal)(New Decimal() {1, 3, 4, 5})
    Dim yVals As New List(Of Decimal)(New Decimal() {2, 2, 2, 2})
    Dim xString As String
    Dim yString As String
    xlWorkSheet = CType(Globals.ThisAddIn.Application.Worksheets.Add(), Excel.Worksheet)

    xlWorkSheet.Activate()
    xlWorkSheet.Name = "My Sheet"
    xlCharts = xlWorkSheet.ChartObjects
    xlChartObj = xlCharts.Add(150, 30, 400, 250)
    xlChart = xlChartObj.Chart

    With xlChart
        .ChartType = Excel.XlChartType.xlXYScatterLines
        .HasLegend = True
        .Legend.Position = Excel.XlLegendPosition.xlLegendPositionRight
        .HasTitle = True
        .ChartTitle.Text = "Test Chart"
        For i = 0 To channels.Count - 1
            .SeriesCollection.NewSeries()
            Series = .SeriesCollection(i + 1)

            xString = "={"
            xString += xVals(0).ToString
            For k = 1 To xVals.Count - 1
                xString += ","
                xString += xVals(k).ToString
            Next
            xString = "}"

            yString = "={"
            yString += yVals(0).ToString
            For k = 1 To yVals.Count - 1
                yString += ","
                yString += yVals(k).ToString
            Next
            yString = "}"

            Series.XValues = xString
            Series.Values = yString
        Next
        .Location(Excel.XlChartLocation.xlLocationAsNewSheet)
    End With
4

2 回答 2

0

使用存储为对象的范围,我能够通过以下代码获得我想要的东西。我在第二个代码块中提到的字符串方法有 8000+ 个字符的限制(不确定实际数字,8192?)。

Dim Series As Excel.Series
Dim xVals As New List(Of Decimal)(New Decimal() {1, 3, 4, 5})
Dim yVals As New List(Of Decimal)(New Decimal() {2, 2, 2, 2})
Dim xRange As Object
Dim yRange As Object
Dim tempSheet as Excel.Worksheet

xlWorkSheet = CType(Globals.ThisAddIn.Application.Worksheets.Add(), Excel.Worksheet)
xlWorkSheet.Activate()
xlWorkSheet.Name = "My Sheet"
xlCharts = xlWorkSheet.ChartObjects
xlChartObj = xlCharts.Add(150, 30, 400, 250)
xlChart = xlChartObj.Chart

With xlChart
    .ChartType = Excel.XlChartType.xlXYScatterLines
    .HasLegend = True
    .Legend.Position = Excel.XlLegendPosition.xlLegendPositionRight
    .HasTitle = True
    .ChartTitle.Text = "Test Chart"
    For i = 0 To channels.Count - 1
        tempSheet = CType(Globals.ThisAddIn.Application.Worksheets.Add(), Excel.Worksheet)
        tempSheet.Activate()
        With tempSheet
            .Visible = False
            For k = 0 To xVals.Count - 1
                tempSheet.Cells(k + 1, 1).Value = xVals(k)
                tempSheet.Cells(k + 1, 2).Value = yVals(k) 'Assumes yVals.Count = xVals.Count
            Next
            xRange = tempSheet.Range("A1:A" + xVals.Count.ToString).Value
            yRange = tempSheet.Range("B1:B" + xVals.Count.ToString).Value
            Globals.ThisAddIn.Application.DisplayAlerts = False
            .Delete()
            Globals.ThisAddIn.Application.DisplayAlerts = True
        End With

        Series = .SeriesCollection.NewSeries()
        With Series
            .XValues = xRange
            .Values = yRange
        End With            
    Next
    .Location(Excel.XlChartLocation.xlLocationAsNewSheet)
End With
于 2012-05-23T20:42:53.560 回答
0

FWIW,Excel 在从工作表中绘制数据时表现最好。是否有任何理由不将列表(或数组)转储到工作表中,并将这些范围用作图表系列源数据?

于 2012-05-19T16:12:15.623 回答