コード例 #1
0
 private static Class resolveType(PropertyDescriptor d) {
   if (TypeResolver.isIntegerType(d.getPropertyType())) {
     return Integer.class;
   } else if (TypeResolver.isLongType(d.getPropertyType())) {
     return Long.class;
   } else if (TypeResolver.isBooleanType(d.getPropertyType())) {
     return Boolean.class;
   } else {
     return d.getPropertyType();
   }
 }
コード例 #2
0
  @SuppressWarnings("unchecked")
  public byte[] export(
      String title, List<ClearingItem> exportList, List<PropertyDescriptor> props) {

    ByteArrayOutputStream out = new ByteArrayOutputStream();

    if (exportList == null || exportList.size() == 0) {
      return new byte[0];
    }

    try {
      Object bean = exportList.get(0);
      BeanInfo infoTo = Introspector.getBeanInfo(bean.getClass());
      PropertyDescriptor[] d = infoTo.getPropertyDescriptors();

      if (props == null || props.isEmpty()) {

        props = new ArrayList<PropertyDescriptor>();

        for (int i = 0; i < d.length; i++) {
          try {

            Class pType = d[i].getPropertyType();

            if (pType.isAssignableFrom(Class.class)) {
              continue;
            }

            if (TypeResolver.isBooleanType(pType)) {
              // ok
            } else if (TypeResolver.isPrimitiveType(pType)) {
              // ok
            } else if (TypeResolver.isDateType(pType)) {
              // ok
            } else if (TypeResolver.isEnumType(pType)) {
              // ok
            } else {
              log.warning("Skip type " + pType);
              continue;
            }

            if (d[i].getName().equals("className")
                || d[i].getName().equals("id")
                || d[i].getName().equals("version")) {
              continue;
            }

            props.add(d[i]);
          } catch (Exception ex) {
            log.severe(ex.getMessage());
            continue;
          }
        }
      }

      Map<String, String> parameters = new HashMap<String, String>();
      parameters.put("ReportTitle", title);

      JasperDesign design = getJasperDesign(props);

      JasperReport jasperReport = JasperCompileManager.compileReport(design);

      JasperPrint jasperPrint = null;

      jasperPrint =
          JasperFillManager.fillReport(
              jasperReport, parameters, new JRBeanCollectionDataSource(exportList));

      JRPdfExporter exporter = new JRPdfExporter();

      exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
      exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, out);
      exporter.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.TRUE);
      exporter.setParameter(
          JRXlsExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS, Boolean.TRUE);
      exporter.setParameter(JRXlsExporterParameter.IS_WHITE_PAGE_BACKGROUND, Boolean.FALSE);

      // writeHeader(response, exportName);

      exporter.exportReport();

      byte[] ret = out.toByteArray();

      out.close();

      return ret;

    } catch (Throwable t) {
      log.log(Level.INFO, t.getMessage(), t);
      return null;
    }
  }