Chapter 5 Reporting with XLReportGen

Creating and Opening XRF Files

About XRF files

To generate a report with XLReportGen, you must create an XRF file with an .xrf extension. The XRF file contains information such as the name of the report template file, the name of the report file, log file name, data sources, parameters and functions. The XRF file tells XLReportGen how to get data from data sources and how to put data into a report.

Create a new XRF file

On the File menu, click New.

Open an XRF file

1. On the File menu, click Open.

2. In the Look in list, click the drive, folder, or Internet location that contains the file you want to open.

3. In the folder list, locate and open the folder that contains the file.

4. Click the file, and then press Open button.

Save an XRF file

On the File menu, click Save. If you're saving the file for the first time, you'll be asked to give it a name.

If you want save a file to another name, do as follows:

1. On the File menu, click Save As.

2. In the File name box, enter a new name for the file.

3. Press Save button.

Configuring Files

About files

You should specify the report template file, report file, report file type and log file. The report template file defines layouts, formats and styles of the report. The report file is the report you want to generate. The type of the report file can be different from the template file. The log file records the log information in the report generating.

The file path can be a relative path or an absolute path. If it is a relative path, the base path is the path of the XRF file. In the paths and names of report file, template file and log file, you can use parameters. For detailed information about parameters, refer to "Configuring Parameters" in this document.

Configuring file information

1. On the Report menu, click Configuration. The Configuration dialog box appears.

2. Click the File tab.

3. Input the path and name of the template file, the report file and the log file into their text box.

4. In the File Type box, click the file type you want. If the file type of the report is same as the template file, click the (Default) in the File Type box.

5. If you want to protect the report, select the Protect Report check box. If the check box is selected, the Excel report generated is protected, and can not be modified. If you select Random Password option button, a random password will be created to protect the report. If you select Input Password option button, you can input a password to protect the report.

6. Press OK button to confirm the changes, press Cancel button to discard the changes.

Converting files

You can convert a file from Microsoft Excel to and from another file format. For example, the template file is a Lotus 1-2-3 file with a .wk3 extension, and the report file is a HTML file with a .htm extension. For most file formats, Microsoft Excel converts only the active sheet. To convert the other sheets, open the template file, switch to the sheet you want to save, and save it.

The file formats XLReportGen supports can be one of these. What file format XLReportGen supports is dependent on your Microsoft Excel. For example, Microsoft Excel 2003 supports XML, but Microsoft Excel 97/2000 does not support it. For more information about converting files, please refer to Microsoft Excel Help. The file "xconv.cfg" located in the XLReportGen directory contains the information of file formats. You can expand it if your Microsoft Excel supports more file formats.

File Format NameValueDescriptionExtensionConverter
xlWorkbookNormal-4143Microsoft Excel Workbook xlsOffice97
xlTemplate17TemplatexltOffice97
xlAddIn18Microsoft Excel Add-InxlaOffice97
xlHtml44HTMLhtm htmlOffice2000
xlWebArchive45Web Archivemht mhtmlOffice2003
xlXMLSpreadsheet46XML SpreadsheetxmlOffice2003
xlCSV6CSV (comma delimited)csvOffice97
xlCSVMac22CSV (comma delimited) (Macintosh)csvOffice97
xlCSVMSDOS24CSV (comma delimited) (MS-DOS)csvOffice97
xlCSVWindows23CSV (comma delimited) (Windows)csvOffice97
xlCurrentPlatformText-4158Text (Tab-delimited)txtOffice97
xlTextMac19Text (Tab-delimited) (Macintosh)txtOffice97
xlTextMSDOS21Text (Tab-delimited) (MS-DOS)txtOffice97
xlTextPrinter36Formatted Text (Space-delimited)prnOffice97
xlTextWindows20Text (Tab-delimited) (Windows)txtOffice97
xlUnicodeText42Unicode TexttxtOffice2000
xlExcel216Microsoft Excel 2.0 WorksheetxlsOffice97
xlExcel2FarEast27Microsoft Excel 2.0 Worksheet Far EastxlsOffice97
xlExcel329Microsoft Excel 3.0 WorksheetxlsOffice97
xlExcel433Microsoft Excel 4.0 WorksheetxlsOffice97
xlExcel4Workbook35Microsoft Excel 4.0 Workbook xlwOffice97
xlExcel539Microsoft Excel 5.0/95 WorkbookxlwOffice97
xlExcel979543Microsoft Excel 97-2003 & 5.0/95 WorkbookxlsOffice2000
xlDBF27DBF 2 (dBASE II)dbfOffice97
xlDBF38DBF 3 (dBASE III)dbfOffice97
xlDBF411DBF 4 (dBASE IV)dbfOffice97
xlDIF9DIF (data interchange format)difOffice97
xlSYLK2SYLK (symbolic link format)slkOffice97
xlWJ2WD114WD1 (1-2-3)wd1Office97
xlWK15WK1 (1-2-3)wk1Office97
xlWK1ALL31WK1, ALL (1-2-3)wk1Office97
xlWK1FMT30WK1, FMT (1-2-3)wk1Office97
xlWK315WK3 (1-2-3)wk3Office97
xlWK3FM332WK3, FM3 (1-2-3)wk3Office97
xlWK438WK4 (1-2-3)wk4Office97
xlWKS4WKS (Works)wksOffice97
xlWorks2FarEast28Works Far EastwksOffice97
xlWQ134WQ1 (Quattro Pro/DOS)wq1Office97

