大家好,我是考100分的小小码 ,祝大家学习进步,加薪顺利呀。今天说一说jasperreport(jasperreport背景透明),希望您对编程的造诣更进一步.
1.代码如下
传递parameters参数,单个属性
/**
* 打印员工pdf报表
*/
@RequestMapping(value = "/{id}/pdf" , method = RequestMethod.GET)
public void pdf(@PathVariable String id) throws IOException {
//1.引入jasper文件
org.springframework.core.io.Resource resource = new ClassPathResource("templates/profile.jasper");
FileInputStream fis = new FileInputStream(resource.getFile());
//2.构造数据
//用户详情数据
UserCompanyPersonal personal = userCompanyPersonalService.findById(id);
//用户岗位信息数据
UserCompanyJobs jobs = userCompanyJobsService.findById(id);
//用户头像
String staffPhoto = "http://qav8b72pi.bkt.clouddn.com/" + id + "?t=100";
//3.填充pdf模版数据,并输出pdf
Map params = new HashMap();
Map<String, Object> personalMap = BeanMapUtils.beanToMap(personal);
Map<String, Object> jobsMap = BeanMapUtils.beanToMap(jobs);
params.putAll(personalMap);
params.putAll(jobsMap);
params.put("staffPhoto" , staffPhoto);
ServletOutputStream sos = response.getOutputStream();
try{
JasperPrint print = JasperFillManager.fillReport(fis , params , new JREmptyDataSource());
JasperExportManager.exportReportToPdfStream(print , sos);
}catch (JRException e){
e.printStackTrace();
}finally {
sos.flush();
}
}
传递数据库jabc数据源列表参数
右键添加band
java bean数据源
创建field
构造模板
package cn.itcast.controller;
import cn.itcast.domain.User;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@RestController
public class JasperController04 {
/**
* 基于javaBean数据源的形式填充数据
*/
@GetMapping("/testJasper4")
public void createPdf(HttpServletRequest request, HttpServletResponse response) throws Exception {
//1.引入jasper文件
Resource resource = new ClassPathResource("templates/testBean.jasper");
FileInputStream fis = new FileInputStream(resource.getFile());
//2.创建JasperPrint,向jasper文件中填充数据
ServletOutputStream os = response.getOutputStream();
try {
Map parameters = new HashMap<>();
//构建javaBean的数据源
//1.获取到对象的list集合
List<User> userList = getUserList();
//2.通过list集合创建javaBean的数据源对象
JRBeanCollectionDataSource ds = new JRBeanCollectionDataSource(userList);
JasperPrint print = JasperFillManager.fillReport(fis,parameters,ds);
JasperExportManager.exportReportToPdfStream(print,os);
} catch (JRException e) {
e.printStackTrace();
}finally {
os.flush();
}
}
public List<User> getUserList() {
List<User> list = new ArrayList<>();
for(int i=0;i<10;i++) {
User user = new User(i+"", "用户"+i, "传智播客","讲师", "1380000000"+i);
list.add(user);
}
return list;
}
}
2.模板如下
fonts.xml
<?xml version="1.0" encoding="UTF-8"?>
<fontFamilies>
<!--<fontFamily name="Lobster Two">-->
<!--<normal>lobstertwo/LobsterTwo-Regular.otf</normal>-->
<!--<bold>lobstertwo/LobsterTwo-Bold.otf</bold>-->
<!--<italic>lobstertwo/LobsterTwo-Italic.otf</italic>-->
<!--<boldItalic>lobstertwo/LobsterTwo-BoldItalic.otf</boldItalic>-->
<!--<pdfEncoding>Identity-H</pdfEncoding>-->
<!--<pdfEmbedded>true</pdfEmbedded>-->
<!--<!–-->
<!--<exportFonts>-->
<!--<export key="net.sf.jasperreports.html">'Lobster Two', 'Times New Roman', Times, serif</export>-->
<!--</exportFonts>-->
<!--–>-->
<!--</fontFamily>-->
<fontFamily name="华文宋体">
<normal>stsong/stsong.TTF</normal>
<bold>stsong/stsong.TTF</bold>
<italic>stsong/stsong.TTF</italic>
<boldItalic>stsong/stsong.TTF</boldItalic>
<pdfEncoding>Identity-H</pdfEncoding>
<pdfEmbedded>true</pdfEmbedded>
<exportFonts>
<export key="net.sf.jasperreports.html">'华文宋体', Arial, Helvetica, sans-serif</export>
<export key="net.sf.jasperreports.xhtml">'华文宋体', Arial, Helvetica, sans-serif</export>
</exportFonts>
<!--
<locales>
<locale>en_US</locale>
<locale>de_DE</locale>
</locales>
-->
</fontFamily>
</fontFamilies>
jasperreports_extension.properties
net.sf.jasperreports.extension.registry.factory.simple.font.families=net.sf.jasperreports.engine.fonts.SimpleFontExtensionsRegistryFactory
net.sf.jasperreports.extension.simple.font.families.lobstertwo=stsong/fonts.xml
3.模板设计如下
通过beanToMap将所有parameters组合起来传到报表参数中,报表也将相关字段拖到页面上即可。
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
转载请注明出处: https://daima100.com/35866.html