BI Publisher: How to create Multisheet in Single Excel file using Excel Template (Doc ID 1352000.1)
In this Document
Goal |
Solution |
Here are the steps required to achieve that: |
References |
Applies to:
BI Publisher (formerly XML Publisher) – Version 10.1.3.4.1 to 11.1.1.5.0 [Release 10.1 to 11.1] Information in this document applies to any platform. Checked for relevance on 05-MAR-2013
Goal
This document has been consolidated into Note 1546377.1. Please reference Note 1546377.1 for the latest updates.
How to create Excel Template in BI Publisher that allows splitting the report data into Multiple Sheets in the Same Single Excel file ?
Solution
The following example will generate a Single Excel file with Multiple Sheet, each sheet will display every Department with its allocated Employees in that department.
Attached to the Document the following sample files used in the example below:
Attachments:
1- Excel Template (EmpsbyDeptConditionalBorder.xls)
2- Sample Data (EmpDeptData.xml)
3- Data Template (EmpDept_DataTemplate.xml)
Here are the steps required to achieve that:
- First step to create the data model that will generate the data
Note: In the example data template is being used as data source, you can generate the data in other way as you need.
1- Create new Report in BI Publisher Enterprise and its Data Model is based on Data Template that extract the data from 2 tables from HR schema (Employees & Departments):
<dataTemplate name=”EMPLOYESS” description=”Test Multisheet XLS File” dataSourceRef=”HR”> <dataQuery> <sqlStatement name=”Q1″> <![CDATA[ SELECT EMPLOYEE_ID,FIRST_NAME||’ ‘||LAST_NAME EMP_NAME,EMAIL,PHONE_NUMBER,HIRE_DATE,SALARY FROM EMPLOYEES WHERE DEPARTMENT_ID = :DEPARTMENT_ID]]> </sqlStatement> <sqlStatement name=”Q2″> <![CDATA[ SELECT T1.DEPARTMENT_ID DEPARTMENT_ID, T1.DEPARTMENT_NAME DEPARTMENT_NAME, COUNT(EMPLOYEE_ID) TOTAL_EMPS, SUM(SALARY) TOTAL_SALARY, AVG(SALARY) AVG_SALARY, MAX(SALARY) MAX_SALARY, MIN(SALARY) MIN_SALARY FROM DEPARTMENTS T1, EMPLOYEES T2 WHERE T1.DEPARTMENT_ID = T2.DEPARTMENT_ID GROUP BY T1.DEPARTMENT_ID, T1.DEPARTMENT_NAME ORDER BY 1 ]]> </sqlStatement> </dataQuery> <link name=”DEPTEMP_LINK” parentQuery=”Q1″ parentColumn=”DEPARTMENT_ID” childQuery=”Q_2″ childColumn=”DEPARTMENT_ID”/> <dataStructure> <group name=”G_DEPT” source=”Q2″> <element name=”DEPARTMENT_ID” value=”DEPARTMENT_ID”/> <element name=”DEPARTMENT_NAME” value=”DEPARTMENT_NAME”/> <group name=”G_EMP” source=”Q1″> <element name=”EMPLOYEE_ID” value=”EMPLOYEE_ID”/> <element name=”EMP_NAME” value=”EMP_NAME”/> <element name=”EMAIL” value=”EMAIL”/> <element name=”PHONE_NUMBER” value=”PHONE_NUMBER”/> <element name=”HIRE_DATE” value=”HIRE_DATE”/> <element name=”SALARY” value=”SALARY”/> </group> <element name=”TOTAL_EMPS” value=”TOTAL_EMPS”/> <element name=”TOTAL_SALARY” value=”TOTAL_SALARY”/> <element name=”AVG_SALARY” value=”AVG_SALARY”/> <element name=”MAX_SALARY” value=”MAX_SALARY”/> <element name=”MIN_SALARY” value=”MIN_SALARY”/> </group> </dataStructure> </dataTemplate>
You will notice that the data template contains 2 queries:
Q1: Retrieve the employees data based on the department id returned from Q2.
Q2: Retrieve the departments data and some other data like number of employees, total salaries, average salary, minimum & maximum salary.
After that it comes the XML data structure for XML data file that will be generated and it has 2 groups:
G_DEPT: For Departments
G_EMP: For Rmployees
So the XML Data file will look like the following:
<?xml version=”1.0″ encoding=”UTF-8″?> <EMPLOYESS> <LIST_G_DEPT> <G_DEPT> <DEPARTMENT_ID>10</DEPARTMENT_ID> <DEPARTMENT_NAME>Administration</DEPARTMENT_NAME> <TOTAL_EMPS>1</TOTAL_EMPS> <TOTAL_SALARY>4400</TOTAL_SALARY> <AVG_SALARY>4400</AVG_SALARY> <MAX_SALARY>4400</MAX_SALARY> <MIN_SALARY>4400</MIN_SALARY> <LIST_G_EMP> <G_EMP> <EMPLOYEE_ID>200</EMPLOYEE_ID> <EMP_NAME>Jennifer Whalen</EMP_NAME> <EMAIL>JWHALEN</EMAIL> <PHONE_NUMBER>515.123.4444</PHONE_NUMBER> <HIRE_DATE>1987-09-17T00:00:00.000+10:00</HIRE_DATE> <SALARY>4400</SALARY> </G_EMP> </LIST_G_EMP> </G_DEPT> <G_DEPT> <DEPARTMENT_ID>20</DEPARTMENT_ID> <DEPARTMENT_NAME>Marketing</DEPARTMENT_NAME> <TOTAL_EMPS>2</TOTAL_EMPS> <TOTAL_SALARY>19000</TOTAL_SALARY> <AVG_SALARY>9500</AVG_SALARY> <MAX_SALARY>13000</MAX_SALARY> <MIN_SALARY>6000</MIN_SALARY> <LIST_G_EMP> <G_EMP> <EMPLOYEE_ID>201</EMPLOYEE_ID> <EMP_NAME>Michael Hartstein</EMP_NAME> <EMAIL>MHARTSTE</EMAIL> <PHONE_NUMBER>515.123.5555</PHONE_NUMBER> <HIRE_DATE>1996-02-17T00:00:00.000+11:00</HIRE_DATE> <SALARY>13000</SALARY> </G_EMP> <G_EMP> <EMPLOYEE_ID>202</EMPLOYEE_ID> <EMP_NAME>Pat Fay</EMP_NAME> <EMAIL>PFAY</EMAIL> <PHONE_NUMBER>603.123.6666</PHONE_NUMBER> <HIRE_DATE>1997-08-17T00:00:00.000+10:00</HIRE_DATE> <SALARY>6000</SALARY> </G_EMP> </LIST_G_EMP> …..
- The following steps are to create the EXCEL template:
2- Create a blank Excel file and save it as “Excel 97-2003 Workbook binary format (*.xls)”
3- The Template layout will be like the following (figure 1):
As in RTF template we have to define the fields and groups that will be mapped to the XML data file.
To do that in the excel template we do what we call “Assigning BI Publisher defined names” to each cell and group that will be mapped to the data. Those defined names must start with a prefix XDO_ .
The cells must be named according to the following format:
Data elements: XDO_?element_name?
where
XDO_ is the required prefix
and
?element_name? is either:
The XML tag name from your data delimited by “?” OR A unique name that you will use to map a derived value to the cell
For example: XDO_?EMPLOYEE_ID?
Data groups: XDO_GROUP_?group_name?
where
XDO_GROUP_ is the required prefix
and
?group_name? is either:
The XML tag name for the parent element in your XML data delimited by “?”. OR A unique name that you will use to define a derived grouping logic
For example: XDO_GROUP_?DEPT?
Note that the question mark delimiter, the group_name, and the element_name are case sensitive.
4- Click the cell in the Excel worksheet.
5- Click the Name box at the left end of the formula bar. The default name will display in the Name box. By default, all cells are named according to position, for example: A8.
6- In the Name box, enter the name using the XDO_ prefix and the tag name from your data. For example: XDO_?EMP_NAME?
7- Press Enter.
The following figure shows the defined name for the Employee Name field entered in the Name box:
8- Repeat for each of the following data fields: DEPARTMENT_NAME, EMPLOYEE_ID, EMAIL, PHONE_NUMBER, and SALARY.
Tips:
If you navigate out of the Name box without pressing Enter, the name you entered will not be maintained.
You cannot edit the Name box while you are editing the cell contents.
The name cannot be more than 255 characters in length.
9- After you have entered all the fields, you can review the names and make any corrections or edits using the Name Manager feature of Excel. Access the Name Manager from the Formulas tab in Excel as shown:
10- After you have named all the cells for this example, the Name Manager dialog will appear as shown:
11- Now it is time to create the group(s) for the report in the excel template: Highlight the cells that make up the group. In this example the cells are A8 – E8
12- Click the Name box at the left end of the formula bar and enter the name using the XDO_GROUP_ prefix and the tag name for the group from your data. In this example: XDO_GROUP_?EMPS?
13- Press Enter.
The following figure shows the XDO_GROUP_ defined named entered for the Employees group. Note that just the row of employee data is highlighted. Do not highlight the headers. Note also that the total cell XDO_?TOTAL_SALARY? is not highlighted
14- To define the department group, include the department name cell and all the employee fields beneath it (A5-E9) as shown in the following figure:
Enter the name for this group as: XDO_GROUP_?DEPT? to match the group in the data. Note that the XDO_?TOTAL_SALARY? cell is included in the department group to ensure it repeats at the department level.
- The following steps are very important which are for preparing and creating XDO_METADATA Sheet in the template:
15- The following figure shows the default or the initial XDO_METADATA sheet that any excel sheet should have:
16- Create a new sheet in your Excel Workbook and name it “XDO_METADATA”
17- Create the header section by entering the following variable names in column A, one per row, starting with row 1:
Version ARU-dbdrv Extractor Version Template Code Template Type Preprocess XSLT File Last Modified Date Last Modified By
18- Skip a row and enter “Data Constraints” in column A of row 10
19- In the header region, for the variable “Template Type” enter the value: TYPE_EXCEL_TEMPLATE
- Now doing the steps to splitting the Report into Multiple Sheets:
20- This example will: – Create a new worksheet for each department – Name each worksheet the name of the department with the number of employees in the department, for example: Sales-21.
21- In the XDO_METADATA sheet we need to do the following:
Use the this set of commands to define the logic to split the report data into multiple sheets:
- Use XDO_SHEET_? to define the logic by which to split the data onto a new sheet.
- Use XDO_SHEET_NAME_? to specify the naming convention for each sheet.
Column A Entry | Column B Entry | Column C Entry |
XDO_SHEET_? | <?xsl_evaluation to split the data?> Example: <?.//G_DEPT?> | n/a |
XDO_SHEET_NAME_? | <?xsl_expression to name the sheet?> Example: <?concat(.//DEPARTMENT_NAME,’-‘,count(.//EMP_NAME))?> | (Optional) <?original sheet name?> Example: <?Sheet3?> |
Now the XDO_METADATA sheet should look like the following figure:
XDO_SHEET_? must refer to an existing high-level node in your XML data. The example <?.//G_DEPT?> will create a new sheet for each occurrence of <G_DEPT> in the data.
If your data is flat you cannot use this command unless you first preprocess the data to create the desired hierarchy. To preprocess the data, define the transformation in an XSLT file, then specify this file in the Preprocess XSLT File field of the header section of the XDO _METADATA sheet.
- We are done, Now Test the Template:
22- If you have installed the Template Builder for Excel, the BI Publisher tab will appear on the ribbon menu as shown in the following figure:
23- Click Sample XML. You will be prompted to select the sample data file.
24- Click Preview.
The sample data will be applied to your template and the output document will be opened in a new workbook.
The following figure shows the generated report. Each department data now displays on its own sheet, which shows the naming convention specified:
Stay Connected with us for more BI Publisher updates through the following channels:
1- Youtube 2- Twitter 3- Linkedin 4- Facebook
If you have any questions regarding BI Publisher or would like to read more updated articles about BI Publisher from Oracle BI Publisher Experts within Oracle please visit the following communities:
Oracle BI Publisher Blog http://blogs.oracle.com/xmlpublisher/
Oracle BI Publisher Forum https://forums.oracle.com/forums/forum.jspa?forumID=245
My Oracle Support – BI Publisher Community https://community.oracle.com/community/support/oracle_e-business_suite/bi_publisher