yangyangrenren 2014-01-14 05:27 采纳率: 25%
浏览 4479

在java web工程中·,利用ireport生成的jasper文件,导出pdf文件

我已经用ireport工具生成了jasper文件,并且利用ireport已经可以成功预览并生成pdf文件,但是现在需要在java web工程中,利用jasper文件生成相应的pdf文件,自己测视了好久,在网上也搜了好久,没有找到解决办法,现在把相应代码及错误贴出来,希望会做的朋友帮忙改正一下!
主要的servlet文件,在web.xml中都已将其配置好了
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import util.JDBCConnection;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRExporterParameter;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.JasperRunManager;
import net.sf.jasperreports.engine.export.JRHtmlExporter;
import net.sf.jasperreports.engine.export.JRHtmlExporterParameter;
import net.sf.jasperreports.engine.export.JRPdfExporter;
import net.sf.jasperreports.engine.export.JRXlsExporter;
import net.sf.jasperreports.engine.export.JRXlsExporterParameter;
import net.sf.jasperreports.engine.util.JRLoader;

public class JRHTMLServlet extends HttpServlet {
private Connection conn = JDBCConnection.getConnection(); //这个是连接mysql数据库
public JRHTMLServlet() {
super();
}
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String type = request.getParameter("type");

try

{

        ServletContext servletContext = this.getServletContext();  
        File reportFile = new File(servletContext.getRealPath("/")+"/WEB-INF/report/Untitled_report_1.jasper");  
        Map parameters = new HashMap();  
        if("pdf".equals(type)){  
           byte[] bytes = JasperRunManager.runReportToPdf(reportFile.getPath(), parameters, conn);  
           response.setContentType("application/pdf");  
           response.addHeader("Content-Disposition", "attachment; filename=report.pdf");  
           response.setContentLength(bytes.length);  
           ServletOutputStream ouputStream = response.getOutputStream();  
           ouputStream.write(bytes, 0, bytes.length);  
           ouputStream.flush();  
           ouputStream.close();  
        } else if ("excel".equals(type)){  
           JRXlsExporter exporter = new JRXlsExporter();     
           ByteArrayOutputStream oStream = new ByteArrayOutputStream();     
           JasperPrint jasperPrint = JasperFillManager.fillReport(reportFile.getPath(), parameters, conn);     
           exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);     
           exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, oStream);     
           exporter.setParameter(JRXlsExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS, Boolean.TRUE);     
           exporter.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.FALSE);     
           exporter.setParameter(JRXlsExporterParameter.IS_WHITE_PAGE_BACKGROUND, Boolean.FALSE);     
           exporter.exportReport();     

           byte[] bytes = oStream.toByteArray();     
           response.setContentType("application/vnd.ms-excel");   
           response.addHeader("Content-Disposition", "attachment; filename=report.xls");  
           response.setContentLength(bytes.length);     
           ServletOutputStream ouputStream = response.getOutputStream();     
           ouputStream.write(bytes, 0, bytes.length);     
           ouputStream.flush();     
           ouputStream.close();     

        } else
        {
             JRHtmlExporter exporter = new JRHtmlExporter();     
             ByteArrayOutputStream oStream = new ByteArrayOutputStream();     
             JasperPrint jasperPrint = JasperFillManager.fillReport(reportFile.getPath(), parameters, conn);     
             exporter.setParameter(JRHtmlExporterParameter.IS_USING_IMAGES_TO_ALIGN, Boolean.FALSE);     
             exporter.setParameter(JRHtmlExporterParameter.JASPER_PRINT, jasperPrint);     
             exporter.setParameter(JRHtmlExporterParameter.CHARACTER_ENCODING, "utf-8");     
             exporter.setParameter(JRHtmlExporterParameter.OUTPUT_STREAM, oStream);     
             exporter.exportReport();     
             byte[] bytes = oStream.toByteArray();     
             response.setContentType("text/html");     
             response.setContentLength(bytes.length);     
             response.setCharacterEncoding("utf-8");     
             ServletOutputStream ouputStream = response.getOutputStream();     
             ouputStream.write(bytes, 0, bytes.length);     
             ouputStream.flush();     
             ouputStream.close();
     }
     }  
     catch (JRException jre)  
     {  
        System.out.println("JRException:" + jre.getMessage());  
     }  
     catch (Exception e)  
     {  
        System.out.println("Exception:" + e.getMessage());  
     }  
     finally{  
        try  
        {  
           conn.close();  
        }  
        catch (SQLException ex)  
        {  
           // TODO Auto-generated catch block  
           ex.printStackTrace();  
        }  
     }  

}

    public void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    this.doGet(request, response); 
}

}

在jsp文件中的主要代码:
Servlet中生成pdf
Servlet中生成excel
Servlet中生成html

然后编译都没有问题,在执行时,如果点击了“Servlet中生成pdf”超链接,就会提示错误:
JRException:Error evaluating expression :
Source text : new java.lang.Integer(1)

若点击“Servlet中生成excel”超链接,就会提示错误:
严重: Servlet.service() for servlet JRHTMLServlet threw exception
java.lang.ClassNotFoundException: org.apache.poi.ss.usermodel.Sheet

若点击“Servlet中生成html”超链接,就会提示错误:
JRException:Error preparing statement for executing the report query :

SELECT
customer.ID AS customer_ID,
customer.USERNAME AS customer_USERNAME,
customer.PASSWORD AS customer_PASSWORD,
customer.REALNAME AS customer_REALNAME,
customer.ADDRESS AS customer_ADDRESS,
customer.MOBILE AS customer_MOBILE,
customer.IDCARD_ID AS customer_IDCARD_ID
FROM
customer customer

以上三种都试了,执行时,页面都可以跳转,但是跳转之后,都没有显示,直接出错了,我尝试了好久好久,确实不知道是什么问题,还请各位帮忙看看啊,在这里先谢谢你们啦
附:数据库的连接:
String url = "jdbc:mysql://localhost/test";
String userName = "root";
String password = "123456";
Class.forName("com.mysql.jdbc.Driver");
Connection connection = DriverManager.getConnection(url, userName,password);
数据库表里面有中文,应该没有影响,现在只要求可以在web工程中导出pdf就可以了

  • 写回答

1条回答 默认 最新

  • jieyaqin0128 2015-05-02 10:32
    关注

    ireport的导出功能早就被吐槽n次了。。还是用finereport靠谱点

    评论

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)