【问题标题】:Retrieve data from excel file to make charts using the visual basic从 excel 文件中检索数据以使用 Visual Basic 制作图表
【发布时间】:2014-04-09 11:58:06
【问题描述】:

我对 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 范围存储到一个数组列表中。我使用数组作为图表点。该程序现在可以运行而没有任何错误,但它只显示图表标题。我在这里做错了什么?我是否必须为图表循环数组以显示 X 和 Y 轴?任何帮助,将不胜感激。谢谢!

【问题讨论】:

    标签: vb.net excel


    【解决方案1】:

    来了。我正在使用 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()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-09-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-10
      • 1970-01-01
      相关资源
      最近更新 更多