Beispiel #1
0
  private static byte[] getCsv(CellDataSet table, String delimiter, String enclosing) {
    if (table != null) {
      AbstractBaseCell[][] rowData = table.getCellSetBody();
      AbstractBaseCell[][] rowHeader = table.getCellSetHeaders();

      boolean offset = rowHeader.length > 0;
      String[][] result = new String[(offset ? 1 : 0) + rowData.length][];
      if (offset) {
        List<String> cols = new ArrayList<String>();
        for (int x = 0; x < rowHeader[0].length; x++) {
          String col = null;
          for (int y = rowHeader.length - 1; y >= 0; y--) {
            String value = rowHeader[y][x].getFormattedValue();
            if (value == null || "null".equals(value)) // $NON-NLS-1$
            {
              value = ""; // $NON-NLS-1$
            }
            if (col == null && StringUtils.isNotBlank(value)) {
              col = value;
            } else if (col != null && StringUtils.isNotBlank(value)) {
              col = value + "/" + col;
            }
          }
          cols.add(enclosing + col + enclosing);
        }
        result[0] = cols.toArray(new String[cols.size()]);
      }
      String[] lastKnownHeader = null;
      for (int x = 0; x < rowData.length; x++) {
        int xTarget = (offset ? 1 : 0) + x;
        if (lastKnownHeader == null) {
          lastKnownHeader = new String[rowData[x].length];
        }
        List<String> cols = new ArrayList<String>();
        for (int y = 0; y < rowData[x].length; y++) {
          String value = rowData[x][y].getFormattedValue();
          if (!SaikuProperties.webExportCsvUseFormattedValue) {
            if (rowData[x][y] instanceof DataCell
                && ((DataCell) rowData[x][y]).getRawNumber() != null) {
              value = ((DataCell) rowData[x][y]).getRawNumber().toString();
            }
          }
          if (rowData[x][y] instanceof MemberCell
              && StringUtils.isNotBlank(value)
              && !"null".equals(value)) {
            lastKnownHeader[y] = value;
          } else if (rowData[x][y] instanceof MemberCell
              && (StringUtils.isBlank(value) || "null".equals(value))) {
            value = (StringUtils.isNotBlank(lastKnownHeader[y]) ? lastKnownHeader[y] : null);
          }

          if (value == null || "null".equals(value)) {
            value = "";
          }
          value = value.replace("\"", "\"\"");
          value = enclosing + value + enclosing;
          cols.add(value);
        }
        result[xTarget] = cols.toArray(new String[cols.size()]);
      }
      return export(result, delimiter);
    }
    return new byte[0];
  }