|
需要引用
Microsoft Excel 11.0 Object Library- Dim rs As New ADODB.Recordset
- Dim rst As New ADODB.Recordset
- Dim strProvinceSQL As String, strCitySQL As String, SQL As String
- Dim strProvince() As String
- Dim I As Integer, J As Integer, K As Integer
- Dim xlApp As New Excel.Application
- Dim xlWrk As Excel.Workbook
- Dim xlSht As Excel.Worksheet
- strProvinceSQL = "select distinct 省 from 档案"
- rs.Open strProvinceSQL, CurrentProject.Connection, adOpenKeyset, adLockReadOnly
- I = rs.RecordCount - 1
- ReDim strProvince(I) As String
- For J = 0 To I
- strProvince(J) = rs.Fields(0)
- rs.MoveNext
- Next
- rs.Close
- For K = 0 To UBound(strProvince)
- Set xlWrk = xlApp.Workbooks.Add
- xlWrk.SaveAs CurrentProject.Path & "" & strProvince(K) & ".xls"
- strCitySQL = "select distinct 省,城市 from 档案 where 省='" & strProvince(K) & "'"
- rs.Open strCitySQL, CurrentProject.Connection, adOpenKeyset, adLockReadOnly
- Do While Not rs.EOF
- SQL = "select * from 档案 where 省='" & strProvince(K) & "' and 城市='" & rs.Fields(1) & "'"
- rst.Open SQL, CurrentProject.Connection, adOpenKeyset, adLockReadOnly
- xlWrk.Sheets.Add
- Set xlSht = xlWrk.ActiveSheet
- xlSht.Name = rst.Fields(3)
- For J = 1 To 3
- xlWrk.Worksheets("sheet" & J).Visible = False
- Next
- For I = 0 To rst.Fields.Count - 1
- xlSht.Cells(1, I + 1) = rst.Fields(I).Name
- Next
- xlSht.Range("A2").CopyFromRecordset rst
- xlWrk.Save
- rst.Close
- rs.MoveNext
- Loop
- rs.Close
- xlWrk.Close
- Next
- Set rst = Nothing
- Set rs = Nothing
- xlApp.Quit
- Set xlApp = Nothing
- MsgBox "OK !!" & vbCrLf & "Saved the files in " & vbCrLf & CurrentProject.Path
复制代码
[ 本帖最后由 Henry D. Sy 于 2008-12-6 23:13 编辑 ] |
|