- ©«¤l
- 519
- ¥DÃD
- 54
- ºëµØ
- 0
- ¿n¤À
- 595
- ÂI¦W
- 258
- §@·~¨t²Î
- win 10
- ³nÅ骩¥»
- []
- ¾\ŪÅv
- 50
- ©Ê§O
- ¨k
- µù¥U®É¶¡
- 2013-3-19
- ³Ì«áµn¿ý
- 2024-11-25
|
¦^´_ 8# GBKEE
¥H¤@ÓSQL±Ôz§¹¦¨»Ý¨D²×©ó¥i¥H¹F¦¨¦p¤U¡A¨Ñ°Ñ¦Ò¡F ÁÂÁÂ- Sub ExcelSQL2()
- Dim i&, j%
- Dim cnn As ADODB.Connection
- Dim rs As ADODB.Recordset
- Dim SQL As String, FileName$
- Dim ws1 As Worksheet
- Dim ws2 As Worksheet
- Dim TimeInterval#
- Dim Time0#
- Dim StartTime, EndTime, TimeStr$
-
- Set ws1 = Sheets("CT")
- Set ws2 = Sheets("Temp")
- ws2.Cells.Clear
-
- FileName = ThisWorkbook.FullName
-
- ws1.Select
- ' StartTime = #1/1/2015 12:00:01 AM#
- ' EndTime = #12/31/2030#
- ' StartTime = CDbl(StartTime)
- ' EndTime = CDbl(EndTime)
- StartTime = "2015/01/01 00:00:01"
-
- TimeInterval = 10 '¶¡¹j 10 ¤ÀÄÁ
-
- ' Time0 = Timer
- TimeStr = " format( iif(MINUTE([¤é´Á®É¶¡]) mod " & TimeInterval & " = 0,[¤é´Á®É¶¡], " & _
- "DateAdd(""n"", " & TimeInterval & "-MINUTE([¤é´Á®É¶¡]) mod " & TimeInterval & _
- " ,[¤é´Á®É¶¡])),""yyyy/mm/dd hh:mm"") "
-
- '=== ³B²z¼ÆÈÄæ¦ìªº¨C 10¤ÀÄÁ¥§¡ ===========================
- Set cnn = New ADODB.Connection
- cnn.Provider = "Microsoft.ACE.OLEDB.12.0"
- cnn.ConnectionString = "Data Source=" & FileName & ";Extended Properties=""Excel 12.0 xml;HDR=Yes;"""
- cnn.Open
-
- SQL = "SELECT " + TimeStr + " as [DateTime], CTcode, " & _
- "AVG([Volts]) as [avgVolt], AVG([Hz]) as [avgHz] " & _
- " From [" & ws1.Name & "$] " & _
- " Where [¤é´Á®É¶¡] >= #" & StartTime & "# " & _
- " Group by " + TimeStr + " ,CTcode " & _
- " Order by " + TimeStr + " ,CTcode"
-
- Set rs = cnn.Execute(SQL)
-
- With ws2
- For i = 0 To rs.Fields.Count - 1
- .Cells(1, i + 1) = rs.Fields(i).Name
- Next
- .Cells(2, 1).CopyFromRecordset rs
- End With
-
- rs.Close
- cnn.Close
- Set rs = Nothing
- Set cnn = Nothing
- ' Debug.Print Timer - Time0
- End Sub
½Æ»s¥N½X |
|