將jasper文件轉(zhuǎn)化為報(bào)表輸出的代碼
系統(tǒng)
1566 0
不推薦用jsp, 用servlet 比較好,我這里有流程性的,你自己看著用吧,這東西用了2年,沒發(fā)現(xiàn)問題。
通用的導(dǎo)出類,支持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?
=
?
"
營銷中心業(yè)務(wù)員
"
?
+
?from?
+
?
"
-
"
?
+
?to?
+
?
"
銷售匯總
"
;?
//
?對(duì)于excel等需要下載的文件名
//
?各種參數(shù)設(shè)置好
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);
//
?填充數(shù)據(jù)
JasperPrint?jp?
=
?JasperFillManager.fillReport(jr,?map,?
null
);
//
?判斷是否正常
List
<
JRPrintPage
>
?pages?
=
?jp.getPages();
if
?(pages.size()?
==
?
0
)?
...
{
??
//
?沒有數(shù)據(jù)
??
return
;
}
ReportType.export(response,?jp,?ReportType.EXCEL,?filename);
將jasper文件轉(zhuǎn)化為報(bào)表輸出的代碼
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061
微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元