>CREATE CURSOR testsub (date1 d(8),sno n(2),cash n(2),weight n(2)) >INSERT INTO testsub VALUES ({^2007/01/01},1,100,10) >INSERT INTO testsub VALUES ({^2007/01/01},2,150,20) >INSERT INTO testsub VALUES ({^2007/01/01},3,300,30) >INSERT INTO testsub VALUES ({^2007/01/02},4,200,40) >INSERT INTO testsub VALUES ({^2007/01/02},5,400,50) >INSERT INTO testsub VALUES ({^2007/01/02},6,300,60) >INSERT INTO testsub VALUES ({^2007/01/03},1,100,70) >>
>select testsub >copy to 'C:\Xls\'+alltrim("Date Wise")+".xls" type xl5 >release all like lo* >loexcel = createobject("Excel.Application") >loworkbook = loexcel.workbooks.open('C:\Xls\'+alltrim("Date Wise")+".xls") >losheet = loworkbook.sheets(1) && or pass the name of your sheet as a string! >loexcel.visible = .t. && display Excel >>Now In Excel Sheet, I want to get Sub Totals as
Select emp_id,First_name,Last_name From employee Into Cursor myEmployee Select Cust_id, o.Order_id, Product_Id, Unit_price, Quantity ; from orders o inner Join orditems oi On o.Order_id = oi.Order_id ; order By Cust_id, o.Order_id ; where Cust_id = 'ALFKI' ; into Cursor myOrders Dimension laSubtotal[2] laSubtotal[1] = 4 && Unit_price column laSubtotal[2] = 5 && Quantity column #include "xlConstants.h" oExcel = Createobject("excel.application") With oExcel .Workbooks.Add .Visible = .T. * Place employee data at top and name the range as 'Employees' PasteAtLocation(oExcel, 'myEmployee', 'A1', 'Employees') * Place orders data 2 rows below Employee data * and name the range as 'Orders' PasteAtLocation(oExcel, 'myOrders',GetRangeAfterSpecifiedRange(oExcel,'Employees'),'Orders') *!* Subtotal grouping by customer then by order With .ActiveWorkBook.ActiveSheet.Range('Orders') .Subtotal(1, xlSum, @laSubtotal) .Subtotal(2, xlSum, @laSubtotal,.f.,.f.,.f.) EndWith .ActiveWorkBook.ActiveSheet.Outline.ShowLevels(4) Endwith Function PasteAtLocation && Paste cursor content at given range and name the range Lparameters toExcel, tcAlias, tcRange, tcRangeName Local lcTemp,ix,lcHeaders lcTemp = Sys(2015)+'.tmp' Select (m.tcAlias) Copy To (m.lcTemp) Type Delimited With "" With Tab lcHeaders='' For ix=1 To Fcount() lcHeaders = m.lcHeaders + Iif(Empty(m.lcHeaders),'',Chr(9)) + Field(m.ix) Endfor _Cliptext = m.lcHeaders + Chr(13) + Chr(10) + Filetostr(m.lcTemp) Erase (lcTemp) toExcel.ActiveWorkBook.ActiveSheet.Range(m.tcRange).PasteSpecial() toExcel.Selection.Name = m.tcRangeName toExcel.Range('A1').Activate Endfunc Function GetRangeAfterSpecifiedRange && Get available cell after a named range - skip 2 rows Lparameters toExcel, tcRangeName Local loRange, lcRange With toExcel.ActiveWorkBook.ActiveSheet loRange = .Range(m.tcRangeName) lcRange = loRange.Offset(loRange.Rows.Count + 2,0).Resize(1,1).Address() Endwith Return lcRange endfuncCetin