Note: Some of these file formats may not be available to you, depending on the language support (U.S. English, for example) that you've selected or installed.

Configuring Data Sources

About data sources

A data source identifies a database computer you want to access. Because of accessing data through ODBC, XLReportGen can access a wide range of data sources, such as Oracle, DB2, Sybase, Informix, Microsoft SQL Server, Teradata, MySQL, Microsoft Access, dBase. XLReportGen supports more than one data sources in one report. You can get data from some different databases such as Oracle, DB2 and Microsoft SQL Server, and put them into one report.

You can define a connection to a data source using an ODBC data source name or a connection string. If you use an ODBC data source name to make a connection, you should specify a user name and a password. If you use a connection string to make a connection, you also should specify a data source name that you can reference in functions.

Adding, modifying and deleting a data source

1. On the Report menu, click Configuration. The Configuration dialog box appears.

2. Click the Data Source tab.

3. If you want to add a data source, press New button, the New Data Source dialog box appears.

4. If you want to modify a data source, click the data source name in the Data Source list box, and press Edit button, the Edit Data Source dialog box appears.

5. If you want to delete a data source, click the data source name in the Data Source list box, and press Delete button, the confirmation dialog box appears. Press Yes button to delete the data source.

6. You can test a data source. Click the data source name in the Data Source list box, and Press Test button to display the information of connection to the data source.

7. Select or clear the Encrypt Password check box. If the check box is selected, passwords will be saved in an encrypted format. Or passwords will be saved in plain text.

8. Press OK button to confirm the changes, press Cancel button to discard the changes.

Configuring Parameters

About parameter

You can use parameters in SQL statements. These values need to be provided to XLReportGen before it executes these SQL statements. To use a parameter, you must declare it first. When XLReportGen generate a report, it will prompt you to input the value of the parameter. XLReportGen will replace the parameter name in the SQL statements with the actual value before it submits the SQL statements to data sources.

A parameter has a name, a title and a default value. The name of a parameter identifies the parameter. You can use the names in SQL statements. The titles will be displayed in the prompt dialog box when XLReportGen is run.

Note: XLReportGen will replace all strings that are the same as the names of the parameters. You should be careful to define a unique name for each parameter. It is a good choice a name begins with the "$" character. For example, you give the name "$ReportDate" for a parameter. Parameters are case-sensitive.

Adding, modifying and deleting a parameter

1. On the Report menu, click Configuration. The Configuration dialog box appears.

2. Click the Parameter tab.

3. If you want to add a parameter, press New button, the New Parameter dialog box appears. Input parameter name, parameter title and default value, press OK button.

4. If you want to modify a parameter, click the parameter name in the Parameter list box, and press Edit button, the Edit Parameter dialog box appears. Change the name, title and default value of the parameter, press OK button.

5. If you want to delete a parameter, click the parameter name in the Parameter list box, and press Delete button, the confirmation dialog box appears. Press Yes button to delete the parameter.

