Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Excel Sub Total
Message
From
08/01/2007 08:17:23
Cetin Basoz
Engineerica Inc.
Izmir, Turkey
 
 
To
07/01/2007 23:49:44
General information
Forum:
Visual FoxPro
Category:
Other
Title:
Miscellaneous
Thread ID:
01183355
Message ID:
01183397
Views:
26
>Dear Experts
>
>
>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)
>
>
>By the following method all data goes to Excel properly.
>
>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
>
>---date1----Sno--Cash--Weight
>01/07/2007---1---100-----10
>01/07/2007---2---100-----20
>01/07/2007---3---300-----30
>Sub Total----------500-----60
>02/07/2007---4---200-----40
>02/07/2007---5---400-----50
>02/07/2007---7---300-----70
>Sub Total----------900-----160
>03/07/2007---1---100-----70
>Sub Total----------100-----70
>
>Plaese help

Tariq,
You can use Subtotal() function on a range. ie:
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
endfunc
Cetin
Çetin Basöz

The way to Go
Flutter - For mobile, web and desktop.
World's most advanced open source relational database.
.Net for foxheads - Blog (main)
FoxSharp - Blog (mirror)
Welcome to FoxyClasses

LinqPad - C#,VB,F#,SQL,eSQL ... scratchpad
Previous
Reply
Map
View

Click here to load this message in the networking platform