使用Excel数据源在vb.net表格中绘制图表

我有一个表单,其中有一个button和图表对象。 我有一个Excel表,我正在dynamic填充。 列C&D分别在单元格C1和D1中具有标题“EOS”和“Count”。 数据填充从C2和D2开始,直到可变的行数。

我想要的是,点击buttonn表单时,在购物车区域中会显示一个简单的条形图。 该图应该具有作为C2,C3,…,Cn值的X轴和作为D2,D3,…,Dn值的Y轴。 我从这个页面有以下代码做我所需要的,但使用Access数据库作为源。

任何人都可以告诉我如何使用excel sheet作为数据源实现它?

 '~~> Code to generate the chart Private Sub Button2_Click(ByVal sender As System.Object, ByVal _ e As System.EventArgs) Handles Button2.Click Dim strConn As String = _ "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & TextBox1.Text & _ ";Persist Security Info=False;" Dim tblFields As String = "SELECT * from Table1" Dim conn As New OleDbConnection(strConn) Dim oCmd As New OleDbCommand(tblFields, conn) Dim oData As New OleDbDataAdapter(tblFields, conn) Dim ds As New DataSet conn.Open() oData.Fill(ds, "Table1") conn.Close() Chart1.DataSource = ds.Tables("Table1") Dim Series1 As Series = Chart1.Series("Series1") Series1.Name = "Sales" Chart1.Series(Series1.Name).XValueMember = "nFruits" Chart1.Series(Series1.Name).YValueMembers = "nSales" Chart1.Size = New System.Drawing.Size(780, 350) End Sub 

有很多从Excel中读取的例子

使用VB.NET读写Excel文件 http://www.codeproject.com/Articles/18073/Reading-and-writing-an-Excel-file-using-VB-NET

在Visual Basic .NET中的Excel工作簿中读取数据 http://www.vb-helper.com/howto_net_read_excel.html

VB.NET Excel http://www.dotnetperls.com/excel-vbnet

另外库用C#编写,用于读取Microsoft Excel文件('97-2007) http://exceldatareader.codeplex.com/

我得到它的工作! 错误是因为我没有提供excel文件的绝对path。 这里是代码:

 Dim strConn As String = _ "Provider=Microsoft.Jet.OLEDB.4.0;" & _ "Data Source=C:\Temp\EOS123.xls;Extended Properties=""Excel 8.0;HDR=YES;""" Dim tblFields As String = "SELECT EOS, Count from [Sheet1$]" Dim conn As New OleDbConnection(strConn) Dim oCmd As New OleDbCommand(tblFields, conn) Dim oData As New OleDbDataAdapter(tblFields, conn) Dim ds As New DataSet conn.Open() oData.Fill(ds, "Sheet1") conn.Close() Chart1.DataSource = ds.Tables("Sheet1")