6. Press OK button to confirm the changes, press Cancel button to discard the changes.

Inputting Functions

You should input functions in the editor window. A function includes a SQL statement and some arguments. XLReportGen executes the SQL statement, and determines whether or how to add data into the report. XLReportGen sequentially executes functions.

Each function is begin with the "@" character. Syntax:

@functionno=functionname(arguments)
sqlstatement

The functionno is a label of the report function.

The functionname represents a report function.

The arguments for a function define various properties for the function. For example, the "sheet" argument identifies a worksheet in the Excel workbook. An argument takes the form Name="Value". The argument value can be delimited by single or double quotes.

The sqlstatement is a SQL statement.

For more detailed information about functions, see "Function Reference" in this document.

You can use comments in text. A comment is the "/*" characters, followed by any sequence of characters (including new lines), followed by the "*/" characters. You cannot nest comments.

Running XRF Files

You can run an XRF file to generate a report in Microsoft Excel spreadsheet format. XLReportGen supports Windows mode and command line mode.

Windows mode

1. On the Report menu, click Run, the Run Report dialog box appears.

2. If you want to display the generated report, select the Display Report with Microsoft Excel check box.

3. Press Start button to run the XRF file.

4. If parameters are defined in the XRF file, XLReportGen will pop up a prompt dialog box. Input the values of the parameters, and press OK button.

5. While XLReportGen is being run, it will display some information such as status, SQL count, error count, function No., records count and log information.

6. You can interrupt the running. Click End button to interrupt it. XLReportGen will immediately save and close the report.

7. Click Close button after completion.

8. If you want to open the report, click Open Report File on the File menu.

9. If you want to check the log, click Open Log File on the File menu.

Command line mode

You can run an XRF file in command line. You have defined two parameters in the XRF file "myreport.xrf". The first parameter is sales date "$SalesDate", and the second is the category of the products "$Category". You can run XLReportGen in command line mode as follows:

excelreport c:\excelreport\myreport.xrf -c 1996-05-01 "Dairy Products"

XLReportGen will replace "$SalesDate" in SQL statements with "1996-05-01", replace "$Category" with "Dairy Products", and then submit SQL statements to data sources.

Using Excel Formulas

Formulas are equations that perform calculations on values in your worksheet. A formula starts with an equal sign (=). For example, the following formula multiplies 2 by 3 and then adds 5 to the result.

=5+2*3

A formula can also contain any or all of the following: functions, references, operators, and constants. For more detail information about formulas, functions and references, refer to Microsoft Excel Help. In a report template file, you can use all kind of Microsoft Excel formulas. And then all formulas in the report template file will be brought to the final report file.

Example

Show Unit Price, Quantity, Discount and Amount. The Amount will be changed if an end user changes Unit Price, Quantity or Discount.

You can use a fomula to show Amount.

1. Create a template file as follows, and define the formula "=C2*D2*(1-E2)" in cell F2. You must use relative reference.

Excel Report Template - Using Formula

2. Write report function as follow, and use COPYRANGE to copy the formula to the next cell. For the first record, XLReportGen will directly put data into row 2. For the other record, it will put data after copy row 2. So the formula in cell F2 will copy to cell F3, F4... and Microsoft Excel will automatically change the formula to "=C3*D3*(1-E3)" ...

@F1=Report(sheet="Sheet1" cell=A2 copyrange=2:2)
SELECT c.CompanyName AS Customer
,p.ProductName
,d.Quantity
,d.UnitPrice
,d.Discount
FROM Orders o
, Customers c
, OrderDetails d
, Products p
WHERE o.CustomerID = c.CustomerID
AND o.OrderID = d.OrderID
AND d.ProductID = p.ProductID
AND YEAR(o.OrderDate) = YEAR('$ReportMonth-01')
AND MONTH(o.OrderDate) = MONTH('$ReportMonth-01')
ORDER BY 1, 2
;

3. The report file generated by XLReportGen is as follows.

Excel Report Sample - Using Formula

Example

Add totals such as Total Quantity, Total Amunt.

You can use statistical functions of Microsoft Excel, such as SUM.

1. Create a template file as follows, define the formula of total quantity as "=SUM(C7:C8)" in cell C9, and the formula of total amount as "=SUM(D7:D8)" in cell D9. You must use relative reference.

