Different Formats In Report Generation Using JSReport Server

The JSReport Server supports different types of formats in report generation, like pdf, xlsx, html, text, html to xlsx etc. In previous articles, the report was generated in PDF format. This is a continuation of my previous article with other types of formats, like XLS for generating reports.

For more information of how to useJSReport, read the below article.

Before implementing the below sample, please make sure that JSReport Server and node.js are up and running in your computer.

Report in Excel format: (using handlebars engine)

Create a new template with “Excel Sample” and JSReport Studio.
JSReport
Create an empty JSON file in ‘data +’ section and name it “Excel Sample”.

Now, create an empty spreadsheet with required title, header, and images as shown below.

JSReport
Save the above file in your machine.

Now, go to JSReport Studio, under ‘xlsxTemplates +’ section, and upload the above created empty spreadsheet.
JSReport
Highlight the empty template and set few properties as shown in the below figure.
JSReport
In order to generate the XLS report, the above properties are mandatory.

To show the report, we need some sample JSON data in “Excel Sample.JSON” file. Use the below sample data and save the JSON file in report server. 
  1. {  
  2.     "deptInfo": {  
  3.         "deptNumber": 101,  
  4.         "deptName""Engineering",  
  5.         "deptBuilding""A-Block",  
  6.         "empCount""A"  
  7.     },  
  8.     "empDetails": {  
  9.         "titles": [{  
  10.             "title""Employee Name"  
  11.         }, {  
  12.             "title""Designation"  
  13.         }, {  
  14.             "title""Experience"  
  15.         }, {  
  16.             "title""Remarks"  
  17.         }],  
  18.         "values": [{  
  19.                 "name""Suresh",  
  20.                 "designation""Senior QA Engineer",  
  21.                 "exp""5 yrs",  
  22.                 "remarks""Selenium, Manual Automation."  
  23.             },  
  24.             {  
  25.                 "name""Raghava",  
  26.                 "designation""Senior Software Engineer",  
  27.                 "exp""6 yrs",  
  28.                 "remarks""C#.Net, Asp.Net Core WebApi"  
  29.             },  
  30.             {  
  31.                 "name""Kiran Kumar",  
  32.                 "designation""Technical Lead",  
  33.                 "exp""8 yrs",  
  34.                 "remarks""Scrum master, .Net Framework, Agile Develpment"  
  35.             },  
  36.             {  
  37.                 "name""Prameela",  
  38.                 "designation""Trainee Engineer",  
  39.                 "exp""1.2 yrs",  
  40.                 "remarks""C#.Net, Sql Server 2008"  
  41.             }  
  42.         ]  
  43.     }  
  44. }  

So far, we have created sample data, and a sample spread sheet. Now, build the actual XLS template with handlebars syntax. This is used to generate the Excel report on Server.

