将jasper文件转化为报表输出的代码
系统
1613 0
不推荐用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();
}
}
使用方法
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);
将jasper文件转化为报表输出的代码
更多文章、技术交流、商务合作、联系博主
微信扫码或搜索:z360901061
微信扫一扫加我为好友
QQ号联系: 360901061
您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。
【本文对您有帮助就好】元