如何使用Chart控件????????在线等候....急!急!!急!!!!!!!

emit 2003-12-03 07:30:56
各位,用VB从数据库中取出数据后导到Excel表中去,同时又要用Chart控件将数据以Graph图形显示出来,当我用Chart控件在窗体(设计时)上托动时出现一个Graph图形和Excel表,我知道这是一个例子,现在的问题是:如何用代码来实现我指定的Excel表连接????
...全文
213 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
taomaintao 2003-12-10
  • 打赏
  • 举报
回复
Option Explicit

#Const DEBUG_MSGS = 0

Private Const NUM_TESTS = 5

' A record for a student.
Private Type StudentRecord
LastName As String
FirstName As String
Age As Integer
StudentID As Integer
Score(1 To NUM_TESTS) As Integer
End Type

Private NumStudents As Integer
Private Students() As StudentRecord
' Graph the age data.
Private Sub GraphAges()
Dim student As Integer
Dim lft As Single
Dim i As Integer
Dim the_age As Integer
Dim txt As String
Dim font_hgt As Single

' Make a convenient scale.
lft = -NumStudents / 10
picAge.Scale (lft, 21)-(NumStudents + 0.25, -4)
font_hgt = 1.2 * picAge.TextHeight("X")

' Draw the axes.
picAge.Line (0, 20)-(0, 0)
picAge.Line -(NumStudents, 0)
For i = 1 To 19
picAge.Line (-lft / 6, i)-Step(lft / 3, 0)
Next i

For student = 1 To NumStudents
' Draw the student's histogram.
the_age = Students(student).Age
FillColor = QBColor(student Mod 8)
picAge.Line (student - 1, 0)- _
(student, the_age), _
QBColor(student Mod 8), BF

picAge.Line (student - 1, 0)- _
(student, the_age), _
vbBlack, B

' Draw the student's age.
txt = Format$(the_age)
picAge.CurrentX = student - 0.5 - _
picAge.TextWidth(txt) / 2
picAge.CurrentY = the_age - font_hgt
picAge.Print txt

' Draw the student's name.
txt = Students(student).FirstName
picAge.CurrentX = student - 0.5 - _
picAge.TextWidth(txt) / 2
picAge.CurrentY = font_hgt * 0.5
picAge.Print txt

txt = Students(student).LastName
picAge.CurrentX = student - 0.5 - _
picAge.TextWidth(txt) / 2
picAge.CurrentY = font_hgt * 1.5
picAge.Print txt
Next student
End Sub
' Graph the test score data.
Private Sub GraphScores()
Dim test As Integer
Dim lft As Single
Dim i As Integer
Dim the_score As Integer
Dim txt As String
Dim font_hgt As Single
Dim student As Integer

' Make a convenient scale.
lft = -NUM_TESTS / 10
picScore.Scale (1 + lft, 110)-(NUM_TESTS + 0.25, -10)
font_hgt = picScore.TextHeight("X")

' Draw the axes.
picScore.Line (1, 100)-(1, 0)
picScore.Line -(NUM_TESTS, 0)
For i = 10 To 100 Step 10
picScore.Line (1 - lft / 6, i)-Step(lft / 3, 0)
txt = Format$(i)
picScore.CurrentX = 1 + lft / 4 - picScore.TextWidth(txt)
picScore.CurrentY = i - font_hgt / 2
picScore.Print txt
Next i
font_hgt = font_hgt * 1.2
picScore.DrawStyle = vbDot
For test = 1 To NUM_TESTS
picScore.Line (test, 0)-(test, 100)
txt = Format$(test)
picScore.CurrentX = test - picScore.TextWidth(txt) / 2
picScore.CurrentY = font_hgt / 3
picScore.Print txt
Next test
picScore.DrawStyle = vbSolid

' Graph the scores.
For student = 1 To NumStudents
' Use a different color.
picScore.ForeColor = QBColor(student Mod 8)

the_score = Students(student).Score(1)
picScore.CurrentX = 1
picScore.CurrentY = the_score
For test = 2 To NUM_TESTS
the_score = Students(student).Score(test)
picScore.Line -(test, the_score)
Next test
Next student
End Sub

' Load the data from the database.
Private Sub LoadData()
Dim ws As Workspace
Dim db As Database
Dim student_rs As Recordset
Dim score_rs As Recordset
Dim query As String

' Use the default workspace.
Set ws = DBEngine.Workspaces(0)

' Open the database.
Set db = ws.OpenDatabase(App.Path & "\graphdb.mdb")

' Create a Recordset to read name,
' ID, and age information.
query = _
"SELECT FirstName, LastName, " & _
"StudentID, Age FROM Students " & _
"ORDER BY StudentID"
Set student_rs = _
db.OpenRecordset(query, dbOpenSnapshot)

' Read the name, ID, and age information.
With student_rs
Do While Not .EOF
NumStudents = NumStudents + 1
ReDim Preserve Students(1 To NumStudents)
Students(NumStudents).LastName = !LastName
Students(NumStudents).FirstName = !FirstName
Students(NumStudents).Age = !Age
Students(NumStudents).StudentID = !StudentID

