2 yangyangrenren yangyangrenren 于 2014.01.14 13:27 提问

在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
jieyaqin0128   2015.05.02 18:32

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

Csdn user default icon
上传中...
上传图片
插入图片