import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.HashMap;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.sf.jasperreports.engine.JREmptyDataSource;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRExporterParameter;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
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;
//功能:测试报表生成pdf,html,excel三种文件的形式
public class Report
{
public String execute(HttpServletRequest request, HttpServletResponse response) throws Exception
{
String dispatch = request.getParameter("dispatch");
if (dispatch.equals("pdf"))
{
pdfAction(request, response);
}
else if (dispatch.equals("html"))
{
htmlAction(request, response);
}
else if (dispatch.equals("excel"))
{
excelAction(request, response);
}
else if (dispatch.equals("jfreechart"))
{
jfreechartAction(request, response);
}
else
{
System.out.println("Do nothing!");
}
return null;
}
// 生成Pdf文件形式的报表
private void pdfAction(HttpServletRequest request, HttpServletResponse response) throws Exception
{
File jasperfile = new File(request.getSession().getServletContext().getRealPath("/jaspers/demo1.jasper"));
JasperReport jasperReport = (JasperReport) JRLoader.loadObject(jasperfile);
File destFile = new File(jasperfile.getParent() + "\\demo1.pdf");// 生成pdf文件的路径
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, new HashMap(), getConnection());// 据据jasper文件生成JasperPrint对象
exportPdf(jasperPrint, destFile);
response.sendRedirect("jaspers/demo1.pdf");
}
// 生成Html文件形式的报表
private void htmlAction(HttpServletRequest request, HttpServletResponse response) throws Exception
{
File jasperfile = new File(request.getSession().getServletContext().getRealPath("/jaspers/demo1.jasper"));
JasperReport jasperReport = (JasperReport) JRLoader.loadObject(jasperfile);
File destFile = new File(jasperfile.getParent() + "\\demo1.html");
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, new HashMap(), getConnection());
exportHtml(jasperPrint, destFile);
response.sendRedirect("jaspers/demo1.html");
}
// 生成Excel文件形式的报表
private void excelAction(HttpServletRequest request, HttpServletResponse response) throws Exception
{
File jasperfile = new File(request.getSession().getServletContext().getRealPath("/jaspers/demo1.jasper"));
JasperReport jasperReport = (JasperReport) JRLoader.loadObject(jasperfile);
File destFile = new File(jasperfile.getParent() + "\\demo1.xls");
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, new HashMap(), getConnection());
exportExcel(jasperPrint, destFile);
response.sendRedirect("jaspers/demo1.xls");
}
// 生成jfreechart文件形式的报表
private void jfreechartAction(HttpServletRequest request, HttpServletResponse response) throws Exception
{
File jrxmlfile = new File(request.getSession().getServletContext().getRealPath("/jaspers/JFreeChartReport.jrxml"));
File destFile = new File(jrxmlfile.getParent() + "\\JFreeChartReport.html");
try
{
JasperReport jasperReport = compileJrxml(jrxmlfile);// 编译jrxml文件
HashMap<String, String> parameters = new HashMap<String, String>();// 传入的参数
parameters.put("ReportTitle", "LAOSHULIN");
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, new JREmptyDataSource());// 报表打印对象的数据填充
exportHtml(jasperPrint, destFile); // 以Html文件形式输出
response.sendRedirect("jaspers/JFreeChartReport.html");
}
catch (Exception e)
{
e.printStackTrace();
}
}
// 生成Excel文件类型的报表
private void exportExcel(JasperPrint jasperPrint, File outfile)
{
JRXlsExporter xlsExporter = new JRXlsExporter();
xlsExporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);// 定义JasperPrint对象
xlsExporter.setParameter(JRExporterParameter.OUTPUT_FILE, outfile);// 定义保存Pdf文件的路径
xlsExporter.setParameter(JRXlsExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS, Boolean.TRUE); // 删除记录最下面的空行
xlsExporter.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.FALSE);// 删除多余的ColumnHeader
xlsExporter.setParameter(JRXlsExporterParameter.IS_WHITE_PAGE_BACKGROUND, Boolean.FALSE);// 显示边框
try
{
xlsExporter.exportReport();
}
catch (JRException e)
{
e.printStackTrace();
}
}
// 生成Pdf文件类型的报表
private void exportPdf(JasperPrint jasperPrint, File outfile)
{
JRPdfExporter pdfExporter = new JRPdfExporter();
pdfExporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); // 定义JasperPrint对象
pdfExporter.setParameter(JRExporterParameter.OUTPUT_FILE, outfile); // 定义保存Pdf文件的路径
try
{
pdfExporter.exportReport();
}
catch (JRException e)
{
e.printStackTrace();
}
}
// 生成Html文件类型的报表
private void exportHtml(JasperPrint jasperPrint, File outfile)
{
JRHtmlExporter htmlExporter = new JRHtmlExporter();
htmlExporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); // 定义JasperPrint对象
htmlExporter.setParameter(JRExporterParameter.OUTPUT_FILE, outfile); // 定义保存Html文件的路径
htmlExporter.setParameter(JRHtmlExporterParameter.IS_USING_IMAGES_TO_ALIGN, Boolean.FALSE);// 输出引擎是否用小图片矫正
htmlExporter.setParameter(JRHtmlExporterParameter.IS_OUTPUT_IMAGES_TO_DIR, Boolean.FALSE);// 是否输出图片到硬盘
try
{
htmlExporter.exportReport();
}
catch (JRException e)
{
e.printStackTrace();
}
}
// 将Jrxml编译成Jasper文件
private JasperReport compileJrxml(File jrxmlfile)
{
InputStream in = null;
JasperReport jasperReport = null;
try
{
in = new FileInputStream(jrxmlfile);
jasperReport = JasperCompileManager.compileReport(in); // 编译成jasper文件
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
catch (JRException e)
{
e.printStackTrace();
}
return jasperReport;
}
// 获取与数据库的连接
private Connection getConnection()
{
Connection conn = null;
try
{
Class.forName("org.postgresql.Driver");
conn = DriverManager.getConnection("jdbc:postgresql://127.0.0.1:5432/netview", "postgres", "system");
}
catch (Exception e)
{
e.printStackTrace();
}
return conn;
}
}
package com.report1;
import java.io.File;
import java.sql.Connection;
import java.util.HashMap;
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.export.JRPdfExporter;
import net.sf.jasperreports.engine.util.JRLoader;
public class ReportTest {
public static final String inpath = "D:\\test\\report\\";
public static final String outpath = "D:\\test\\report\\";
public void testPdf(){
try {
File jasperfile = new File(inpath+"demo1.jasper");
JasperReport jasperReport = (JasperReport) JRLoader.loadObject(jasperfile);
File destFile = new File(outpath + "\\demo1.pdf");// 生成pdf文件的路径
Connection connection = null;
JasperPrint jasperPrint;
jasperPrint = JasperFillManager.fillReport(jasperReport, new HashMap(), connection);
JRPdfExporter pdfExporter = new JRPdfExporter();
pdfExporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); // 定义JasperPrint对象
pdfExporter.setParameter(JRExporterParameter.OUTPUT_FILE, destFile); // 定义保存Pdf文件的路径
try
{
pdfExporter.exportReport();
}
catch (JRException e)
{
e.printStackTrace();
}
}
catch (JRException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}// 据据jasper文件生成JasperPrint对象
}
public static void main(String[] args) {
ReportTest test = new ReportTest();
test.testPdf();
}
}