#If DEBUG_MSGS = 1 Then
Debug.Print _
Students(NumStudents).FirstName & " " & _
Students(NumStudents).LastName & ", Age: " & _
Students(NumStudents).Age & ", ID: " & _
Students(NumStudents).StudentID
#End If

' Get this student's test scores.
query = _
"SELECT TestNumber, Score FROM " & _
"TestScores WHERE StudentID=" & _
!StudentID & _
" ORDER BY TestNumber"
Set score_rs = _
db.OpenRecordset(query, dbOpenSnapshot)
Do While Not score_rs.EOF
Students(NumStudents).Score( _
score_rs!TestNumber) = _
score_rs!Score

#If DEBUG_MSGS = 1 Then
Debug.Print _
" Score(" & _
Format$(score_rs!TestNumber) & ") = " & _
Students(NumStudents).Score( _
score_rs!TestNumber)
#End If

score_rs.MoveNext
Loop
score_rs.Close

' Go get the next student record.
.MoveNext
Loop
End With
student_rs.Close

' Close the workspace.
ws.Close
End Sub

Private Sub Form_Load()
' Load the data.
LoadData

' Graph the age data.
GraphAges

' Graph the test score data.
GraphScores
End Sub

taomaintao 2003-12-10
  • 打赏
  • 举报
回复
Access数据库的OLE型字段中存放的图形是按OLE格式存放的图像,而不是VB的Image控件或Picturebox控件所支持的标准图片格式(.bmp、.rle、.ico、.gif、.jpg、.emf和.wmf),所以不能使用Image控件或Picturebox控件来观看。应该使用VB的OLE控件,该控件在工具条中如下图所示。我们以VB5目录下的NWIND.MDB文件为例说明这一过程。首先在窗体上添加Data控件(以下称为Data1),将Database设为NWIND.MDB文件,然后将RecordSource属性设为Employees表。再在窗体上添加OLE控件,此时VB会显示一个对话框让你选择文件类型,按“取消”键(如果这时你误按了“确定”键,则需将OLE控件的Class属性置为空。)。设置DataSource为Data1,DataField为Photo(这是一个OLE图形字段)。这时你再运行就可以得到想要的结果了。这个技巧也适用VB 4.0的32位版本。如果是使用VB 4.0的16位版本或VB 3.0, 不能使用这一技巧,可以访问Microsoft Knowledge Base(http://www.microsoft.com/kb/default.asp),其中的Q147727 是针对VB 4.0的16位版本,Q113957则针对VB 3.0。从这两篇文章中,你也可以了解到Access 1.x和2.0是怎样存储OLE字段的。
HereWeAre 2003-12-10
  • 打赏
  • 举报
回复
我已经给了你一份详细的次料,请你查看一下,也许对你有帮助(在你的邮箱里)
hhjjhjhj 2003-12-08
  • 打赏
  • 举报
回复
用OLE插入Graph
引用Microsoft Graph x.0 Object Library

Private Sub Command1_Click()
Dim obGR As Graph.Application
Set obGR = Me.OLE1.object.Application
obGR.FileImport "C:\bbb\ccc.xls"
'.......
End Sub
余下的照着“对象浏览器”慢慢研究吧

zichen66 2003-12-08
  • 打赏
  • 举报
回复
如果你有安裝MSDN的話會在C:\Program Files\Microsoft Visual Studio\MSDN98\98VS\1028\SAMPLES\VB98\ChrtSamp
這就是一個很詳細的例子
上官云峰 2003-12-03
  • 打赏
  • 举报
回复
首先你要先引用excel,
你的vb安装msdn了吗?
Private Sub Form_Load()
' 然后用 GetObject 方法设置对象。
Set wkbObj = GetObject _
("C:\My Documents\MySpread.xls")
End Sub
emit 2003-12-03
  • 打赏
  • 举报
回复

大家来帮帮忙吧

online 2003-12-03
  • 打赏
  • 举报
回复
如何用代码来实现我指定的Excel表连接????
Public Function Read_Excel _
(ByVal sFile _
As String) As ADODB.Recordset

On Error GoTo fix_err
Dim rs As ADODB.Recordset
Set rs = New ADODB.Recordset
Dim sconn As String

rs.CursorLocation = adUseClient
rs.CursorType = adOpenKeyset
rs.LockType = adLockBatchOptimistic

sconn = "DRIVER=Microsoft Excel Driver (*.xls);" & "DBQ=" & sFile
rs.Open "SELECT * FROM [sheet1$]", sconn
Set Read_Excel = rs
Set rs = Nothing
Exit Function
fix_err:
Debug.Print Err.Description + " " + _
Err.Source, vbCritical, "Import"
Err.Clear
End Function

Private Sub cmdReadXLS_Click()
Dim obj As Read_Files.CReadFile
Set obj = New Read_Files.CReadFile

Set dgData.DataSource = obj.Read_Excel(App.Path & "\" & "test.xls")
Set obj = Nothing
End Sub
online 2003-12-03
  • 打赏
  • 举报
回复
????
没有明白?????

7,789

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