ireport 通过java导出xls,pdf格式
代码如下:
Path reportFile = Paths.get(System.getProperty("user.dir"),
"/webapps/mdap-report/WEB-INF/jaspers/" + report + ".jasper");
String path = reportFile.toString();
Map params = new HashMap();
MysqlConnection mysqlConnection = new MysqlConnection();
connection = mysqlConnection.getConnection();
if (reportType.equals("xls")) {
JasperPrint print = JasperFillManager.fillReport(path, params, connection);
response.setContentType("application/vnd.ms-excel");
String defaultname = "report.xls";
String reportName = new String(defaultname.getBytes("gbk"), "utf-8");
response.setHeader("Content-disposition", "attachment; filename=" + reportName);
ServletOutputStream ouputStream = response.getOutputStream();
JRXlsExporter exporter = new JRXlsExporter();
exporter.setParameter(JRXlsExporterParameter.JASPER_PRINT, print);
exporter.setParameter(JRXlsExporterParameter.OUTPUT_STREAM, ouputStream);
exporter.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.FALSE);
exporter.setParameter(JRXlsExporterParameter.IS_WHITE_PAGE_BACKGROUND, Boolean.FALSE);
exporter.setParameter(JRXlsExporterParameter.IGNORE_PAGE_MARGINS, Boolean.TRUE);
String sheetName = "入口明细表";
String[] sheetNames = { sheetName };
exporter.setParameter(JRXlsExporterParameter.SHEET_NAMES, sheetNames);
String decoderReportName = "入口统计报表";
if (name != null) {
decoderReportName = name;
}
String decoderFileName = UrlUtil.getURLDecoderString(decoderReportName);
response.setHeader("Content-Disposition",
"attachment;filename=" + getFilename(request, decoderFileName) + ".xls");
response.setContentType("application/vnd.ms-excel");
exporter.exportReport();
ouputStream.flush();
ouputStream.close();
}
if (reportType.equals("pdf")) {
// 导出PDF
HashMap<String, Object> parameters = new HashMap<String, Object>();// 给报表模板文件传参
// 得到枚举类型的参数名称,参数名称若有重复的只能得到第一个--获取页面传来的参数,和模板中文件的sql参数名称一一对应
// Enumeration<?> temp = request.getParameterNames();
// while (temp.hasMoreElements()) {
// String paramName = (String) temp.nextElement();
// String paramValue = request.getParameter(paramName);
// parameters.put(paramName, paramValue);
// }
// System.out.println("--------path------------" + path);
MysqlConnection mysqlConnection1 = new MysqlConnection();
connection = mysqlConnection1.getConnection();
byte[] bytes = JasperRunManager.runReportToPdf(path, parameters, connection);
response.setContentType("application/pdf");
response.setContentLength(bytes.length);
ServletOutputStream out;
out = response.getOutputStream();
out.write(bytes, 0, bytes.length);
out.flush();
out.close();
}
}
// } catch (IOException e) {
// e.printStackTrace();
// }
catch (JRException e) {
e.printStackTrace();
}
}
导出excel中数据居中问题
解决方式:可修改纸张大小Page format
报错:类找不到问题
解决方式:一般情况为jar包版本问题
将多个jasper导入到一个excel中,并且是放置到不同的sheet页
JRXlsExporter xls = new JRXlsExporter();
xls.setParameter(JRExporterParameter.JASPER_PRINT_LIST,list);//此处的list为JasperPrint的list集合
xls.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET,
Boolean.TRUE);//设置为true,即可在一个excel中,每个单独的jasper对象放入到一个sheet页中
String[] sheetNames = {"自定义1","自定义2","自定义3"};
xls.setParameter(JRXlsExporterParameter.SHEET_NAMES, sheetNames);//此处这样写即可