Отчет Jasper Excel экспортирует HTML-контент - PullRequest
0 голосов
/ 17 мая 2018

У меня есть два способа экспортировать 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

This is Exported Excel file Как это исправить?Я использую следующие 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>

1 Ответ

0 голосов
/ 21 мая 2018

Я сделал ошибку с xlsExporter.setExporterOutput методом, а рабочий код

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");
    ServletOutputStream servletOutputStream = httpServletResponse.getOutputStream();
    JRXlsExporter xlsExporter = new JRXlsExporter();
    xlsExporter.setExporterInput(new SimpleExporterInput(jasperPrint));
    xlsExporter.setExporterOutput(new SimpleOutputStreamExporterOutput(servletOutputStream));
    xlsExporter.exportReport();

    FacesContext.getCurrentInstance().responseComplete();
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...