1,PDF
//據據jasper文件生成JasperPrint對象
JasperPrint jasperPrint = JasperFillManager.fillReport("WebRoot//JasperFile//report9.jasper", new HashMap(),connection);
//生成我們的導出類JRPdfExporter 來自JRExporter
JRPdfExporter jrpdfExporter = new JRPdfExporter();
//設JasperPrint參數
jrpdfExporter.setParameter(JRExporterParameter.JASPER_PRINT,jasperPrint);
//設置輸入的PDF文件放在什麼地方
jrpdfExporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, "WebRoot//JasperFile//exporter.pdf");
//輸出
jrpdfExporter.exportReport();
或者
File Pdf = new File(pdfFile);
JasperExportManager.exportReportToPdfFile(jasperPrint, Pdf.getAbsolutePath());
2,XLS
//Excel JExcelApiExporter 來自JRExporter
JExcelApiExporter xlsExporter = new JExcelApiExporter();
xlsExporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
xlsExporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME,"WebRoot//JasperFile//exporter.xls");
xlsExporter.exportReport();
3,在WEB上面生成HTML的報表
public class HtmlReportServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
//連結數據庫
Class.forName("com.mysql.jdbc.Driver");
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost/jasperreportdb","root","root");
//獲得Jasper輸入流
InputStream inputStream = getServletConfig().getServletContext().getResourceAsStream("JasperFile//report9.jasper");
//獲得JasperPrint對象
JasperPrint jasperPrint = JasperFillManager.fillReport(inputStream, new HashMap(),connection);
connection.close();
//設置格式
response.setContentType("text/html");
//獲得輸出流 ,這裏不能這樣response.getOutputStream()
PrintWriter printWriter = response.getWriter();
//創建JRHtmlExporter對象
JRHtmlExporter htmlExporter = new JRHtmlExporter();
//把jasperPrint到Session裏面(net.sf.jasperreports.j2ee.jasper_print)
request.getSession().setAttribute(ImageServlet.DEFAULT_JASPER_PRINT_SESSION_ATTRIBUTE, jasperPrint);
//設值jasperPrint
htmlExporter.setParameter(JRExporterParameter.JASPER_PRINT,jasperPrint);
//設置輸出
htmlExporter.setParameter(JRExporterParameter.OUTPUT_WRITER,printWriter);
//設置圖片生成的Servlet(生成圖片就用這個ImageServlet,並且要在XML文件裏面配置 image?image=這個是Servlet的url-pattern)
htmlExporter.setParameter(JRHtmlExporterParameter.IMAGES_URI,"image?image=");
//導出
htmlExporter.exportReport();
printWriter.close();
} catch (SQLException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (JRException e) {
e.printStackTrace();
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doGet(request, response);
}
}
<servlet>
<servlet-name>HtmlReportServlet</servlet-name>
<servlet-class>com.langhua.JasperReport.Servlet.HtmlReportServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>ImageServlet</servlet-name>
<servlet-class>net.sf.jasperreports.j2ee.servlets.ImageServlet</servlet-class>
</servlet>
<!--url-pattern視你的情況而定,如果圖片出不來,你就看他的連結是什麼 -->
<servlet-mapping>
<servlet-name>ImageServlet</servlet-name>
<url-pattern>/servlet/image</url-pattern>
</servlet-mapping>
4,JasperReport調用接口
private static void callJasperReport(String jasperFileStr, String pdfFileStr) {
Connection con = null;
try {
File jasperFile = new File(jasperFileStr);
if (jasperFile.exists()) {
HashMap<String, Object> params = getParamaterMap();
Class.forName("oracle.jdbc.driver.OracleDriver");
con = DriverManager.getConnection(jdbcUrl, dbUser, dbPassword);
JasperPrint jasperPrint = null;
if (con != null) {
jasperPrint = JasperFillManager.fillReport(jasperFileStr,
params, con);
} else {
jasperPrint = JasperFillManager.fillReport(jasperFileStr,
params, new JREmptyDataSource());
}
//PDF
//outputPDF(pdfFileStr, jasperPrint);
//xls
outputXLS(pdfFileStr, jasperPrint);
} else {
System.out.println("找不到jasper文件:" + jasperFileStr);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (con != null) {
try {
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
private static HashMap<String, Object> getParamaterMap() {
HashMap<String, Object> params = new HashMap<String, Object>();
return params;
}
5, Java Project中包含的Jar包可參考:
commons-beanutils-1.8.2.jar
commons-collections-3.2.1.jar
commons-digester-1.7.jar
commons-logging-1.1.jar
iText-2.1.7.jar
iTextAsian.jar
JasperReports-4.0.2.jar
ojdbc14.jar
org-netbeans-core.jar (缺少會報字體異常,Caused by: java.lang.ClassNotFoundException: org.netbeans.beaninfo.editors.ColorEditor$SuperColor)
jxl-2.6.10.jar (缺少會報: java.lang.NoClassDefFoundError: jxl/write/biff/RowsExceededException)