簡體   English   中英

從Excel文件中檢索數據以使用Visual Basic制作圖表

[英]Retrieve data from excel file to make charts using the visual basic

我是Visual Basic的新手。 我正在使用Visual Studio 2013和MS Excel2010。我想用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軸? 任何幫助,將不勝感激。 謝謝!

這里是。 我正在使用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 +”從其中刪除字符串,如在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()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM