>* Pivottable sampling for VFP3-5 - Excel97 >* Current wizard doesn't work for Excel97 >* VFP6 users probably don't need this >* This is a simple sample and dummy with regard to >* data choosen for pivot table >* Pivot tables are kind of a crosstab tables >* Data choosen is testdata!orditems >* First a cursor is constructed having order_date, prod_name, >* employee_name, customer.company and quantity >* Cursor is copied to an XL5 file - faster then using ODBC >* from Excel or writing directly to Excel with datatoclip() >* Unless data is huge >* XLS is opened >* company set as page field >* employee set as column field >* Product and Dates are set as row field to demonstrate multipl fields setting >* When you remove dates you get sum() grouped by product > >SET safety off >SET century on >#DEFINE DataPath "" > >SELECT b.emp_id ,a.company, b.order_date, d.prod_name, c.quantity ; > FROM (DataPath+"customer") a; > INNER JOIN (DataPath+"orders") b; > INNER JOIN (DataPath+"orditems") c; > INNER JOIN (DataPath+"products") d; > ON d.product_id = c.product_id ; > ON c.order_id = b.order_id ; > ON b.cust_id = a.cust_id; > order by 1,2,3 ; > into cursor myCursor ; > nofilter > >SELECT a.first_name + a.last_name as Employee, ; > b.company, b.prod_name, b.quantity, b.order_date as "dates" ; > from (DataPath+"employee") a; > INNER join myCursor b; > on a.emp_id = b.emp_id ; > into cursor pivotdata > >COPY to pivotdata type xl5 && Copied to xls file >lcRows = ltrim(str(reccount()+1)) && Including header line >lcCols = ltrim(str(fcount())) >* Pivot sample >oExcel = createobject("Excel.application") >#DEFINE xlDatabase 1 > >#DEFINE xlRowField 1 >#DEFINE xlColumnField 2 >#DEFINE xlPageField 3 >#DEFINE xlDataField 4 > >WITH oExcel > .visible = .t. > .workbooks.open(sys(5)+curdir()+"pivotdata.xls") && Open saved xls > WITH .ActiveWorkBook > .Sheets("Pivotdata").activate > .ActiveSheet.PivotTableWizard(xlDatabase, ; > "pivotdata!R1C1:R"+lcRows+"C"+lcCols,"", "PivotTable1") >&& Wizard sets Range and name > WITH .ActiveSheet.PivotTables("PivotTable1") > dimension flist[2] > flist[1]="prod_name" > flist[2]="dates" > .AddFields( @flist, "employee","company" ) && Fields added >* Add rowfields w/o an array >* .AddFields( "prod_name", "employee", "company" ) && Fields added >* .PivotFields("dates").Orientation = xlRowField >&& Dates added as second row field >* Add rowfields w/o an array > .PivotFields("quantity").Orientation = xlDataField >&& Quantity set as data field > .PivotFields("company").Subtotals(1) = .t. > .PivotFields("company").Subtotals(1) = .f. > ENDWITH > ENDWITH >ENDWITH >>Cetin