Excel Report Template - Adding Total

2. Write report function as follow. When XLReportGen insert some rows according to the records, Microsoft Excel will automatically change the formulas.

@F2=REPORT(sheet="Report2" type=var cell=B7 reserve=2)
SELECT c.CategoryName, SUM(d.Quantity), Sum(d.UnitPrice * d.Quantity * (1-d.Discount))
FROM Orders o
	,OrderDetails d
	,Products p
	,Categories c
WHERE o.OrderID = d.OrderID
AND d.ProductID = p.ProductID
AND p.CategoryID = c.CategoryID
AND YEAR(o.OrderDate) = YEAR('$ReportMonth-01')
AND MONTH(o.OrderDate) = MONTH('$ReportMonth-01')
GROUP BY c.CategoryName
ORDER BY c.CategoryName
;

3. The report generated by XLReportGen is as follows. The formula of total quantity is changed to "=SUM(C7:C14)", and the formula of total amount is changed to "=SUM(D7:D14)".

Excel Report Sample - Adding Total

Sorting, Grouping and Totaling

Sorting data

Sorting means placing data in some kind of order to help you find and evaluate it. For example, you may want to have a customer list sorted alphabetically by name or by country.

To sort your data, you can use SQL. Use the ORDER BY clause to have your results displayed in a sorted order.

SELECT EmployeeID
,LastName
,FirstName
,HireDate
FROM Employees
ORDER BY HireDate;	/* ascending sort */

In the example above, results will come back in ascending order by hire date. To explicitly specify ascending or descending order, add ASC or DESC, to the end of your ORDER BY clause. The following is an example of a descending order sort.

ORDER BY HireDate DESC;	/* descending sort */

Totaling

You can sum the values, count all the values or only those values that are distinct from one another, and determine the maximum, minimum, average. To add totals, there are two ways.

1. You can add total using statistical functions of Microsoft Excel, such as SUM. For more detail information, refer to "Using Excel Formulas" in this document.

2. You can use aggregate functions in SQL statement, such as COUNT, SUM, AVG, MAX, MIN.

  1. In the fixed table report, you can add total directly using a separate SQL.
  2. In the variable table report, you must add total first using a Fixed Table report function before you use the Variable Table report function. Because the cell address of the total field will change after you use Variable Table report function.

Grouping data and subreports

Grouped data is data that is sorted and broken up into meaningful groups. In a customer list, for example, a group might consist of all those customers living in the same Region.

To group data in a report, you should use GROUP VARIABLE TABLE REPORT function. For more detail information, refer to "Group Variable Table Report" in this document.

Using GROUP REPORT function, you can make subreports within a report. A subreport would typically be used to perform one-to-many lookups such as Customer / Order / OrderDetails.

To make sub reports within the main report,

  1. Write a JOIN SQL statement to acccess data from two or more tables. For example, you can join Customers, Orders and OrderDetails tables.
  2. Use GROUP VARIABLE TABLE REPORT function.

For more detail information, refer to the samples invoice.xrf, product_catalog.xrf and sales_detail.xrf within XLReportGen.

Subtotaling

A subtotal is a summary that totals or sums numeric values in a group. You can sum the values in each group, count all the values in each group, and determine the maximum, minimum, average in each group. For example, determine the total sales per sales representative in a sales reports.

To add subtotals, you can use the functions of Microsoft Excel or aggregate functions in SQL statement.

1. You can add sub-totals using statistical functions of Microsoft Excel, such as SUM.

  1. The range of SUM function should contain cells for detail records in the report template file.
  2. The range of SUM function must contain at least one row/column that is not included in the range for the detail records. For example, the row 13 is for detail record, you should add blank row 14, and the function is written as SUM(H13:H14). If you do not want to show the blank row in the report, you can hide the row.
  3. You should use relative references. For example, SUM(H13:H14). Microsoft Excel will change the function automatically as XLReportGen adds some rows in the report.
Microsoft Excel will change the function automatically as XLReportGen adds some rows in the report.

