设为首页收藏本站Access中国

Office中国论坛/Access中国论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

返回列表 发新帖
查看: 4617|回复: 7
打印 上一主题 下一主题

Access引用Excel为何excel进程关不掉

[复制链接]
1#
发表于 2016-9-13 22:16:34 | 显示全部楼层
Private Sub CreatePivotTable_Click()

    Dim XLA As New Excel.Application
    Dim XLB As Workbook
    Dim XLS As Worksheet
    Dim PC As PivotCache
    Dim PT As PivotTable
    Dim rs As New ADODB.Recordset
    Dim sSQL As String


    Set XLA = CreateObject("Excel.Application")
    Set XLB = Nothing
    Set XLS = Nothing
    Set XLB = XLA.Workbooks().Add

    XLB.SaveAs CurrentProject.Path & "\PivotTable.xlsx"

    Set XLS = XLB.Worksheets.Add
    XLS.Name = "PivotSheet"
    XLS.Activate

    rs.CursorLocation = adUseClient
    sSQL = "SELECT style,po,color,pack,quantity FROM ORD"
    rs.Open sSQL, CurrentProject.Connection, adOpenKeyset, adLockReadOnly

    Set PC = XLA.ActiveWorkbook.PivotCaches.Create(xlExternal)
    Set PC.Recordset = rs

    Set PT = XLA.ActiveSheet.PivotTables.Add(PC, XLA.ActiveSheet.Range("A1"), "A")

    With XLA.ActiveSheet.PivotTables("A")
        .PivotFields("STYLE").Orientation = xlPageField
        .PivotFields("PO").Orientation = xlRowField
        .PivotFields("COLOR").Orientation = xlRowField
        .PivotFields("pack").Orientation = xlRowField
'      .PivotFields("SIZE").Orientation = xlColumnField
        .PivotFields("QUANTITY").Orientation = xlDataField
    End With

    rs.Close

    XLB.Save

    MsgBox "PivotTable Saved On " & CurrentProject.Path
    Set PC = Nothing
    Set PT = Nothing
    Set rs = Nothing
    Set XLS = Nothing

XLB.Close

    Set XLB = Nothing
    XLA.Quit
    Set XLA = Nothing

End Sub
您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|站长邮箱|小黑屋|手机版|Office中国/Access中国 ( 粤ICP备10043721号-1 )  

GMT+8, 2024-5-9 07:41 , Processed in 0.131097 second(s), 24 queries .

Powered by Discuz! X3.3

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表