数据源是JavaBean,请问如何调用它在指定的位置生成一个PDF文件;注意,不是在网页页面中调用;
JRBeanCollectionDataSource dataSource = new JRBeanCollectionDataSource(保存数据的List对象);
File reportFile = null;
reportFile = new File(this.getServletContext().getRealPath("/report.jasper"));
FileInputStream fis = new FileInputStream(reportFile);
OutputStream os = null;
Map<String,Object> parameters = new HashMap<String,Object>();//报表要用的参数
try {
JasperPrint jasperPrint = JasperFillManager.fillReport(fis, parameters, dataSource);
byte[] bytes = JasperExportManager.exportReportToPdf(jasperPrint);
response.setHeader("Content-Disposition", "attachment;filename=temp.pdf");
os = response.getOutputStream();
os.write(bytes);
os.flush();
} catch (JRException e) {
e.printStackTrace();
}finally {
try {
if(os!=null)
os.close();
if(fis!=null)
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}追问
File reportFile = null;
reportFile = new File(this.getServletContext().getRealPath("/report.jasper"));
FileInputStream fis = new FileInputStream(reportFile);
OutputStream os = null;
Map<String,Object> parameters = new HashMap<String,Object>();//报表要用的参数
try {
JasperPrint jasperPrint = JasperFillManager.fillReport(fis, parameters, dataSource);
byte[] bytes = JasperExportManager.exportReportToPdf(jasperPrint);
response.setHeader("Content-Disposition", "attachment;filename=temp.pdf");
os = response.getOutputStream();
os.write(bytes);
os.flush();
} catch (JRException e) {
e.printStackTrace();
}finally {
try {
if(os!=null)
os.close();
if(fis!=null)
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}追问
27行报错...请问需要引用什么类包啊...
温馨提示:答案为网友推荐,仅供参考