2. If you want to have total and sub-totals, you can add total using SUMIF function. For example, SUMIF(G:G,"Subtotal:",H1:H16).

  1. You can add total using SUMIF function. The range of SUMIF function must contain one row/column that is not included in the range of the group. For example, the range of the group is rows 1:15, you should add blank row 16, and write the function as SUMIF(G:G,"Subtotal:",H1:H16). You can hide the blank row.
  2. You can add total using aggregate function in SQL statement. You must add total first using a Fixed Table report function before you use the Variable Table report function. Because the cell address of the total field will change after you use Variable Table report function.

3. You can add sub-totals using aggregate function in SQL statement too.

  1. Use aggregate function and GROUP BY clause, get summary data for each group, and insert results into a temporary table.
  2. If you have different kinds of summaries, repeat the step 1, and insert results into another temporary table.
  3. Use group report function, and join the detail data and the summary data using JOIN. The summary fields must be included in the group list.

For more detail information, please refer to samples invoice.xrf, sales_detail.xrf within XLReportGen.

Charting

Charts are visually appealing and make it easy for users to see comparisons, patterns, and trends in data. You can use Microsoft Excel to add sophisticated, colorful charts in your reports. For example, you can see at a glance whether sales are falling or rising over quarterly periods, or how the actual sales compare to the projected sales.

To create a chart in a report, you should create the chart in the template file. You can create a chart on its own sheet or as an embedded object on a worksheet. For more detail information how to create chart, refer to Microsoft Excel Help.

To create a chart in the report template file, you can use some sample data. Using sample data, you can set the various chart options. After you have made the report template, you delete the sample data. When you generate the report, XLReportGen will put data into the report, and you get the chart. For more detail information about charting, refer to the sample monthly_sales.xrf within XLReportGen.

Example

The following function provides data for the chart: Sales by Categories.

@F2=REPORT(sheet="Report2" type=var cell=B7 reserve=2)
SELECT c.CategoryName
, SUM(d.Quantity)
, Sum(d.UnitPrice * d.Quantity * (1-d.Discount))
FROM Orders o
	,OrderDetails d
	,Products p
	,Categories c
WHERE o.OrderID = d.OrderID
AND d.ProductID = p.ProductID
AND p.CategoryID = c.CategoryID
AND YEAR(o.OrderDate) = YEAR('$ReportMonth-01')
AND MONTH(o.OrderDate) = MONTH('$ReportMonth-01')
GROUP BY c.CategoryName
ORDER BY c.CategoryName

Result

The worksheet of the chart defined in the report template:

Excel Report Template - Charting

The chart defined in the report template is a blank chart.

The worksheet of the chart generated in the report:

Excel Report Sample - Charting

The chart generated in the report:

Excel Report Chart - Charting

Pictures

Inserting pictures into a report template

To make eye-catching reports, you can add pictures to your reports. You can insert pictures into the report template directly in Microsoft Excel. For example, you want to display a logo in your report. You can insert the logo graphics file into the report template. For more information about adding pictures to worksheets, refer to Microsoft Excel Help.

Inserting pictures into a report

Except for inserting the static pictures during report design, you want to insert pictures during report buliding process. You hope a reporting tool to pull pictures from database into Excel report. XLReportGen can insert pictures from the graphics files, and support all graphics file format that Microsoft Excel support.

To insert pictures into a report using XLReportGen, you should do as follows:

1. Store the path and name of the graphics files in the database

You stored the path and file name of the pictures in database, did not store the pictures. The file path can be a relative path, an absolute path or a URL. For example, you store "images\emp1.jpg" in Photo field.

2. Identify the image fields in the report function

Write a report function in the XRF file, and identify the image fields using IMAGE argument. For example,

@F1=Report(sheet="Employee Profile" ... image=photo)

3. Specify the positioning option and size in the report template

To specify the positioning option and size, you should write a formatting expression into the cell in the report template file. XLReportGen will get the text of the cell, and insert a picture into the cell according to the instruction in the format expression.

4. Run XLReportGen to generate report with pictures

During report generating process, XLReportGen will read the graphics files, and insert them into the report according to your instruction. If the path and file name of the picture is "", XLReportGen will return "". XLReportGen will return "#Error" if it does not find the file of the picture.

For more detail information about pictures, refer to the samples employee_profile.xrf, product_catalog.xrf within XLReportGen.

Using Parameters

