- public class Print extends HttpServlet{
- private String filePatch;
- private String fileName;
- /***生成JapserPrint***
- * @throws Exception */
- private JasperPrint init(List<UserSocietyInsuranceReportBean> list,Map map) throws Exception{
- if(this.filePatch.equals(""))
- throw new Exception("没有设置路径!");
- JasperPrint print = null;
- /**** 编译成japser***/
- try{
- JasperCompileManager.compileReportToFile(this.getFilePatch()+getFileName()+".jrxml");
- }catch(Exception ex){
- ex.printStackTrace();
- throw new Exception("编译成Jasper失败",ex);
- }
- /**编译成JaperPrint**/
- try{
- print = JasperFillManager.fillReport(this.getFilePatch()+getFileName()+".jasper",map,new JRBeanCollectionDataSource(list));
- return print;
- }catch(Exception ex){
- ex.printStackTrace();
- throw new Exception("编译成JasperPrint失败",ex);
- }
- }
- /****显示成PDF格式*****/
- public void printPDF(HttpServletRequest request,HttpServletResponse response,List<UserSocietyInsuranceReportBean> list,Map map) throws Exception {
- OutputStream out = response.getOutputStream();
- response.setContentType("application/pdf");
- // response.setHeader("Content-Disposition","attachment; filename="+fileName+".pdf");
- JRPdfExporter exporter = new JRPdfExporter();
- try{
- request.getSession().setAttribute(
- BaseHttpServlet.DEFAULT_JASPER_PRINT_SESSION_ATTRIBUTE,init(list,map));
- List jasperPrintList = BaseHttpServlet
- .getJasperPrintList(request);
- exporter.setParameter(JRExporterParameter.JASPER_PRINT_LIST,
- jasperPrintList);
- exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, out);
- exporter.exportReport();
- }catch(Exception ex){
- ex.printStackTrace();
- }
- finally{
- if(out!=null)
- out.close();
- }
- }
- public String getFilePatch() {
- return filePatch;
- }
- public void setFilePatch(String filePatch) {
- this.filePatch = filePatch;
- }
- public String getFileName() {
- return fileName;
- }
- public void setFileName(String fileName) {
- this.fileName = fileName;
- }
- }
Jasper生成PDF文件
最新推荐文章于 2022-08-26 14:58:20 发布