您的位置:首页 > Web前端 > HTML

iReport制作EXCEL、PDF或者HTML文件

2014-02-23 23:02 323 查看
公司用到了iReport制作报表,正好,毕业论文上也得用到报表,也就稍稍学习了下。个人感觉还是有个人教你的话,这个入门的最佳捷径。

一、首先是下载iReport,我下载的是iReport-5.1.0,下载地址:http://yunpan.cn/QpaIG6AMXkbwU

二、这个iReport的使用教程就不用我教了吧,自个搜个教程去,呵呵。

三、SSH框架与iReport集成,采用的是将iReport编译产生的jsper文件放在SRC目录下。

四、与jasperreport集成所需要的jar包有这些:jasperreports-5.1.2.jar、commons-digester-2.1.jar、groovy-all-2.0.1.jar、poi-3.7-20101029.jar等等

就不一一列举出来了,不过提供下载地址:http://yunpan.cn/Q4JtzMQaacpjM

五、我将生成EXCEL、PDF或者HTML文件的方法放在ReportServiceImpl类中,代码如下:

public static Connection conn = DButil.getConnection();
/**
* 导出为html文件
* @param response
*/
@Override
public void exportHtml(HttpServletResponse response){

try {
response.setCharacterEncoding("UTF-8");
String path = ReportServiceImpl.class.getClassLoader().getResource("excelDemo.jasper").getPath();
JasperReport jasperReport =(JasperReport)JRLoader.loadObject(path);
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, null,conn);
JRHtmlExporter exporter = new JRHtmlExporter();

exporter.setParameter(JRHtmlExporterParameter.JASPER_PRINT, jasperPrint);
exporter.setParameter(JRHtmlExporterParameter.OUTPUT_WRITER,response.getWriter());
exporter.setParameter(JRHtmlExporterParameter.IS_USING_IMAGES_TO_ALIGN,Boolean.FALSE);
exporter.setParameter(JRExporterParameter.CHARACTER_ENCODING, "utf-8");
exporter.exportReport();
} catch (JRException e) {
e.printStackTrace();
} catch (IOException e){
e.printStackTrace();
}
}

/**
* 导出为excel文件
* @param response
*/
@Override
public void exportExcel( HttpServletResponse response){
try {
response.setCharacterEncoding("UTF-8");

String path = ReportServiceImpl.class.getClassLoader().getResource("excelDemo.jasper").getPath();
JasperReport jasperReport= (JasperReport)JRLoader.loadObject(path);
JasperPrint jasperPrint=JasperFillManager.fillReport(jasperReport,null,conn);
JRXlsExporter exporter=new JRXlsExporter();
exporter.setParameter(JRXlsExporterParameter.JASPER_PRINT, jasperPrint);
exporter.setParameter(JRXlsExporterParameter.OUTPUT_STREAM, response.getOutputStream());
exporter.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.FALSE);
exporter.setParameter(JRXlsExporterParameter.IS_WHITE_PAGE_BACKGROUND, Boolean.FALSE);

response.setHeader("Content-Disposition", "attachment;filename=first.xls");
response.setContentType("application/vnd_ms-excel");
exporter.exportReport();
} catch (JRException e) {
e.printStackTrace();
} catch (IOException e){
e.printStackTrace();
}
}

/**
* 导出为pdf文件
* @param response
*/
@Override
public void exportPdf(HttpServletResponse response) {
try {
String path = ReportServiceImpl.class.getClassLoader().getResource("excelDemo.jasper").getPath();
JasperReport jasperReport= (JasperReport)JRLoader.loadObject(path);
JasperPrint jasperPrint=JasperFillManager.fillReport(jasperReport,null,conn);
JRPdfExporter exporter = new JRPdfExporter();
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, response.getOutputStream());

response.setHeader("Content-Disposition", "attachment;filename=first.pdf");
response.setContentType("application/pdf");
response.setCharacterEncoding("UTF-8");
exporter.exportReport();

} catch (Exception e) {
e.printStackTrace();
}
}


六、所遇到的问题

1.首先老是提示缺少类的问题,不过,下载了这个完整的jar包的就不会有这样的问题了。

1.在导出PDF时,出现了问题:无法显示中文,有中文的地方都显示为空白。

解决办法:


七、参考资料

/article/2212848.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: