A grouped sales report in a few lines.
A header per region, a line per sale and a subtotal per region, saved as PDF, XLSX and CSV. The same API in Delphi, C++ Builder and C#.
dotnet add package esegece.sgcReports
C++ Builder and C# versions
uses
Classes, SysUtils,
sgcReports_Types, sgcReports_Model, sgcReports_Data, sgcReports_Fluent;
const
CS_SALES = '[' +
'{"Region":"East","Customer":"Acme Corp","Amount":1450.00},' +
'{"Region":"East","Customer":"Blue Harbor","Amount":600.00},' +
'{"Region":"North","Customer":"Nordic Labs","Amount":2100.75},' +
'{"Region":"North","Customer":"Fjord Ltd","Amount":880.20}]';
CS_TOTAL = '[FormatFloat(''#,0.00'', Sum(Sales.Amount))]';
procedure ExportSalesReport(const aFolder: string);
var
oSales: TsgcReportJSONSource;
oBuilder: TsgcReportBuilder;
begin
oSales := TsgcReportJSONSource.Create(nil);
oBuilder := TsgcReportBuilder.Create;
try
oSales.SourceName := 'Sales';
oSales.JSON := CS_SALES;
oBuilder.Title('Sales by region').Author('eSeGeCe').Units(ruMillimeters)
.Style('Base').Font('Arial', 10).EndStyle
.Style('Group').Font('Arial', 11).Bold.BackColor($EEEEEE).EndStyle
.Page.Paper(rpsA4).Margins(15)
// one header per region, one line per sale, a subtotal per region
.GroupHeader('Sales.Region', 9).Style('Group')
.Text('Region: [Sales.Region]', 0, 1, 180, 7).TagRole(rtrHeading2).EndBand
.MasterData('Sales', 6).Style('Base')
.Text('[Sales.Customer]', 2, 0, 120, 6)
.Text('[Sales.Amount]', 130, 0, 50, 6).AlignRight.Format('#,0.00').EndBand
.GroupFooter(9).Style('Base')
.Text('Region total ' + CS_TOTAL, 80, 1, 100, 6).AlignRight.Bold.EndBand
.PageFooter(8).Style('Base')
.Text('Page [Page] of [TotalPages]', 0, 1, 180, 6).AlignCenter.EndBand
.EndPage.Data(oSales);
// accessible PDF (PDF/UA is on by default), plus the same data as Excel
oBuilder.SaveToPDF(IncludeTrailingPathDelimiter(aFolder) + 'sales.pdf');
oBuilder.SaveToXLSX(IncludeTrailingPathDelimiter(aFolder) + 'sales.xlsx');
oBuilder.SaveToCSV(IncludeTrailingPathDelimiter(aFolder) + 'sales.csv');
finally
oBuilder.Free;
oSales.Free;
end;
end;