To use a parameter, you must define it first. If you have defined a parameter name, you can use it in SQL statements. When XLReportGen is run, it will replace the parameter name in the SQL statements with the actual value before it submits the SQL statements to data sources. Besides in SQL statements, you can use parameters in the paths and names of report file and log file.

In fact, XLReportGen will replace all strings that are the same as the names of the parameters. You should be careful to define a unique name for each parameter. It is a good choice a name begins with the "$" character.

Example

Input an order id to get the order information. The field OrderID is numeric type.

1. Defining a parameter

Define a parameter as follows:

Name: $OrderID
Title: Order ID (>=10248)
Default: 10360

2. Using a parameter

You can use the parameter "$OrderID" in SQL statements. For example:

SELECT o.OrderID
,o.OrderDate
,SUM(d.UnitPrice * d.Quantity * (1-d.Discount)) AS Amount
FROM Orders o, OrderDetails d
WHERE o.OrderID = d.OrderID
AND o.OrderID = $OrderID
GROUP BY o.OrderID, o.OrderDate
;

Example

Define two parameters. The first parameter is sales date, and the second is the category of the products. The field OrderDate is date type, and CategoryName is char type.

1. Defining parameters

Define parameters as follows:

Name1: $SalesDate
Title1: Sales Date
Default1: 1996-05-01
Name2: $Category
Title2: Category of Products
Default2: 

2. Using parameters

You can use the parameters "$SalesDate", "$Category" in SQL statements. For example:

SELECT ......
FROM Orders, OrderDetails, Products, Categories
WHERE ......
AND OrderDate = '$SalesDate'
AND CategoryName LIKE '$Category%'
;
/* For MS Jet SQL, LIKE '$Category*' */

Example

Get the information from the database, table and column that you identify when the report is generated.

1. Defining parameters

Define parameters as follows:

Name1: $Database
Title1: Database Name
Default1:
Name2: $Table
Title2: Table Name
Default2:
Name3: $Column
Title3: Column Name
Default3:

2. Using parameters

You can use the parameters "$Database", "$Table" and "$Column" in SQL statements. For example:

USE $Database;
or
DATABASE $Database;
SELECT $Column
FROM $Table
;

Example

Use parameters in the path and name of the report file and log file.

1. Defining a parameter

Define a parameter as follows:

Name: $CustomerID
Title: Customer ID
Default: C000001

2. Using a parameter

ReportFileName=report\report_$CustomerID.xls
LogFileName=log\report_$CustomerID.log
or
ReportFileName=report\$CustomerID\report.xls
LogFileName=log\$CustomerID\report.log

Programming

Using add-ins, macros

In Microsoft Excel, you can automate a task with a macro. A macro is a series of commands and functions that are stored in a Microsoft Visual Basic module and can be run whenever you need to perform the task.

You can write macros in the report template file, and can use automatic macros, such as Auto_Open, Auto_Close to automate a task. For examples, you can use Auto_Open macro to make the template, or use Auto_Close to change the report after XLReportGen puts data into the report.

Add-ins, files in the XLStart directory are not loaded when Microsoft Excel is called by a program, and Auto_Open macros won't be run when you open the file from a program. XLReportGen has an option to process it.

To use the option:

  1. On the Tools menu, click Option. The Options dialog box appears.
  2. Click Excel tab, select Enable addins when Excel starts up or Enable Auto_Open macro.
  3. Press OK button.

Making XRF files programmatically

Sometimes you want to make an XRF file programmatically. You can do this because the XRF file is a text file. You can write a program to make an XRF file using C, perl or DOS shell, and then run XLReportGen to generate report. The two steps can be written into a batch file.

  1. Write a program to make the XRF file as you need.
  2. Write a batch file to call the program and XLReportGen in command line mode.

For example, you write a batch file runrpt.bat as follows. changexrf is an executable file that reads template.txt and output template.xrf. First runrpt.bat call changexrf to make the XRF file, and then call XLReportGen to generate the report.

@echo off
if "%1"=="" goto usage
goto process
:usage
echo Usage: runrpt ReportDate
echo ReportDate	Date format 'YYYY-MM-DD'
goto :EOF
:process
changexrf %1 <"template.txt" >"template.xrf"
ExcelReport "template.xrf" -C %1