|
3#
楼主 |
发表于 2009-12-18 09:41:12
|
只看该作者
Private Sub 导出_Click()
Dim xlApp As Excel.Application
Dim xlBook As Excel.Workbook
Dim rs As New ADODB.Recordset
Dim sql As String
Dim i As Long
Dim fname As String
Dim shtname As String
On Error GoTo 导出_Err
fname = GetFolder '打开文件夹并选取文件
shtname = InputBox("请选择表:", "表选择窗体", "Sheet1") '指定导出到的工作表(Sheet)名称
sql = "select * from 订单明细 where 生产号=" & Me.生产号
rs.Open sql, CurrentProject.Connection, adOpenKeyset, adLockOptimistic '打开记录集
Set xlApp = CreateObject("Excel.Application") '创建一个Excel实例
xlApp.Application.Visible = True '使Excel可见
Set xlBook = xlApp.Workbooks.Open(fname) '打开Excel工作簿
'导出主表
xlBook.Application.Sheets(shtname).Select '按指定名称选择工作表
xlBook.Application.Range("A1").Value = "生产号"
xlBook.Application.Range("A2").Value = "客户"
xlBook.Application.Range("C1").Value = "车间"
xlBook.Application.Range("C2").Value = "下单日期"
xlBook.Application.Range("B1").Value = Me.生产号
xlBook.Application.Range("B2").Value = Me.客户
xlBook.Application.Range("D1").Value = Me.车间
xlBook.Application.Range("D2").Value = Me.下单日期
'导出子表
xlBook.Application.Cells(4, 1).Value = "序号"
xlBook.Application.Cells(4, 2).Value = "生产号"
xlBook.Application.Cells(4, 3).Value = "客户订单号"
xlBook.Application.Cells(4, 4).Value = "型号规格"
xlBook.Application.Cells(4, 5).Value = "产品名称"
xlBook.Application.Cells(4, 6).Value = "材质"
xlBook.Application.Cells(4, 7).Value = "数量"
xlBook.Application.Cells(4, 8).Value = "交货日期"
For i = 1 To rs.RecordCount
xlBook.Application.Cells(i + 4, 1).Value = rs("序号")
xlBook.Application.Cells(i + 4, 2).Value = rs("生产号")
xlBook.Application.Cells(i + 4, 3).Value = rs("客户订单号")
xlBook.Application.Cells(i + 4, 4).Value = rs("型号规格")
xlBook.Application.Cells(i + 4, 5).Value = rs("产品名称")
xlBook.Application.Cells(i + 4, 6).Value = rs("材质")
xlBook.Application.Cells(i + 4, 7).Value = rs("数量")
xlBook.Application.Cells(i + 4, 8).Value = rs("交货日期")
rs.MoveNext
Next
xlApp.Quit
rs.Close
Set xlApp = Nothing
Set xlBook = Nothing
导出_Exit:
Exit Sub
导出_Err:
MsgBox "数据错误,请检查!"
Resume 导出_Exit
End Sub
??没有? |
|