請大家提供個思路或者路徑
uj5u.com熱心網友回復:
直接用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控制元件,不是很難,網上例程也很多,隨便找找
第一次回帖,有分沒?
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/77816.html
標籤:VB.NET
