直接用VB.net 读Excel,然后再用Vb.net 绘图.
最近刚好在做读excel的程序,给你个函数.
vb.net 读 Excel
''' <summary>
''' 根据表名获取Excel内容。
''' </summary>
''' <param name="sheetName">工作表名称,例:sheet1</param>
''' <param name="filePath">Excel的完整路径</param>
''' <param name="where">可选参数,筛选选择数据的范围,例"站名='哈尔滨'"</param>
''' <returns>在filepath中以sheetname为名字的工作表</returns>
''' <remarks></remarks>
Public Shared Function GetTableFromExcel(sheetName As String, filePath As String, Optional where As String = "") As DataTable
Dim connStrTemplate As String = String.Empty
Dim fileType As String = System.IO.Path.GetExtension(filePath)
If String.IsNullOrEmpty(fileType) Then Return Nothing
Select Case fileType
Case ".xlsx"
connStrTemplate = "Provider=Microsoft.ACE.OLEDB.12.0;" & "Data Source=" & filePath & ";" & "Extended Properties='Excel 12.0;HDR=YES;IMEX=1'"
Case ".xls"
connStrTemplate = "Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source=" & filePath & ";" & "Extended Properties='Excel 8.0;HDR=YES;IMEX=1'"
Case Else
Throw New Exception("文件格式有误")
End Select
Dim dt As DataTable = Nothing
If (Not System.IO.File.Exists(filePath)) Then Return Nothing
Dim conn As OleDbConnection = New OleDbConnection(String.Format(connStrTemplate, filePath))
Try
conn.Open()
If (sheetName Is Nothing OrElse sheetName.Trim().Length = 0) Then '如果表名不存在则获取第一个表.
Dim schemaTable As DataTable = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, {Nothing, Nothing, Nothing, "TABLE"})
sheetName = schemaTable.Rows(0)("TABLE_NAME").ToString().Trim()
End If
Dim da As OleDbDataAdapter = Nothing
Dim ds As DataSet = New DataSet()
Dim strSQL As String = "Select * From [" & sheetName & "$]"
If (Not String.IsNullOrWhiteSpace(where)) Then
'strSQL = "Select * From [" + sheetName + "$] where 站名='哈尔滨'"
strSQL = String.Format("Select * From [" & sheetName & "$] Where {0}", where)
End If
Try
da = New OleDbDataAdapter(strSQL, conn)
da.Fill(ds)
dt = ds.Tables(0)
Catch er As Exception
da = New OleDbDataAdapter("Select * From [sheet1$]", conn)
da.Fill(ds)
dt = ds.Tables(0)
End Try
Catch ex As Exception
Throw ex
Finally
conn.Close()
End Try
Return dt
End Function
#End Region
绘图用chart控件,不是很难,网上例程也很多,随便找找
第一次回帖,有分没?