Есть следующий код по формированию отчетов в форматах PDF и XLS. При этом, содержимое файла report_name.jrxml хранится в БД и вытягивается оттуда "на лету"
InputStream input = new ByteArrayInputStream(jrXmlText.getBytes("UTF-8"));
JasperReport jasperReport = JasperCompileManager.compileReport(input);
JasperPrint print = JasperFillManager.fillReport(jasperReport, parameters, resultSetDataSource);
response = (HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse();
outputStream = response.getOutputStream();
exportReport(extension, response, print, reportFileName);
outputStream.flush();
И метод exportReport
case "xls":
response.setContentType("application/xls");
reportFileName = URLEncoder.encode(reportFileName, "UTF8");
response.setHeader("Content-Disposition", "attachment; filename*=\"utf-8'" + reportFileName + "." + extension + "\"");
JRXlsExporter xlsExporter = new JRXlsExporter();
xlsExporter.setExporterInput(new SimpleExporterInput(print));
xlsExporter.setExporterOutput(new SimpleOutputStreamExporterOutput(outputStream));
SimpleXlsReportConfiguration xlsReportConfiguration = new SimpleXlsReportConfiguration();
xlsReportConfiguration.setOnePagePerSheet(false);
xlsReportConfiguration.setRemoveEmptySpaceBetweenRows(true);
xlsReportConfiguration.setFontSizeFixEnabled(false);
xlsReportConfiguration.setDetectCellType(true);
xlsReportConfiguration.setWhitePageBackground(false);
xlsExporter.setConfiguration(xlsReportConfiguration);
xlsExporter.exportReport();
break;
case "pdf":
response.setContentType("application/pdf");
reportFileName = URLEncoder.encode(reportFileName, "UTF8");
response.setHeader("Content-Disposition", "attachment; filename*=\"utf-8'" + reportFileName + "." + extension + "\"");
//JasperExportManager.exportReportToPdfStream(print, outputStream); // По идее должен открывать файл в браузере
JRPdfExporter pdfExporter = new JRPdfExporter();
pdfExporter.setExporterInput(new SimpleExporterInput(print));
pdfExporter.setExporterOutput(new SimpleOutputStreamExporterOutput(outputStream));
SimplePdfExporterConfiguration configuration = new SimplePdfExporterConfiguration();
configuration.setCreatingBatchModeBookmarks(true);
pdfExporter.setConfiguration(configuration);
pdfExporter.exportReport();
break;
В Firefox и IE отчеты формируются и пользователь получает стандартный диалог с вопросом
При работе в Crome этого не происходит, вместо диалога сразу скачивается файл (без вопроса пользователю), но с расширением xhtml вместо нужного (pdf/xls).
Сборка персонального компьютера от Artline: умный выбор для современных пользователей