且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

VB.NET的Excel图表(散点线)自动化

更新时间:2022-12-01 22:39:34

应该有一个更直接的解决方案,直接指定正确的格式等,但这可以解决您所描述的问题因为我们正在使用VB.Net,所以我删除了不需要的Missing引用. (我想您是从C#示例转换而来的.)
There should be a more direct solution that just specifies the right format, etc. straight away, but this fixes the problem as you''ve described it, and because we''re using VB.Net, I''ve removed the unneeded Missing references. (I guess you converted this from a C# sample.)
Private Sub DisplayGraph()
    Try
        Dim xlApp As Excel.Application
        Dim xlWorkBook As Excel.Workbook
        Dim xlWorkSheet As Excel.Worksheet

        xlApp = New Excel.ApplicationClass
        xlWorkBook = xlApp.Workbooks.Add()
        xlWorkSheet = xlWorkBook.Sheets("sheet1")
        xlApp.Visible = True

        ''Adding Data..
        xlWorkSheet.Cells(1, 1) = 2
        xlWorkSheet.Cells(1, 2) = 1
        xlWorkSheet.Cells(2, 1) = 3
        xlWorkSheet.Cells(2, 2) = 2
        xlWorkSheet.Cells(3, 1) = 2
        xlWorkSheet.Cells(3, 2) = 5
        xlWorkSheet.Cells(4, 1) = 4
        xlWorkSheet.Cells(4, 2) = 7
        xlWorkSheet.Cells(5, 1) = 8
        xlWorkSheet.Cells(5, 2) = 2
        xlWorkSheet.Cells(6, 1) = 5
        xlWorkSheet.Cells(6, 2) = 3

        Dim oChart As Excel._Chart
        oChart = xlWorkBook.Charts.Add()

        oChart.ChartWizard(xlWorkSheet.Range("A1", "B6"), _
          Excel.XlChartType.xlXYScatterLines, _
          PlotBy:=Excel.XlRowCol.xlRows)
        oChart.PlotBy = Excel.XlRowCol.xlColumns
    Catch ex As Exception
        MessageBox.Show(ex.Message)
    End Try

End Sub