将jasper文件转化为报表输出的代码
来源:互联网 发布:淘宝开店的流程是什么? 编辑:程序博客网 时间:2024/05/16 11:54
不推荐用jsp, 用servlet 比较好,我这里有流程性的,你自己看着用吧,这东西用了2年,没发现问题。
通用的导出类,支持html,Excel,PDF三种
public class ReportType ...{
public static int HTML = 1;
public static int EXCEL = 2;
public static final int PDF = 3;
public static void export(HttpServletResponse response, JasperPrint jp, int type, String filename) throws Exception ...{
JRExporter exporter = null;
if (type == HTML) ...{
exporter = new JRHtmlExporter();
exporter.setParameter(JRHtmlExporterParameter.IS_USING_IMAGES_TO_ALIGN, false);
exporter.setParameter(JRHtmlExporterParameter.BETWEEN_PAGES_HTML, "");
// exporter.setParameter(JRHtmlExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS, Boolean.TRUE);
} else if (type == EXCEL) ...{
exporter = new JRXlsExporter();
response.setContentType("application/vnd.ms-excel");
response.addHeader("Content-Disposition", new String(("attachment; filename=" + filename + ".xls").getBytes("GBK"), "ISO-8859-1"));
exporter.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.FALSE);
// exporter.setParameter(JRXlsExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS, Boolean.TRUE);
} else if (type == PDF) ...{
exporter = new JRPdfExporter();
response.setContentType("application/pdf");
response.addHeader("Content-Disposition", new String(("attachment; filename=" + filename + ".pdf").getBytes("GBK"), "ISO-8859-1"));
} else ...{
return;
}
exporter.setParameter(JRExporterParameter.CHARACTER_ENCODING, "GBK");
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jp);
exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, response.getOutputStream());
exporter.exportReport();
}
}
public static int HTML = 1;
public static int EXCEL = 2;
public static final int PDF = 3;
public static void export(HttpServletResponse response, JasperPrint jp, int type, String filename) throws Exception ...{
JRExporter exporter = null;
if (type == HTML) ...{
exporter = new JRHtmlExporter();
exporter.setParameter(JRHtmlExporterParameter.IS_USING_IMAGES_TO_ALIGN, false);
exporter.setParameter(JRHtmlExporterParameter.BETWEEN_PAGES_HTML, "");
// exporter.setParameter(JRHtmlExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS, Boolean.TRUE);
} else if (type == EXCEL) ...{
exporter = new JRXlsExporter();
response.setContentType("application/vnd.ms-excel");
response.addHeader("Content-Disposition", new String(("attachment; filename=" + filename + ".xls").getBytes("GBK"), "ISO-8859-1"));
exporter.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.FALSE);
// exporter.setParameter(JRXlsExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS, Boolean.TRUE);
} else if (type == PDF) ...{
exporter = new JRPdfExporter();
response.setContentType("application/pdf");
response.addHeader("Content-Disposition", new String(("attachment; filename=" + filename + ".pdf").getBytes("GBK"), "ISO-8859-1"));
} else ...{
return;
}
exporter.setParameter(JRExporterParameter.CHARACTER_ENCODING, "GBK");
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jp);
exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, response.getOutputStream());
exporter.exportReport();
}
}
使用方法
String jasper = "jasper/saler/salerMonthSummary.jasper"; // 你的jasper文件地址
String filename = "营销中心业务员" + from + "-" + to + "销售汇总"; // 对于excel等需要下载的文件名
// 各种参数设置好
Map map = new HashMap();
map.put("YearFrom", from.getYear());
map.put("MonthFrom", from.getMonth());
map.put("YearTo", to.getYear());
map.put("MonthTo", to.getMonth());
// 读取jasper
JasperReport jr = (JasperReport) JRLoader.loadObjectFromLocation(jasper);
// 填充数据
JasperPrint jp = JasperFillManager.fillReport(jr, map, null);
// 判断是否正常
List<JRPrintPage> pages = jp.getPages();
if (pages.size() == 0) ...{
// 没有数据
return;
}
ReportType.export(response, jp, ReportType.EXCEL, filename);
String filename = "营销中心业务员" + from + "-" + to + "销售汇总"; // 对于excel等需要下载的文件名
// 各种参数设置好
Map map = new HashMap();
map.put("YearFrom", from.getYear());
map.put("MonthFrom", from.getMonth());
map.put("YearTo", to.getYear());
map.put("MonthTo", to.getMonth());
// 读取jasper
JasperReport jr = (JasperReport) JRLoader.loadObjectFromLocation(jasper);
// 填充数据
JasperPrint jp = JasperFillManager.fillReport(jr, map, null);
// 判断是否正常
List<JRPrintPage> pages = jp.getPages();
if (pages.size() == 0) ...{
// 没有数据
return;
}
ReportType.export(response, jp, ReportType.EXCEL, filename);
- 将jasper文件转化为报表输出的代码
- 将jasper文件转化为报表输出的代码
- 将文件转化为字符串的方法
- 将任意数字转化为对应gbk输出的程序
- 用Java代码将指定目录下的doc文件转化为txt格式文档
- 将C++代码转化为高亮的html代码
- 将具有特殊格式的文件转化为xml文件
- 将XML文件转化为JSON格式的文件
- 将unicode编码转化为汉字输出
- 将memo转化为JPG输出
- 将PPT转化为PDF我的JAVA代码实现!
- arrayToJson将数组转化为json格式的js代码
- java代码将地址转化为坐标的工具类
- 将c++代码转化为lua代码
- 将指定文件内容转化为指定编码的字符串
- 将FLASH的SWF文件转化为EXE: SWF2EXE
- 将MATLAB的.m脚本转化为.exe文件
- 将csv文件转化为可视化的html表格
- URI-URL-SOAP
- 来到CSDN
- GridView详述
- 以网上支付平台的选择为例说明网站可信度的意义
- sprintf详解
- 将jasper文件转化为报表输出的代码
- Stream转换成String
- CString常用函数
- cxGrid补遗之得到含查询条件的sql语句
- 第三方解决方案与网络会员制营销案例
- CString 操作指南一
- 建立udl数据链接文件,测试链接
- CString 操作指南二
- Threading 3D Game Engine Basics