springboot integrated birt do report a slightly more complex

Disclaimer: This article is a blogger original article, welcome to reprint. https://blog.csdn.net/guo_xl/article/details/86688832

Foreword

In SpringBoot integrated Birt , the last example is only relatively simple example. This focuses on how to do a slightly more complex report

Target report

To create a user report, the data display portion 2 is divided into
a first portion with a list of all users, a second section lists the details of the user, contains details of the account of different types of users, pdf after completion follows:

Here Insert Picture Description

data structure

The rear end of the acquired data is alist<User>

User data is structured as follows, attention is associated with a user in List<Account>an object

Class User{
    private Long userKy;
    private String fullName;
    private String email;
    private String mobile;
    private Date birthDay;
    private String userId;
    private String language;
    private String companyName;
    private List<Account> accounts;
}
Class Account{
    private long key;
    private String accountName;
    private String accountNo;
    private String ccy;
    private int paymentType;  // 1.payment 2.payroll 3.fix deposit
    private int userKy;
}

Knowledge Point

  • dataSource
  • dataset open fetch
  • DataSource relations and the dataset
  • dataset filter
  • Use grid assembly and table
  • Use script
  • Use css
  • setting master page header and footer

Code flow

  1. birt tool in the establishment of a new report, named user.rptdesin
  2. New build scripted data Source of DataSource, opened its script, enter the following code in the initialize event.
//获取到spring的上下文
var spring=reportContext.getAppContext().get("spring");
//获取到userReportDataService这个service
var userService=spring.getBean("userReportDataService");
//获取数据,也就说datasource里的数据就是userList
userList=userService.getUsers();

java code follows userService

public class UserReportDataService {
 //从一个user.json文件里获取到user的list
     public List<User> getUsers(){
         InputStream is=UserReportDataService.class.getClassLoader().getResourceAsStream("mock/user.json");
         try {
             ObjectMapper mapper = new ObjectMapper();
             JavaType javaType = mapper.getTypeFactory().constructParametricType(ArrayList.class,User.class);
             List<User> users=mapper.readValue(is,javaType);
             return users;
         } catch (IOException e) {
             e.printStackTrace();
         }
         return null;
     }
}
  1. Newly established dataSet, the association just selected dataSource. Named users, opened its script

Select the open input

count=0;

Select the input fech

if(userList.size()>count) {
 user=userList.get(count);
 row.userId=user.getUserId();
 row.fullName=user.getFullName();
 row.mobile=user.getMobile();
 row.language=user.getLanguage(); 
 row.email=user.getEmail();   
 row.companyName=user.getCompanyName();
 row.birthDay=user.getBirthDay();
 row.userKy=user.getUserKy();
 count++;
 return true;
}
return false;

fetch in a row is a record in the dataset, pay attention here can only insert a record.

  1. Newly established dataSet, the association just selected dataSource. Named userAccount, opened its script

Select the open input

importPackage(Packages.java.util)
acctArrList = new ArrayList();

for(var i=0;i<userList.size();i++) {
 u=userList.get(i);
 var accts=u.getAccounts();
 if(accts!=null){
  j=0;
  while(j<accts.size()){
    acct=accts.get(j);
    acct.setUserKy(u.getUserKy());
    acctArrList.add(acct);
    j++;
  }
 }
}
count=0;

Due to fetch only write a record. So first of all the account can only be put out in an ArrayList

Select the input fech

if(acctArrList.size()>count) {
   acct=acctArrList.get(count);
   row.accountName=acct.getAccountName();
   row.accountNo=acct.getAccountNo();
   row.ccy=acct.getCcy();
   row.key=acct.getKey();
   row.paymentType=acct.getPaymentType();
   row.userKy=acct.getUserKy();
   count++;
   return true;
}
return false;

The relationship between the data source and data set?

data source is the data source is obtained, that is to select the data source Scripted acquired code data, as the second step by means of script.
When the data acquisition source is completed, data set acquired from a desired data source in the data set by the open and fetch operations

  1. The entire report design
    Here Insert Picture Description
    divided into a total 1,2,3,4,5 Zhang table.

1 and 2 up and down the grid through a separate
2, 4, 5 each in the row is a detail of
Here Insert Picture Description
a blue box is a detail of the row
rptdesign file download

  1. table is very simple. 1, and then drag a grid table to select the data set of users.
  2. table 2, drag a table to the grid, select the data set is userAccount, table2 contains a 3,4,5.
  • table 3 corresponding user account to the account of the current row paymentType = 1
  • Corresponding to the user account table 4 is the current line account paymentType = 2
  • table 5 corresponding user account to the account of the current row paymentType = 3
  1. In an example to explain. 3 table, drag a table to map the location of the post, as UserAccounts selected data set, which has been time UserAccounts fetch into account all of the inside. Note the use of the red box below the filter conditions
    Here Insert Picture Description

  2. The next step is adding css the report look better
    Here Insert Picture Description
    to add a css styles in the document, in the box below to select the style you want to import

  3. Modified master page, the page header and footer added
    Here Insert Picture Description
    to one Grid at the top, with reference to the downloaded content may Grid rptdesign file

  4. Add a logo image
    Here Insert Picture Description
    Here Insert Picture Description
    add Image added to a picture, and choose to Emedded image, so the picture will be embedded into the report design file.

  5. java code
    this and SpringBoot integrated Birt substantially the same is not specifically described herein
    entire code github code inside.

  6. After running spring boot, enter the postman inhttp://localhost:8080/report/user

debug method

Very bad script found the problem, the simplest way is currently printed in the script, the following

importPackage(Packages.java.lang);
System.out.println("xxxxx");

github code

reference

Guess you like

Origin blog.csdn.net/guo_xl/article/details/86688832