搜索

excel下载,JasperReports输出pdf


发布时间: 2022-11-25 02:57:01    浏览次数:37 次

~~~java
/**
* 导出Excel报表
* @return
*/
@RequestMapping("/exportBusinessReport")
public Result exportBusinessReport(HttpServletRequest request, HttpServletResponse response){
try{
//远程调用报表服务获取报表数据
Map<String, Object> result = reportService.getBusinessReport();

//取出返回结果数据,准备将报表数据写入到Excel文件中
String reportDate = (String) result.get("reportDate");
Integer todayNewMember = (Integer) result.get("todayNewMember");
Integer totalMember = (Integer) result.get("totalMember");
Integer thisWeekNewMember = (Integer) result.get("thisWeekNewMember");
Integer thisMonthNewMember = (Integer) result.get("thisMonthNewMember");
Integer todayOrderNumber = (Integer) result.get("todayOrderNumber");
Integer thisWeekOrderNumber = (Integer) result.get("thisWeekOrderNumber");
Integer thisMonthOrderNumber = (Integer) result.get("thisMonthOrderNumber");
Integer todayVisitsNumber = (Integer) result.get("todayVisitsNumber");
Integer thisWeekVisitsNumber = (Integer) result.get("thisWeekVisitsNumber");
Integer thisMonthVisitsNumber = (Integer) result.get("thisMonthVisitsNumber");
List<Map> hotSetmeal = (List<Map>) result.get("hotSetmeal");

//获得Excel模板文件绝对路径
String temlateRealPath = request.getSession().getServletContext().getRealPath("template") +
File.separator + "report_template.xlsx";

//读取模板文件创建Excel表格对象
XSSFWorkbook workbook = new XSSFWorkbook(new FileInputStream(new File(temlateRealPath)));
XSSFSheet sheet = workbook.getSheetAt(0);

XSSFRow row = sheet.getRow(2);
row.getCell(5).setCellValue(reportDate);//日期

row = sheet.getRow(4);
row.getCell(5).setCellValue(todayNewMember);//新增会员数(本日)
row.getCell(7).setCellValue(totalMember);//总会员数

row = sheet.getRow(5);
row.getCell(5).setCellValue(thisWeekNewMember);//本周新增会员数
row.getCell(7).setCellValue(thisMonthNewMember);//本月新增会员数

row = sheet.getRow(7);
row.getCell(5).setCellValue(todayOrderNumber);//今日预约数
row.getCell(7).setCellValue(todayVisitsNumber);//今日到诊数

row = sheet.getRow(8);
row.getCell(5).setCellValue(thisWeekOrderNumber);//本周预约数
row.getCell(7).setCellValue(thisWeekVisitsNumber);//本周到诊数

row = sheet.getRow(9);
row.getCell(5).setCellValue(thisMonthOrderNumber);//本月预约数
row.getCell(7).setCellValue(thisMonthVisitsNumber);//本月到诊数

int rowNum = 12;
for(Map map : hotSetmeal){//热门套餐
String name = (String) map.get("name");
Long setmeal_count = (Long) map.get("setmeal_count");
BigDecimal proportion = (BigDecimal) map.get("proportion");
row = sheet.getRow(rowNum ++);
row.getCell(4).setCellValue(name);//套餐名称
row.getCell(5).setCellValue(setmeal_count);//预约数量
row.getCell(6).setCellValue(proportion.doubleValue());//占比
}

//通过输出流进行文件下载
ServletOutputStream out = response.getOutputStream();
response.setContentType("application/vnd.ms-excel");
response.setHeader("content-Disposition", "attachment;filename=report.xlsx");
workbook.write(out);

out.flush();
out.close();
workbook.close();

return null;
}catch (Exception e){
return new Result(false, MessageConstant.GET_BUSINESS_REPORT_FAIL,null);
}
}
~~~

 

 

JasperReports是一个强大、灵活的报表生成工具,能够展示丰富的页面内容,并将之转换成PDF,HTML,或者XML格式。该库完全由Java写成,可以用于在各种Java应用程序,包括J2EE,Web应用程序中生成动态内容。一般情况下,JasperReports会结合Jaspersoft Studio(模板设计器)使用导出PDF报表。

 

