teutamteu > 18-12-17, 04:56 PM
teutamteu > 18-12-17, 04:58 PM
Noname > 18-12-17, 05:16 PM
maidinhdan > 18-12-17, 10:49 PM
Function ExportExcel()
Dim oApp As New Excel.Application
Dim oBook As Excel.Workbook
Dim oSheet As Excel.Worksheet
' Tao phien lam viec Excel voi Duongdanfile
Set oBook = oApp.Workbooks.Add 'Neu dung file mau thi doi .Add thanh .Open(DuongdanFile)
Set oSheet = oBook.Sheets("Sheet1") ' Neu co file mau doi ten Sheet cu the
Dim Tenfile As String
' Dim DB As DAO.Database
Dim rs1 As DAO.Recordset
Dim rs As DAO.Recordset
' tao recordset la Tentable
Set rs1 = CurrentDb.OpenRecordset("SELECT * FROM tblMucLuc", dbOpenDynaset)
Set rs = CurrentDb.OpenRecordset("SELECT * FROM tblNguoigiaodich", dbOpenDynaset)
If rs1.EOF Then Exit Function
rs1.MoveFirst
Do Until rs1.EOF
' Chep du lieu len Excel
oSheet.Range("A1") = "Tieu de trong Bao cao" & "_" & rs1.Fields("Noidungbaocao")
oSheet.Range("A4").CopyFromRecordset rs 'Xuat tu cot A4 tro xuong
Tenfile = "D:\" & rs1.Fields("TenTinh") & "_" & rs1.Fields("Noidung")
oBook.SaveAs Tenfile, 51 '51 la dinh dang kieu Excel *.xlsx
rs1.MoveNext
Loop
' Xoa bien va giai phong bo nho
rs1.Close
rs.Close
oBook.Close
oApp.Quit
End Function