这篇文章主要介绍了java 中jasperreport实现动态列打印的实现代码的相关资料,希望通过本文大家能掌握这部分内容,需要的朋友可以参考下
Java 中jasperReport实现动态列打印的实现代码
以下代码中注释说明很清楚,希望能帮助到大家,大家参考下。
示例代码:
public ActionResult projectPrint() {
String[] printValue = null;
// 从页面中获得要查询的字段
String reqPrintValue = getRequest().getParameter("printValue");
// 没有选择则默认全打印
if (null == reqPrintValue || StringUtils.isEmpty(reqPrintValue)) {
printValue = new String[] { "pnumber", "pname", "pdepart", "pdecision", "pthrow", "plastmonth", "pfund", "ploan" };
} else {
printValue = reqPrintValue.split(",");
}
// 查询统计数据
List
public static void export(JasperPrint jasperPrint, HttpServletResponse response, HttpServletRequest request,
String type) throws IOException {
if (EXCEL_TYPE.equals(type)) {
exportExcel(jasperPrint, response, request);
} else if (PDF_TYPE.equals(type)) {
exportPDF(jasperPrint, response, request);
} else if (HTML_TYPE.equals(type)) {
exportHTML(jasperPrint, response, request);
} else {
exportPrint(jasperPrint, response, request);
}
}
public static void exportExcel(JasperPrint jasperPrint, HttpServletResponse response, HttpServletRequest request)
throws IOException {
response.setContentType("application/vnd.ms-excel");
String filename = DownloadHelper.encodeFilename("未命名.xls", request);
response.setHeader("Content-disposition", "attachment;filename=" + filename);
ServletOutputStream ouputStream = response.getOutputStream();
JRXlsExporter exporter = new JRXlsExporter();
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, ouputStream);
exporter.setParameter(JRXlsExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS, Boolean.TRUE);
exporter.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.FALSE);
exporter.setParameter(JRXlsExporterParameter.IS_WHITE_PAGE_BACKGROUND, Boolean.FALSE);
try {
exporter.exportReport();
} catch (JRException e) {
e.printStackTrace();
}
ouputStream.flush();
ouputStream.close();
}
public static void exportPDF(JasperPrint jasperPrint, HttpServletResponse response, HttpServletRequest request)
throws IOException {
response.setContentType("application/pdf");
String filename = DownloadHelper.encodeFilename("未命名.pdf", request);
response.setHeader("Content-disposition", "attachment;filename=" + filename);
ServletOutputStream ouputStream = response.getOutputStream();
try {
JasperExportManager.exportReportToPdfStream(jasperPrint, ouputStream);
} catch (JRException e) {
e.printStackTrace();
}
ouputStream.flush();
ouputStream.close();
}