maven坐标:

 

```xml
<dependency>
<groupId>net.sf.jasperreports</groupId>
<artifactId>jasperreports</artifactId>
<version>6.8.0</version>
</dependency>
```

 

## 2. JasperReports概述

 

### 2.1 JasperReports快速体验

 

本小节我们先通过一个快速体验来感受一下JasperReports的开发过程。

 

第一步:创建maven工程,导入JasperReports的maven坐标

 

~~~xml
<dependency>
<groupId>net.sf.jasperreports</groupId>
<artifactId>jasperreports</artifactId>
<version>6.8.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
~~~

 

第二步:将提前准备好的jrxml文件复制到maven工程中(后面会详细讲解如何创建jrxml文件)

 

![42](42.png)

 

第三步:编写单元测试,输出PDF报表

 

~~~java
@Test
public void testJasperReports()throws Exception{
String jrxmlPath =
"D:\\ideaProjects\\projects111\\jasperdemo\\src\\main\\resources\\demo.jrxml";
String jasperPath =
"D:\\ideaProjects\\projects111\\jasperdemo\\src\\main\\resources\\demo.jasper";

 

//编译模板
JasperCompileManager.compileReportToFile(jrxmlPath,jasperPath);

 

//构造数据
Map paramters = new HashMap();
paramters.put("reportDate","2019-10-10");
paramters.put("company","itcast");
List<Map> list = new ArrayList();
Map map1 = new HashMap();
map1.put("name","xiaoming");
map1.put("address","beijing");
map1.put("email","xiaoming@itcast.cn");
Map map2 = new HashMap();
map2.put("name","xiaoli");
map2.put("address","nanjing");
map2.put("email","xiaoli@itcast.cn");
list.add(map1);
list.add(map2);

 

//填充数据
JasperPrint jasperPrint =
JasperFillManager.fillReport(jasperPath,
paramters,
new JRBeanCollectionDataSource(list));

 

//输出文件
String pdfPath = "D:\\test.pdf";
JasperExportManager.exportReportToPdfFile(jasperPrint,pdfPath);
}

 

 

~~~java
//导出运营数据到pdf并提供客户端下载
@RequestMapping("/exportBusinessReport4PDF")
public Result exportBusinessReport4PDF(HttpServletRequest request, HttpServletResponse response) {
try {
Map<String, Object> result = reportService.getBusinessReportData();

//取出返回结果数据,准备将报表数据写入到PDF文件中
List<Map> hotSetmeal = (List<Map>) result.get("hotSetmeal");

//动态获取模板文件绝对磁盘路径
String jrxmlPath =
request.getSession().getServletContext().getRealPath("template") + File.separator + "health_business3.jrxml";
String jasperPath =
request.getSession().getServletContext().getRealPath("template") + File.separator + "health_business3.jasper";
//编译模板
JasperCompileManager.compileReportToFile(jrxmlPath, jasperPath);

//填充数据---使用JavaBean数据源方式填充
JasperPrint jasperPrint =
JasperFillManager.fillReport(jasperPath,result,
new JRBeanCollectionDataSource(hotSetmeal));

ServletOutputStream out = response.getOutputStream();
response.setContentType("application/pdf");
response.setHeader("content-Disposition", "attachment;filename=report.pdf");

//输出文件
JasperExportManager.exportReportToPdfStream(jasperPrint,out);

return null;
} catch (Exception e) {
e.printStackTrace();
return new Result(false, MessageConstant.GET_BUSINESS_REPORT_FAIL);
}
}

免责声明 excel下载,JasperReports输出pdf,资源类别:文本, 浏览次数:37 次, 文件大小:-- , 由本站蜘蛛搜索收录2022-11-25 02:57:01。此页面由程序自动采集,只作交流和学习使用,本站不储存任何资源文件,如有侵权内容请联系我们举报删除, 感谢您对本站的支持。 原文链接:https://www.cnblogs.com/songyuejava/p/16916645.html