daily pastebin goal
32%
SHARE
TWEET

Untitled

a guest Jan 12th, 2018 51 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public static AbstractJasperReportsSingleFormatView getJasperReportsView(HttpServletRequest request, String url, String format, String fileName) {
  2.         String[] sheetNames = {fileName};
  3.         String viewFormat = format == null ? "pdf" : format;
  4.  
  5.         // set possible content headers
  6.         Properties availableHeaders = new Properties();
  7.         availableHeaders.put("html", "inline; filename=" + fileName + ".html");
  8.         availableHeaders.put("csv", "inline; filename=" + fileName + ".csv");
  9.         availableHeaders.put("pdf", "inline; filename=" + fileName + ".pdf");
  10.         availableHeaders.put("xls", "inline; filename=" + fileName + ".xls");
  11.         availableHeaders.put("xlsx", "inline; filename=" + fileName + ".xlsx");
  12.  
  13.         // get jasperView class based on the format supplied
  14.         // defaults to pdf
  15.         AbstractJasperReportsSingleFormatView jasperView;
  16.         if (viewFormat.equals("csv")) {
  17.             jasperView = new JasperReportsCsvView();
  18.             Map exporterParameters = new HashMap();
  19.             exporterParameters.put("net.sf.jasperreports.engine.export.JRCsvExporterParameter.FIELD_DELIMITER", "|");
  20.             exporterParameters.put("net.sf.jasperreports.engine.export.JRCsvExporterParameter.RECORD_DELIMITER", "\r\n");
  21.             jasperView.setExporterParameters(exporterParameters);
  22.         } else if (viewFormat.equals("html")) {
  23.             jasperView = new JasperReportsHtmlView();
  24.         } else if (viewFormat.equals("xls")) {
  25.             Map exporterParameters = new HashMap();
  26.             exporterParameters.put("net.sf.jasperreports.engine.export.JRXlsExporterParameter.IS_DETECT_CELL_TYPE", true);
  27.             exporterParameters.put("net.sf.jasperreports.engine.export.JRXlsExporterParameter.IS_COLLAPSE_ROW_SPAN", true);
  28.             exporterParameters.put("net.sf.jasperreports.engine.export.JRXlsExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_COLUMNS", true);
  29.             exporterParameters.put("net.sf.jasperreports.engine.export.JRXlsExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS", true);
  30.             exporterParameters.put("net.sf.jasperreports.engine.export.JRXlsExporterParameter.IS_WHITE_PAGE_BACKGROUND", false);
  31.             exporterParameters.put("net.sf.jasperreports.engine.export.JRXlsExporterParameter.SHEET_NAMES", sheetNames);
  32.             jasperView = new JasperReportsXlsView();
  33.             jasperView.setExporterParameters(exporterParameters);
  34.         } else if (viewFormat.equals("xlsx")) {
  35.             Map exporterParameters = new HashMap();
  36.             exporterParameters.put("net.sf.jasperreports.engine.export.JRXlsExporterParameter.IS_DETECT_CELL_TYPE", true);
  37.             //exporterParameters.put("net.sf.jasperreports.engine.export.JRXlsExporterParameter.IS_COLLAPSE_ROW_SPAN", true);
  38.             exporterParameters.put("net.sf.jasperreports.engine.export.JRXlsExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_COLUMNS", true);
  39.             exporterParameters.put("net.sf.jasperreports.engine.export.JRXlsExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS", true);
  40.             exporterParameters.put("net.sf.jasperreports.engine.export.JRXlsExporterParameter.IS_WHITE_PAGE_BACKGROUND", false);
  41.             exporterParameters.put("net.sf.jasperreports.engine.export.JRXlsExporterParameter.SHEET_NAMES", sheetNames);
  42.             jasperView = new JasperReportsXlsxView();
  43.             jasperView.setExporterParameters(exporterParameters);
  44.         } else {
  45.             jasperView = new JasperReportsPdfView();
  46.         }
  47.  
  48.         // get appContext. required by the view
  49.         WebApplicationContext ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(request.getSession().getServletContext());
  50.  
  51.         // set the appropriate content disposition header.
  52.         Properties headers = new Properties();
  53.         headers.put("Content-Disposition", availableHeaders.get(viewFormat));
  54.  
  55.         // set the relevant jasperView properties
  56.         jasperView.setUrl(url);
  57.         jasperView.setApplicationContext(ctx);
  58.         jasperView.setHeaders(headers);
  59.         // return view
  60.         return jasperView;
  61.     }
RAW Paste Data
Top