»áÔ±£º ÃÜÂ룺 ¡¡Ãâ·Ñ×¢²á | Íü¼ÇÃÜÂë | »áÔ±µÇ¼ ÍøÒ³¹¦ÄÜ£º ¼ÓÈëÊÕ²Ø ÉèΪÊ×Ò³ ÍøÕ¾ËÑË÷  
¼¼ÊõÎĵµ > VBÎĵµ > Êý¾Ý±¨±í
°ÑDataGridµÄÊý¾Ý¿ìËÙдÈëEXCEL
·¢±íÈÕÆÚ£º2003-06-23 00:00:00×÷Õߣº¶ ³ö´¦£º  

µÚÒ»VBÂÛ̳ÉÏ(http://www.vbgood.com)µÄÅóÓѾ­³£Ìá³ö¡°ÔõÑù°ÑDataGridµÄÊý¾Ý¿ìËÙдÈëEXCEL¡±Õâ¸öÎÊÌâ,Ò²¿´Á˺ܶàÓÃVB¿ØÖÆEXCEL±¨±íµÄÎÄÕºÍʵÀý,×ܵĸоõÊÇÓÃEXCEL¶ÔÏóÒ»¸ö¸ñ×ÓÒ»¸ö¸ñ×ÓµÄд£¬ËÙ¶ÈÌ«Âý¡£¿´¹ýһЩ¹úÄÚÍâµÄ´óÐÍÈí¼þ£¬×¢Òâµ½ËûÃǶÔÊý¾ÝµÄ±¨±í´¦ÀíÒ²ÊÇдµ½EXCELÀïÃ棬ÏÔÈ»£¬EXCEL¶ÔÊý¾Ý±¨±íµÄ´¦Àíȷʵǿ´óºÍÁ÷ÐУ¬¼ÈÈ»Èç´Ë,¾Í°´ÕÕ³ѸÏÈÉúµÄÖ÷ÕÅ:¡°ÄÃÀ´£¡¡±¡£

Òª½â¾öµÄµÚÒ»¸öÎÊÌâÊÇËٶȡ£ºÜ¶àÅóÓÑÔÚʹÓÃEXCEL¶ÔÏóµÄʱºòÖ»×¢Òâµ½ËüÊǸö¿ÉÒÔ¿ØÖƵĶÔÏ󣬶øºöÂÔÁËEXCELÒ²¿ÉÒÔ×÷Ϊһ¸öÊý¾Ý¿âʹÓã¬ËüµÄÃû×Ö¾ÍÊÇÊý¾Ý¿âµÄÃû×Ö£¬¹¤×÷±í¾ÍÊÇÒ»ÕÅÊý¾Ý¿âÖÐµÄ±í£¬ÕâÑù»°£¬¿âÖ®¼äÊý¾ÝµÄǨÒÆʹÓÃSQLÓï¾äÒª¿ì¶àÁË¡£

µÚ¶þ¸öÎÊÌâÊÇDataGrid×÷ΪÏÔʾÊý¾ÝµÄÍø¸ñ¿Ø¼þ£¬ÈκÎʱºò²»Äܵ±×÷Êý¾Ý¿âʹÓá£Òò´ËÔÚÒª°ÑËüµÄÊý¾ÝÊä³öµÄʱºò¿ÉÒÔ¿¼ÂÇÕÒËüÏÔʾµÄÊý¾ÝÔ´£¬°ÑËüµÄ²éѯÌõ¼þÓëÊä³öµÄ²éѯÌõ¼þÏàÒ»Ö¾ÍÍêÈ«¿ÉÒÔʵÏÖÎÒÃǵÄÄ¿µÄ¡£

±¾ÊÂÀýÑÝʾÁ˲»Í¬µÄÊý¾ÝԴдÈëEXCEL£¬ÔÚVB6+WIN98ÏÂÃæµ÷ÊÔͨ¹ý¡£ÒªÊ¹ÓÃËü±ØÐë×öµ½ÒÔÏÂ3µã£º

1£® ÔÚµ±Ç°Ä¿Â¼ÀïÃæÓÐÒ»¸öACCESSÊý¾Ý¿â£¬ÀïÃæÓÐ"±ÏҵѧÉú"£¬"ÔÚУѧÉú"ºÍ"ÍËѧѧÉú"ÈýÕÅ±í£¬Ã¿ÕűíÀïÃæÓÐ"°à¼¶","ÐÕÃû","ÐÔ±ð","¼Òͥסַ"ºÍ"¹¤×÷µ¥Î»"µÈ×ֶΣ¬ÒÔ¼°Ò»Ð©Êý¾Ý¡£

2£® Òª´Ó"¹¤³Ì"¡ú"ÒýÓÃ"ÀïÃæÒýÓÃMicrosoft Active Data Objects 2.1 LibraryºÍMicrosoft DAO 3.6 Object LibraryÁ½¸ö¶ÔÏó¿â¡£

3£® EXCELµÄ°²×°Â·¾¶ÊÇĬÈÏ°²×°Â·¾¶¡£

ÔÚ´°ÌåÉÏÃæ·ÅÖÃÒ»¸öDataGridºÍÈý¸öCommandButton,°ÑÏÂÁдúÂ븴ÖƵ½ÀïÃ棬²¢°´F5ÔËÐУ¬¾Í¿ÉÒÔʵÏÖÎÒÃǵÄÄ¿µÄ¡£

´úÂëÈçÏ£º

'ÉùÃ÷APIº¯Êý

Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long

'¶¨Òå±äÁ¿

Dim Cnn1 As New ADODB.Connection

Dim rst1 As New ADODB.Recordset

Dim StrSQL As String

Dim TabName As String

Dim Selection As String

Private Sub Command1_Click()

'ÉèÖõÚÒ»¸öÊý¾ÝÔ´µÄ²éѯÌõ¼þ

TabName = "±ÏҵѧÉú"

Selection = "°à¼¶,ÐÕÃû,¹¤×÷µ¥Î»"

'»ñÈ¡Êý¾Ý

ReqData

End Sub

Private Sub Command2_Click()

'ÉèÖõڶþ¸öÊý¾ÝÔ´µÄ²éѯÌõ¼þ

TabName = "ÔÚУѧÉú"

Selection = "*"

'»ñÈ¡Êý¾Ý

ReqData

End Sub

Private Sub Command3_Click()

'ÉèÖõÚÈý¸öÊý¾ÝÔ´µÄ²éѯÌõ¼þ

TabName = "ÍËѧѧÉú"

Selection = "°à¼¶,ÐÕÃû,ÐÔ±ð"

'»ñÈ¡Êý¾Ý

ReqData

End Sub

Private Sub Command4_Click()

'Èç¹ûEXCELÎļþÒѾ­´ò¿ª,ÐèÒªÏȹرÕËü.

Dim lpClassName As String

Dim lpCaption As String

Dim Handle As Long

lpClassName = "XLMAIN"

lpCaption = "Microsoft Excel - MyExcel.xls"

Handle = FindWindow(lpClassName$, lpCaption$)

If Handle <> 0 Then

MsgBox "ÇëÏȹرÕEXCELÎļþ!", vbOKOnly + vbInformation, "²»ÄܶÔÒѾ­´ò¿ªµÄÎļþ½øÐÐд²Ù×÷!"

Exit Sub

End If

'¼ì²éEXCELÎļþÊÇ·ñ´æÔÚ,Èç¹û´æÔÚÔòɾ³ý

If Dir(App.Path & "\MyExcel.xls") <> "" Then Kill App.Path & "\MyExcel.xls"

'½øÐÐÊý¾Ýת»»

Dim dbs As Database

'´ò¿ªÊý¾Ý¿â

Set dbs = OpenDatabase(App.Path & "\db1.mdb")

'°ÑÊý¾Ýµ¼ÈëEXCEL

dbs.Execute "SELECT " & Selection & " INTO [Excel 8.0;DATABASE=" & App.Path & "\MyExcel.xls].[WorkSheet1] FROM " & TabName

'¹Ø±ÕÊý¾Ý¿â¶ÔÏó

dbs.Close

'ÊÍ·ÅÊý¾Ý¿â¶ÔÏó

Set dbs = Nothing

'µ÷ÓÃEXCEL´ò¿ª²úÉúµÄEXCEL±í¸ñ

Shell "C:\Program Files\Microsoft Office\Office\EXCEL.EXE " & App.Path & "\MyExcel.xls", vbMaximizedFocus

End Sub

Private Sub ReqData()

'ÉèÖòéѯÓï¾ä

StrSQL = "SELECT " & Selection & " FROM " & TabName

'Èç¹ûÊý¾Ý¿âÒÑ´ò¿ªµÄÔòÏȹرÕ,·ÀÖ¹³ö´í

If Cnn1.ConnectionString <> "" Then Cnn1.Close

'´ò¿ªÊý¾Ý¿â

Cnn1.Open "Provider=Microsoft.Jet.OLEDB.3.51; Data Source=" & App.Path & "\db1.mdb;"

'ÉèÖüǼ¼¯µÄ´ò¿ª·½Ê½ºÍËøµÄ»úÖƵÈ

With rst1

  .CursorType = adOpenKeyset

  .LockType = adLockOptimistic

  .Open StrSQL, Cnn1, , , adCmdText

End With

'ΪDataGrid1ÉèÖÃÊý¾ÝÔ´

Set DataGrid1.DataSource = rst1

DataGrid1.Refresh

'ΪDataGrid1ÉèÖñêÌâ,²¢ÏÔʾ¼Ç¼Êý

DataGrid1.Caption = "±í[" & TabName & "]¹²" & rst1.RecordCount & "Ìõ¼Ç¼"

End Sub

Private Sub DataGrid1_RowColChange(LastRow As Variant, ByVal LastCol As Integer)

'ΪDataGrid1ÉèÖñêÌâ,²¢ÏÔʾ¼Ç¼Êý,ÓôËʼþÊÇ·ÀÖ¹²Ù×÷Աɾ³ý¼Ç¼ºó¼Ç¼Êý·¢Éú±ä»¯

DataGrid1.Caption = "±í[" & TabName & "]¹²" & rst1.RecordCount & "Ìõ¼Ç¼"

End Sub

±¾ÊÂÀýÖ»ÊÇÑÝʾÁËACCESSÊý¾Ý¿âºÍDataGridÒÔ¼°¼òµ¥µÄ²éѯ£¬¶ÔÓÚÆäËûÀàÐ͵ÄÊý¾Ý¿âºÍÍø¸ñ¿Ø¼þÒÔ¼°¸´ÔӵIJéѯ¶¼¿ÉÒÔ²ÎÕÕ±¾Àý½øÐÐÐÞ¸ÄʹÓã¬Ï£Íû¶Ô´ó¼ÒµÄ±à³ÌÓÐËùÆô·¢¡£

ÒÔÉÏʾÀý´úÂëÈçÐèÒª£¬¿ÉдÐÅduyunqing@163.netË÷È¡¡£

¡¾·µ»Ø¶¥²¿¡¿ ¡¾´òÓ¡±¾Ò³¡¿ ¡¾¹Ø±Õ´°¿Ú¡¿

¹ØÓÚÎÒÃÇ / ¸øÎÒÁôÑÔ / °æȨ¾Ù±¨ / Òâ¼û½¨Òé / ÍøÕ¾±à³ÌQQȺ   
Copyright ©2003- 2024 Lihuasoft.net webmaster(at)lihuasoft.net ¼ÓÔØʱ¼ä 0.00349