Write the following code in template section.
  1. {{#xlsxAdd "xl/worksheets/sheet1.xml" "worksheet.sheetData[0].row"}}  
  2. <row r="3">  
  3.     <c t="inlineStr" s="{{@root.$removedItem.c.[0].$.s}}"><is><t>{{deptInfo.deptNumber}}</t></is></c>  
  4.     <c t="inlineStr" s="{{@root.$removedItem.c.[1].$.s}}"><is><t>{{deptInfo.deptName}}</t></is></c>  
  5.     <c t="inlineStr" s="{{@root.$removedItem.c.[2].$.s}}"><is><t>{{deptInfo.deptBuilding}}</t></is></c>  
  6.     <c t="inlineStr" s="{{@root.$removedItem.c.[3].$.s}}"><is><t>{{deptInfo.empCount}}</t></is></c>  
  7. </row>  
  8. {{/xlsxAdd}}  
  9.   
  10. {{#xlsxAdd "xl/worksheets/sheet1.xml" "worksheet.sheetData[0].row"}}  
  11. <row r="5">  
  12.     {{#each empDetails.titles}}  
  13.         <c t="inlineStr" s="4"><is><t>{{title}}</t></is></c>  
  14.     {{/each}}  
  15. </row>  
  16. {{/xlsxAdd}}  
  17.   
  18. {{#each empDetails.values}}  
  19. {{#xlsxAdd "xl/worksheets/sheet1.xml" "worksheet.sheetData[0].row"}}  
  20. <row r="{{#getIndex 6 @index}}{{/getIndex}}">  
  21.     <c t="inlineStr"><is><t>{{name}}</t></is></c>  
  22.     <c t="inlineStr"><is><t>{{designation}}</t></is></c>  
  23.     <c t="inlineStr"><is><t>{{exp}}</t></is></c>  
  24.     <c t="inlineStr"><is><t>{{remarks}}</t></is></c>  
  25. </row>  
  26. {{/xlsxAdd}}  
  27. {{/each}}  
  28.   
  29. {{{xlsxPrint}}}  

In order to generate the number of rows, we need to use the following custom script. This “getIndex()” is used in the above <row> tag.

  1. function getIndex(val1, val2){  
  2.     return val1 + val2;  
  3. }  

After completing the above steps, SAVE or SAVE ALL the template and click “Run” button in JSReport Studio to display the xlsx report on the screen. The output should look like the below screen.
JSReport
Attributes in Excel template design:

In the above template,

  • ‘r’ indicates row and the row number started with 3 because the uploaded design template occupies the first 2 rows for header section with image and title.
  • ‘c’ indicates column in a row.
  • ‘t’ indicates type, which means the type of value contained in the column cell.
  • ‘s’ indicates style, initially the uploaded template ( Which is appear in xlsxTemplates section) has some set of predefined styles. Which can be applied to the template with index value starting from ‘0’ or can be specified with removed item in a row.
  • {{xlsxPrint}} is a mandatory tag for excel and should be added at end of the template in order to print the excel report from server.

Customizing the report with Drilldown/Collapsible Section:

  • There could be some scenarios where we need to display the report in collapsible/drilldown. For this XLS provides a special attribute “outlineLevel”. If this is set to “1” in each row like below the data can be viewable as collapsible/expanded section. After adding this attribute the section should look like below screen.
    1. {{#each empDetails.values}}  
    2. {{#xlsxAdd "xl/worksheets/sheet1.xml" "worksheet.sheetData[0].row"}}  
    3. <row r="{{#getIndex 6 @index}}{{/getIndex}}" outlineLevel="1">  
    4.     <c t="inlineStr"><is><t>{{name}}</t></is></c>  
    5.     <c t="inlineStr"><is><t>{{designation}}</t></is></c>  
    6.     <c t="inlineStr"><is><t>{{exp}}</t></is></c>  
    7.     <c t="inlineStr"><is><t>{{remarks}}</t></is></c>  
    8. </row>  
    9. {{/xlsxAdd}}  
    10. {{/each}}  

  • Now, re-run the jsreport studio and the output will display with grouping icons ( + collapsible mode, - expanded mode) as below.

    JSReport
    Fig: Expanded Mode

    JSReport
    Fig: Collapsible Mode

Applying own style formats to the report

So far we have worked with predefined styles in the XLS template. But there are some instance where it requires our formatted styles. This can be achieved by modifying ‘s’ attribute values.

In order to apply the custom styles, go to the server installed path and look for content.txt file.

Write down some own custom styles and combine them into the existing styles in the jsreport server folder.

For example, assume that your jsreport server is running in D:\jsreportserver path then excel template styles are located in D:\jsreportserver\data\xlsxTemplates\<templateName> path with 3 files like below,

JSReport
This content.TXT file is in JSON format. So, open content.txt file in any text editor and copy the contents to any JSON formatter like jsonlint. But, internally this txt file uses “open XML” patterns. For more information on how to use open XML, please use the link http://officeopenxml.com/SSstyles.php . After applying the custom styles in the page, save the page and restart the jsreport server.

Now, run the above report in the jsreport studio.

The sample custom style should look like below,

  1. "cellXfs": [{  
  2.     "$": {  
  3.         "count""20"  
  4.     },  
  5.     "xf": [{  
  6.         "$": {  
  7.             "numFmtId""0",  
  8.             "fontId""0",  
  9.             "fillId""0",  
  10.             "borderId""1",  
  11.             "xfId""0"  
  12.         }  
  13.     },{  
  14.         "$": {  
  15.             "numFmtId""164",  
  16.             "fontId""4",  
  17.             "fillId""2",  
  18.             "borderId""1",  
  19.             "xfId""0",  
  20.             "applyNumberFormat""1",  
  21.             "applyFont""1",  
  22.             "applyFill""1",  
  23.             "applyBorder""1",  
  24.             "applyAlignment""1"  
  25.         },  
  26.         "alignment": [{  
  27.             "$": {  
  28.                 "horizontal""center",  
  29.                 "vertical""top",  
  30.                 "wrapText""1",  
  31.                 "readingOrder""1"  
  32.             }  
  33.         }]  
  34.     }  
  35.     ....  
  36.     ....  
  37.     ....  
  38. ]