<dependency>
<groupId>com.lowagie</groupId>
<artifactId>itext</artifactId>
<version>2.1.7</version>
</dependency>
<dependency>
<groupId>com.lowagie</groupId>
<artifactId>itextasian</artifactId>
<version>1.0</version>
</dependency>
<!-- groovy -->
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-all</artifactId>
<version>1.7.5</version>
</dependency>
<!-- jasperreport -->
<dependency>
<groupId>net.sf.jasperreports</groupId>
<artifactId>jasperreports</artifactId>
<version>5.1.0</version>
<exclusions>
<exclusion>
<groupId>com.lowagie</groupId>
<artifactId>itext</artifactId>
</exclusion>
<!-- <exclusion>
<artifactId>jackson-databind</artifactId>
<groupId>com.fasterxml.jackson.core</groupId>
</exclusion>
<exclusion>
<artifactId>jackson-annotations</artifactId>
<groupId>com.fasterxml.jackson.core</groupId>
</exclusion>
<exclusion>
<artifactId>jackson-core</artifactId>
<groupId>com.fasterxml.jackson.core</groupId>
</exclusion> -->
</exclusions>
</dependency>
@ParentPackage("json-default")
@Namespace("/")
@Controller
@Scope("prototype")
public class DBReportAction extends BaseAction<Object> {
@Autowired
private DataSource dataSource;
@Action("db_exportJasperPdf")
public String exportJasperPdf() throws IOException,JRException, SQLException {
// 下载导出
// 设置头信息
ServletActionContext.getResponse().setContentType("application/pdf");
String filename = "运单数据[来源数据库].pdf";
String agent = ServletActionContext.getRequest().getHeader("user-agent");
filename = FileUtils.encodeDownloadFilename(filename, agent);
ServletActionContext.getResponse().setHeader("Content-Disposition","attachment;filename=" + filename);
// 根据 jasperReport模板 生成pdf
// 1.读取模板文件
String jrxml = ServletActionContext.getServletContext().getRealPath("/WEB-INF/jasper/dbReport.jrxml");//改动1:模板文件的放置路径
JasperReport report = JasperCompileManager.compileReport(jrxml);
// 2.设置模板数据
// 2.1 Parameter变量的数据设置
Map<String, Object> paramerters = new HashMap<String, Object>();
paramerters.put("author", "黑马程序员");//改动2:设置parameter(author)数据
// 2.2 Field变量的数据设置
JasperPrint jasperPrint = JasperFillManager.fillReport(report, paramerters, dataSource.getConnection());
// 3.生成PDF客户端
JRPdfExporter exporter = new JRPdfExporter();
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
exporter.setParameter(JRExporterParameter.OUTPUT_STREAM,ServletActionContext.getResponse().getOutputStream());
exporter.exportReport();// 导出 流对接
ServletActionContext.getResponse().getOutputStream().close();
return NONE;
}
}
320.67 KB, 下载次数: 92
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) | 黑马程序员IT技术论坛 X3.2 |