从excel文件中检索数据以使用visual basic创建图表

时间:2014-03-06 17:16:36

标签: vb.net excel

我是Visual Basic的新手。我正在使用Visual Studio 2013和MS Excel 2010.我想用VB编写代码,可以从Excel .xlsx文件中检索信息并使用该信息来制作图表。

这是编辑后的版本:

Imports System.Reflection
Imports Excel = Microsoft.Office.Interop.Excel
'Add reference Assemblies, Framework, System.Windows.Forms.DataVisualization
'Imports System.Windows.Forms.DataVisualization.Charting


Public Class Form1

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

    Dim excelApp As Excel.Application
    Dim excelWB As Excel.Workbook
    Dim excelWS As Excel.Worksheet
    Dim FNameRng As Excel.Range
    Dim AveRng As Excel.Range
    Dim AveCLRng As Excel.Range
    Dim AveUCLRng As Excel.Range
    Dim FNameArry As New ArrayList()
    Dim AveArry As New ArrayList()
    Dim AveCLArry As New ArrayList()
    Dim AveUCLArry As New ArrayList()

    excelApp = CreateObject("Excel.Application")
    excelApp.Visible = False
    'Open the Workbook
    excelWB = excelApp.Workbooks.Open("C:\Users\Joesph\Documents\Charts\Control Limit\18x17 - 10 mil stop.xlsx")
    excelWS = excelApp.Sheets("18x17 - 10 mil stop")

    'Set the Range for File Name
    FNameRng = excelWS.Range("A2", excelWS.Range("A2").End(Excel.XlDirection.xlDown))
    'Set the Range for Average Data
    AveRng = excelWS.Range("B2", excelWS.Range("B2").End(Excel.XlDirection.xlDown))
    AveCLRng = excelWS.Range("H2", excelWS.Range("H2").End(Excel.XlDirection.xlDown))
    AveUCLRng = excelWS.Range("I2", excelWS.Range("I2").End(Excel.XlDirection.xlDown))

    'Store Range as Array
    FNameArry.Add(FNameRng.Value)
    AveArry.Add(AveRng.Value)
    AveCLArry.Add(AveCLRng.Value)
    AveUCLArry.Add(AveUCLRng.Value)

    Me.CenterToScreen()
    Me.WindowState = FormWindowState.Maximized

    Chart1.Titles.Add("Title1")
    Chart1.Titles(0).Text = "Average"
    Chart1.Titles(0).Font = New Font("Garamond", 24, FontStyle.Bold)

    Chart1.Series("Series1").XValueMember = "FNameArry"
    Chart1.Series("Series1").YValueMembers = "AveArry"
    Chart1.Series("Series1").YValueMembers = "AveCLArry"
    Chart1.Series("Series1").YValueMembers = "AveUCLArry"


End Sub
End Class

因此,我将Excel范围存储到arraylist中。我使用数组作为图表点。程序现在可以运行而没有任何错误,但它只显示图表标题。我在这做错了什么?我是否必须循环图表的数组以显示X和Y轴?任何帮助,将不胜感激。谢谢!

1 个答案:

答案 0 :(得分:0)

在这里。我使用OLE db驱动程序从xlsx而不是Interop获取数据。我也使用3个系列而不是具有多个Y值的单个系列。

Imports System.Windows.Forms.DataVisualization.Charting
Imports System.Data
Imports System.Data.OleDb

'The Excel file name
Dim fileName As String = "YourExcelData.xlsx"

'connection string for Xlsx files -   Microsoft ACE OLEDB 12.0
'Connect to Excel 2007 (and later) files with the Xlsx file extension. 
'That is the Office Open XML format with macros disabled.
' "HDR=Yes;" indicates that the first row contains columnnames, not data. 
'"HDR=No;" indicates the opposite.

'“+ fileNameString +”从中删除String作为上面的defind     Dim sConn As String =“Provider = Microsoft.ACE.OLEDB.12.0; Data Source =”+ fileNameString +“; Extended Properties =”“Excel 12.0 Xml; HDR = YES”“;”     将myConnection调暗为新的OleDbConnection(sConn)     myConnection.Open()

' The code to follow uses a SQL SELECT command to display the data from the worksheet.
' Create new OleDbCommand to return data from worksheet.
' change range 
Dim myCommand As New OleDbCommand("Select * From [data1$A2:I2500]", myConnection)

' create a database reader    
Dim myReader As OleDbDataReader = myCommand.ExecuteReader (CommandBehavior.CloseConnection)
' Populate the chart with data in the file
' can also use Chart.DataBindTable
Chart1.Series(0).Points.DataBindXY(myReader, "FNameArry", myReader, "AveArry")
Chart1.Series(1).Points.DataBindXY(myReader, "FNameArry", myReader, "AveCLArry")
Chart1.Series(2).Points.DataBindXY(myReader, "FNameArry", myReader, "AveUCLArry")

' close the reader and the connection
myReader.Close()
myConnection.Close()