使用数据库作为数据源打印报表,导出成pdf格式。
源码:
/**
* 演示报表效果用
* 打印报表到pdf格式
*
*/
public class JasperPrintDemo {
private String dburl = "";
private String username = "";
private String password = "";
public static void main(String[] args) {
JasperPrintDemo print = new JasperPrintDemo();
try {
print.printOut("D:/iReport/yangzc0913/report4.jrxml", "D:/iReport/yangzc0913/report4.pdf");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 打印报表
* @param filepath jrxml文件路径
* @param destfile 目标路径 pdf格式
* @throws SQLException
*/
private void printOut(String filepath, String destfile) throws SQLException{
File file = new File(filepath);
Connection con = null;
try {
//加载jrxml文件
JasperDesign design = JRXmlLoader.load(file);
//编译文件
JasperReport report = JasperCompileManager.compileReport(design);
con = getCon();//取得连接
Map<String,String> parms = new HashMap<String, String>();
//填充报表
JasperPrint jp = JasperFillManager.fillReport(report, parms,con);
JasperExportManager.exportReportToPdfFile(jp, destfile);//打印报表
// JasperExportManager.exportReportToHtmlFile(jp, destfile);//打印成html
} catch (JRException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} finally{
if(con != null)
con.close();
}
}
/**
* 取得连接
* @return
* @throws SQLException
* @throws ClassNotFoundException
*/
private Connection getCon() throws SQLException, ClassNotFoundException{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection con = DriverManager.getConnection(dburl, username, password);
return con;
}
}
手动数据源
源码:
/**
* 报表打印
* @author 杨忠诚
*
*/
public class JasperPrintDemo1 {
public static void main(String[] args) throws JRException {
Map<String, String> parms = new HashMap<String, String>();
ReportDataSource ds = new ReportDataSource();
// for(int i=0; i< 2; i++){
HashMap<String, Object> hm = new HashMap<String, Object>();
//设定各个参数的值
hm.put("id", "1");
hm.put("missiontimes", "2000-1-1至2004-12-31");
hm.put("mission", "帮助企业实现科学管理!");
hm.put("values", "\t我们生存的目的是能够给客户带来价值,我们真正的要了解他们的需求并实现超出他们预期的成果;");
hm.put("perspective", "打造国产第一品牌的管理软件产品;\n开拓属于自己的管理软件市场;\n培育高素质的管理软件人才;\n树立管理软件的行业丰碑。");
ds.addRecord(hm);
hm = new HashMap<String, Object>();
//设定各个参数的值
hm.put("id", "1");
hm.put("missiontimes", "2008-1-1至2010-12-31");
hm.put("mission", "帮助企业实现科学管理!");
hm.put("values", "我们生存的目的是能够给客户带来价值,我们真正的要了解他们的需求并实现超出他们预期的成果;");
hm.put("perspective", "打造国产第一品牌的管理软件产品;\n开拓属于自己的管理软件市场;\n培育高素质的管理软件人才;\n树立管理软件的行业丰碑。");
ds.addRecord(hm);
hm = new HashMap<String, Object>();
//设定各个参数的值
hm.put("id", "1");
hm.put("missiontimes", "2008-1-1至2010-12-32");
hm.put("mission", "帮助企业实现科学管理!");
hm.put("values", "我们生存的目的是能够给客户带来价值,我们真正的要了解他们的需求并实现超出他们预期的成果;");
hm.put("perspective", "打造国产第一品牌的管理软件产品;\n开拓属于自己的管理软件市场;\n培育高素质的管理软件人才;\n树立管理软件的行业丰碑。");
ds.addRecord(hm);
hm = new HashMap<String, Object>();
//设定各个参数的值
hm.put("id", "1");
hm.put("missiontimes", "2008-1-1至2010-12-33");
hm.put("mission", "帮助企业实现科学管理!");
hm.put("values", "我们生存的目的是能够给客户带来价值,我们真正的要了解他们的需求并实现超出他们预期的成果;");
hm.put("perspective", "打造国产第一品牌的管理软件产品;\n开拓属于自己的管理软件市场;\n培育高素质的管理软件人才;\n树立管理软件的行业丰碑。");
ds.addRecord(hm);
hm = new HashMap<String, Object>();
//设定各个参数的值
hm.put("id", "1");
hm.put("missiontimes", "2008-1-1至2010-12-34");
hm.put("mission", "帮助企业实现科学管理!");
hm.put("values", "我们生存的目的是能够给客户带来价值,我们真正的要了解他们的需求并实现超出他们预期的成果;");
hm.put("perspective", "打造国产第一品牌的管理软件产品;\n开拓属于自己的管理软件市场;\n培育高素质的管理软件人才;\n树立管理软件的行业丰碑。");
ds.addRecord(hm);
hm = new HashMap<String, Object>();
//设定各个参数的值
hm.put("id", "1");
hm.put("missiontimes", "2008-1-1至2010-12-35");
hm.put("mission", "帮助企业实现科学管理!");
hm.put("values", "我们生存的目的是能够给客户带来价值,我们真正的要了解他们的需求并实现超出他们预期的成果;");
hm.put("perspective", "打造国产第一品牌的管理软件产品;\n开拓属于自己的管理软件市场;\n培育高素质的管理软件人才;\n树立管理软件的行业丰碑。");
ds.addRecord(hm);
hm = new HashMap<String, Object>();
//设定各个参数的值
hm.put("id", "1");
hm.put("missiontimes", "2008-1-1至2010-12-36");
hm.put("mission", "帮助企业实现科学管理!");
hm.put("values", "我们生存的目的是能够给客户带来价值,我们真正的要了解他们的需求并实现超出他们预期的成果;");
hm.put("perspective", "打造国产第一品牌的管理软件产品;\n开拓属于自己的管理软件市场;\n培育高素质的管理软件人才;\n树立管理软件的行业丰碑。");
ds.addRecord(hm);
hm = new HashMap<String, Object>();
//设定各个参数的值
hm.put("id", "1");
hm.put("missiontimes", "2008-1-1至2010-12-37");
hm.put("mission", "帮助企业实现科学管理!");
hm.put("values", "我们生存的目的是能够给客户带来价值,我们真正的要了解他们的需求并实现超出他们预期的成果;");
hm.put("perspective", "打造国产第一品牌的管理软件产品;\n开拓属于自己的管理软件市场;\n培育高素质的管理软件人才;\n树立管理软件的行业丰碑。");
ds.addRecord(hm);
// }
//载入报表
JasperDesign design = ReportPrintManager.loadDesign(new File("D:/iReport/report13.jrxml"));
JasperReport report = ReportPrintManager.complieReport(design);//编译报表
//导出报表
ReportPrintManager.printReportToPdf(report, parms, ds, "D:/iReport/ss.pdf");
// ReportPrintManager.printReportToHtml(report, parms, ds, "D:/iReport/ss.html");
}
}