У меня есть два способа экспортировать PDF и Excel из отчетов Jasper с простыми словами.Экспорт в формате PDF работает отлично.Но экспорт в Excel экспортирует .xlxs, хотя в файле Excel есть html-коды и никаких необходимых данных нет.Код выглядит следующим образом.
ChatListBean.java
public void printExcel(){
String reportPath = JsfUtil.getAbsolutePath("/resources/reports/chat.jasper");
String subReportPath = JsfUtil.getAbsolutePath("/resources/reports");
String realPath = JsfUtil.getAbsolutePath("");
Map<String, Object> params = new HashMap<String, Object>();
params.put("realPath", realPath);
params.put("SUBREPORT_DIR", subReportPath);
// params.put("location", selectedLocation.getName());
JRBeanCollectionDataSource beanCollectionDataSource = new JRBeanCollectionDataSource(findChats);
try {
JasperReportUtil.toEXCEL(beanCollectionDataSource, reportPath, params, "Chat_History");
} catch (Exception ex) {
Logger.getLogger(ChatListBean.class.getName()).log(Level.SEVERE, null, ex);
}
}
JasperReportUtil.java
public static void toEXCEL(JRBeanCollectionDataSource beanCollectionDataSource, String reportPath, Map<String, Object> params, String reportName) throws Exception{
JasperPrint jasperPrint = JasperFillManager.fillReport(reportPath, params, beanCollectionDataSource);
HttpServletResponse httpServletResponse = (HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse();
httpServletResponse.addHeader("Content-disposition", "attachment; filename=" + reportName + ".xlsx");
JRXlsExporter xlsExporter = new JRXlsExporter();
xlsExporter.setExporterInput(new SimpleExporterInput(jasperPrint));
xlsExporter.setExporterOutput(new SimpleOutputStreamExporterOutput(reportName));
xlsExporter.exportReport();
FacesContext.getCurrentInstance().responseComplete();
}
Этоэкспортированный файл Excel
Как это исправить?Я использую следующие maven-зависимости.
<groupId>net.sf.jasperreports</groupId>
<artifactId>jasperreports</artifactId>
<version>6.5.1</version>
и
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.16</version>