使用excel数据的vb.net图表

时间:2016-03-08 20:10:11

标签: vb.net excel

我想在做一个更高级的项目之前做一个简单的例子,我无法弄清楚如何在vb.net中使用图表中单元格的excel数据。当我在网上查找所有我能找到的数据库到vb.net或从vb.net到excel图表。离开这个已经有一段时间了,但没有一个工作http://www.siddharthrout.com/2011/10/18/charting-with-vb-net-2010/

然后我尝试了下面的代码没有运气,获取运行时错误将范围转换为字符串

Imports Excel = Microsoft.Office.Interop.Excel
Public Class Form1
Dim appXL As Excel.Application
Dim wbsXL As Excel.Workbooks
Dim wbXL As Excel.Workbook
Dim Rangex As Excel.Range
Dim Rangey As Excel.Range
Public Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    appXL = CreateObject("Excel.Application")
    appXL.Visible = True
    wbsXL = appXL.Workbooks
    wbXL = wbsXL.Open("C:\Users\aholiday\Desktop\Data.xlsx")

    Rangey = wbXL.Range("A1:A2")
    Rangex = wbXL.Range("B1:B2")

    With chtTest
        .Series("Series1").YValueMembers = Rangey
        .Series("Series1").XValueMember = Rangex

    End With
End Sub
End Class

任何人都可以向我展示一个基本的例子,使用2个单元格作为x,2个单元格作为y轴数据。

1 个答案:

答案 0 :(得分:1)

我可能不明白你在问什么。我无法让你的榜样发挥作用。因此,我只使用了此页面中的一半代码和来自另一页面的一半代码,我得到了一些工作。然而,因为我无法让你的代码运行,我不确定这是否是你要求的。     “http://ask.brothersoft.com/how-to-use-an-excel-insertable-object-in-visual-basic-160217.html     “http://vb.net-informations.com/excel-2007/vb.net_excel_create_chart.htm

Private Sub Button1_Click(sender As Object,e As EventArgs)Handles Button1.Click

    Dim XLSApp As New Excel.Application
    XLSApp.Visible = True
    Dim XLSWbks As Excel.Workbooks = XLSApp.Workbooks
    Dim XLSWbk As Excel.Workbook = XLSWbks.Add
    Dim XLSShts As Excel.Sheets = XLSWbk.Worksheets
    Dim XLSSht As Excel.Worksheet = XLSShts("Sheet1")
    XLSSht.Range("A1").Value = "1"
    XLSSht.Range("A2").Value = "2"
    XLSSht.Range("B1").Value = "1"
    XLSSht.Range("B2").Value = "2"


    'create chart
    Dim chartPage As Excel.Chart
    Dim xlCharts As Excel.ChartObjects
    Dim myChart As Excel.ChartObject
    Dim chartRange As Excel.Range

    xlCharts = XLSSht.ChartObjects
    myChart = xlCharts.Add(10, 80, 300, 250)
    chartPage = myChart.Chart
    chartRange = XLSSht.Range("A1", "B2")
    chartPage.SetSourceData(Source:=chartRange)
    chartPage.ChartType = Excel.XlChartType.xlColumnClustered


End Sub