Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
VFP to Excel Pivot Tables
Message
De
22/01/2002 19:34:13
 
 
À
22/01/2002 13:18:08
Todd Cottengim
Alpine Land Information Services
Redding, Californie, États-Unis
Information générale
Forum:
Visual FoxPro
Catégorie:
COM/DCOM et OLE Automation
Divers
Thread ID:
00608533
Message ID:
00608725
Vues:
13
>Is there a kind soul out there who would be willing to share Pivot Table code with me?

* this is a fragment from one of my Pivot reports
* I create a serties of queries, gather denormalized data
* and COPY TO MyTestFile.XLS TYPE XLS
#INCLUDE c:\common\include\library.h
#INCLUDE c:\common\include\vfp_excel.h

* open Excel sheet
LOCAL loExcel as Excel.Application
LOCAL loWorkBook as Excel.Workbook
LOCAL loActiveSheet as Excel.Sheets
LOCAL loRange as EXCEL.Range
LOCAL loPivot as Excel.PivotTable
LOCAL lcFileName

lcFileName = "MyTestFile.XLS"

loExcel = CREATEOBJECT("Excel.Application")
IF VARTYPE(loExcel) != "O"
	MESSAGEBOX("Could not instantiate Excel!", ;
		MB_ICONSTOP, "Error")
	RETURN .f.
endif

loExcel.Application.WindowState = xlMaximized	&& -4137
loWorkBook = loExcel.Workbooks.Open(lcFileName)

* rename the sheet to work with
loActiveSheet = loExcel.ActiveSheet
loActiveSheet.Name = "Data"

* create pivot table in existing workbook, then
* delete original detail data sheet and save the pivot only
with loExcel
	.Range("A1").Select
	.ActiveCell.FormulaR1C1 = "Tran Date"
	.Range("B1").Select
	.ActiveCell.FormulaR1C1 = "Country"
	.Range("C1").Select
	.ActiveCell.FormulaR1C1 = "Code"
	.Range("D1").Select
	.ActiveCell.FormulaR1C1 = "Trans"
	.Range("A1").Select
endwith

* find last used cell
lcLastCell = ALLTRIM(STR(lnLstRow))
loActiveSheet.Range("A1:D" + lcLastCell).Columns.AutoFit()

* make it of the form: "R999C4" (we always have 4 columns here)
lcRCLastCell = "R" + lcLastCell + "C4"

loPivot = loExcel.ActiveSheet.PivotTableWizard(xlDatabase, ;
        "Data!R1C1:" + lcRCLastCell, "", "Transactions", .T., .T.)
loPivot.SaveData = .F.
loPivot.AddFields("Tran Date", "Country", "Code")
loPivot.PivotFields("Trans").Orientation = xlDataField

* rename the sheet to yesterday's date
* we use yesterday as today's data has not been received yet
* (change "/" to "-" as slash is not legal in sheet names)
LOCAL lcDataSheetName
loActiveSheet = loWorkBook.Sheets("Sheet1")
lcDataSheetName = strtran(dtoc(date()-1),"/","-")
loActiveSheet.Name = lcDataSheetName

* Start formatting

* set Excel Print Area
lcLastCell = loExcel.ActiveCell.SpecialCells(xlLastCell).Address()
loActiveSheet.PageSetup.PrintArea = "$A$1:" + lcLastCell

lcBetween = " between " + DTOC(ldStart) + " and " + DTOC(ldEnd)
lcMsg = "Weekly transaction stats " + lcBetween

* go to bottom and skip two rows for a message
loExcel.Range(lcLastCell).Select
loRange = loExcel.ActiveCell

* extract column number from letter and subtract 65: CHR("A")
n = ASC(SUBSTR(lcLastCell,2,1)) - 65

* move 2 down, and left to first column
loRange = loRange.Offset(2,-n)

loExcel.Range("A" + ALLTRIM(STR(loRange.Row))).Value = lcMsg

* define printed page header/footer
With loActiveSheet.PageSetup
	*.LeftHeader   = ""
	.CenterHeader = "Transactions " + lcBetween
	*.RightHeader  = ""
	.LeftFooter   = "&BConfidential&B"
	.CenterFooter = "&D"
	.RightFooter  = "Page &P"
	.PrintGridlines = .F.
	.CenterHorizontally = .T.
	.CenterVertically = .F.
	*.Orientation = xlPortrait
endwith

* SaveAs in latest XLS format
lcFileName = "T" + DTOS(DATE()-1) + ".XLS"
lnFileFormat = xlNormal
lcPassword = ""
lcWriteResPassword = ""
llReadOnlyRecommended = .F.
llCreateBackup = .F.
loWorkbook.SaveAs(lcFileName, lnFileFormat, lcPassword, ;
	lcWriteResPassword, llReadOnlyRecommended, llCreateBackup)
* etc. etc.

HTH


Alex Feldstein, MCP, Microsoft MVP
VFP Tips: English - Spanish
Website - Blog - Photo Gallery


"Once again, we come to the Holiday Season, a deeply religious time that each of us observes, in his own way, by going to the mall of his choice." -- Dave Barry
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform