TReport

Source: source/classes/report.prg

TReport is FiveWin's high-level report engine. It automates the creation of tabular reports from DBF files or any data source, with support for column headers, grouping, subtotals, grand totals, page numbering, shadows, grid lines, and export to Excel. TReport uses TPrinter internally for output.

Report Processing Flow

flowchart TD A[Init] --> B[bInit Block] B --> C[StartPage] C --> D{Check Groups} D -->|New Group| E[StartGroup
Print Group Header] D -->|Same Group| F[Print Column Data] E --> F F --> G[EndLine / Skip] G --> H{End of Data?} H -->|No| I{Page Full?} I -->|Yes| J[EndPage / StartPage] I -->|No| D J --> D H -->|Yes| K[EndGroup
Print Totals] K --> L[Grand Total Line] L --> M[EndPage] M --> N[bEnd Block] N --> O[Preview / Print]

Key DATA Members

DATATypeDescription
oDeviceTPrinterOutput printer device
aGroupsArrayArray of TRGroup objects (grouping levels)
aColumnsArrayArray of TRColumn objects (report columns)
aFontArrayArray of TFont objects used in the report
aPenArrayArray of TPen objects for lines/grid
bForBlockFOR condition (filter records)
bWhileBlockWHILE condition (stop condition)
bInitBlockExecuted before report starts
bEndBlockExecuted after report ends
bSkipBlockCustom skip block (default: DBSKIP())
bStartPageBlockExecuted at the start of each page
bEndPageBlockExecuted at the end of each page
bStartLineBlockExecuted before each data line
bEndLineBlockExecuted after each data line
bStartGroupBlockExecuted when a new group starts
bEndGroupBlockExecuted when a group ends
nRowNumericCurrent row position on page
nPageNumericCurrent page number
nMarginNumericLeft margin
nBottomRowNumericBottom margin row (triggers new page)
nStdLineHeightNumericStandard line height in pixels
lSummaryLogicalSummary-only mode (no detail lines)
lTotalLogicalShow grand totals
lShadowLogicalPrint shadows under totals
lGridLogicalPrint grid lines
nRptAlignNumericReport alignment (RPT_LEFT, RPT_CENTER, RPT_RIGHT)

Key Methods

MethodDescription
New( aTitle, aHead, aFoot, aFont, aPen, lSummary, ... )Create the report
Activate( bFor, bWhile, bInit, bEnd, ... )Run the report
AddColumn( oColumn )Add a column to the report
DelColumn( nColumn )Remove a column
InsColumn( oColumn, nPos )Insert a column at a specific position
AddGroup( oGroup )Add a grouping level
Say( nCol, xText, nFont, nPad, nRow )Print text in a column
SayBitmap( nRow, nCol, cBitmap, nWidth, nHeight, ... )Print an image
Box( nRow, nCol, nBottom, nRight, nPen, nScale )Draw a box
Line( nRow, nCol, nBottom, nRight, nPen, nScale )Draw a line
TotalLine( nType, nGrid )Print total/subtotal line
NewLine( nHeight )Advance to next line
BackLine( nLine )Move back by n lines
NeedNewPage()Check if current row exceeds bottom margin
Margin( nValue, nType, nScale )Set margins (left, right, top, bottom)
CellView()Enable cell-view mode (grid around every cell)
ToExcel( bProgress )Export report to Excel

Report Commands

REPORT oRpt ;
   [ TITLE cTitle1, cTitle2, ... ] ;
   [ HEADER cHead1, cHead2, ... ] ;
   [ FOOTER cFoot1, cFoot2, ... ] ;
   [ FONT oFont1, oFont2, ... ] ;
   [ PEN oPen1, oPen2, ... ] ;
   [ SUMMARY ] ;
   [ PREVIEW ] ;
   [ TO PRINTER ] ;
   [ TO FILE cFile ] ;
   [ NAME cReportName ]

COLUMN [ TITLE cTitle ] ;
   [ DATA bData ] ;
   [ SIZE nWidth ] ;
   [ PICTURE cPicture ] ;
   [ FONT nFontIndex ] ;
   [ LEFT | CENTER | RIGHT ] ;
   [ TOTAL ] ;
   [ SHADOW ] ;
   [ GRID ]

GROUP [ ON bGroupExpr ] ;
   [ HEADER cHeader ] ;
   [ FOOTER cFooter ] ;
   [ FONT nFontIndex ] ;
   [ EJECT ]

END REPORT

Example: Simple List Report

#include "FiveWin.ch"

function Main()

   local oRpt, oFont1, oFont2

   USE customers

   DEFINE FONT oFont1 NAME "Arial" SIZE 0, -14
   DEFINE FONT oFont2 NAME "Arial" SIZE 0, -10

   REPORT oRpt TITLE "Customer List" ;
      FONT oFont1, oFont2 PREVIEW

   COLUMN TITLE "Name"    DATA customers->name    SIZE 30
   COLUMN TITLE "City"    DATA customers->city    SIZE 20
   COLUMN TITLE "State"   DATA customers->state   SIZE  5
   COLUMN TITLE "Phone"   DATA customers->phone   SIZE 15
   COLUMN TITLE "Balance" DATA customers->balance SIZE 12 ;
      PICTURE "999,999.99" TOTAL

   END REPORT

   oFont1:End()
   oFont2:End()

   USE

return nil

Example: Grouped Report with Subtotals

#include "FiveWin.ch"

function Main()

   local oRpt, oFont1, oFont2, oFont3

   USE invoices INDEX invoices

   SET ORDER TO TAG "CUSTOMER"

   DEFINE FONT oFont1 NAME "Arial Bold" SIZE 0, -16
   DEFINE FONT oFont2 NAME "Arial"      SIZE 0, -10
   DEFINE FONT oFont3 NAME "Arial Bold" SIZE 0, -11

   REPORT oRpt ;
      TITLE "Invoice Report by Customer" ;
      HEADER "Period: " + DToC( Date() - 365 ) + " to " + DToC( Date() ) ;
      FOOTER "Page: " + Str( oRpt:nPage, 3 ) ;
      FONT oFont1, oFont2, oFont3 ;
      PREVIEW

   // Group by customer
   GROUP ON invoices->custid ;
      HEADER "Customer: " + invoices->custname ;
      FOOTER "Subtotal" ;
      FONT 3 ;
      EJECT

   COLUMN TITLE "Invoice #"  DATA invoices->invnum    SIZE 12
   COLUMN TITLE "Date"       DATA invoices->invdate   SIZE 10
   COLUMN TITLE "Description" DATA invoices->descript SIZE 30
   COLUMN TITLE "Amount"     DATA invoices->amount    SIZE 12 ;
      PICTURE "999,999.99" TOTAL

   END REPORT

   oFont1:End()
   oFont2:End()
   oFont3:End()

   USE

return nil

Example: Custom Report with Events

#include "FiveWin.ch"

function Main()

   local oRpt, oFont1, oFont2
   local nTotal := 0

   USE products INDEX products

   DEFINE FONT oFont1 NAME "Calibri" SIZE 0, -14
   DEFINE FONT oFont2 NAME "Calibri" SIZE 0, -10

   REPORT oRpt TITLE "Product Catalog" ;
      FONT oFont1, oFont2 PREVIEW

   COLUMN TITLE "Code"     DATA products->code     SIZE 10
   COLUMN TITLE "Product"  DATA products->name     SIZE 30
   COLUMN TITLE "Category" DATA products->category SIZE 15
   COLUMN TITLE "Price"    DATA products->price    SIZE 12 ;
      PICTURE "999,999.99" TOTAL
   COLUMN TITLE "Stock"    DATA products->stock    SIZE  8 TOTAL

   END REPORT

   // Custom activation with event blocks
   oRpt:Activate( ;
      { || products->active },                ; // bFor: only active products
      ,                                        ; // bWhile
      { || oRpt:Margin( 1, 1, 1 ) },         ; // bInit
      { || MsgInfo( "Report completed" ) },   ; // bEnd
      { || oRpt:Say( 1, "Page " + ;
         LTrim( Str( oRpt:nPage ) ), 2 ) },  ; // bStartPage
      ,                                        ; // bEndPage
      ,                                        ; // bStartGroup
      )                                          // bEndGroup

   oFont1:End()
   oFont2:End()

   USE

return nil

Example: Export to Excel

#include "FiveWin.ch"

function Main()

   local oRpt

   USE customers

   REPORT oRpt TITLE "Customers" PREVIEW

   COLUMN TITLE "Name"    DATA customers->name    SIZE 30
   COLUMN TITLE "Email"   DATA customers->email   SIZE 30
   COLUMN TITLE "Balance" DATA customers->balance SIZE 12 ;
      PICTURE "999,999.99" TOTAL

   END REPORT

   // Export to Excel after preview
   oRpt:ToExcel( { |n| SysRefresh() } )

   USE

return nil

Tips

See Also