コード例 #1
0
 /**
  * Returns the contents of the cell, with no formatting applied
  *
  * @return
  */
 String unformattedContents() {
   switch (currentCell.getType()) {
     case "s": // string stored in shared table
       int idx = Integer.parseInt(lastContents);
       return new XSSFRichTextString(sst.getEntryAt(idx)).toString();
     case "inlineStr": // inline string (not in sst)
       return new XSSFRichTextString(lastContents).toString();
     default:
       return lastContents;
   }
 }
コード例 #2
0
  /**
   * Read the numeric format string out of the styles table for this cell. Stores the result in the
   * Cell.
   *
   * @param startElement
   * @param cell
   */
  void setFormatString(StartElement startElement, StreamingCell cell) {
    Attribute cellStyle = startElement.getAttributeByName(new QName("s"));
    String cellStyleString = (cellStyle != null) ? cellStyle.getValue() : null;
    XSSFCellStyle style = null;

    if (cellStyleString != null) {
      style = stylesTable.getStyleAt(Integer.parseInt(cellStyleString));
    } else if (stylesTable.getNumCellStyles() > 0) {
      style = stylesTable.getStyleAt(0);
    }

    if (style != null) {
      cell.setNumericFormatIndex(style.getDataFormat());
      String formatString = style.getDataFormatString();

      if (formatString != null) {
        cell.setNumericFormat(formatString);
      } else {
        cell.setNumericFormat(BuiltinFormats.getBuiltinFormat(cell.getNumericFormatIndex()));
      }
    } else {
      cell.setNumericFormatIndex(null);
      cell.setNumericFormat(null);
    }
  }
コード例 #3
0
 /**
  * Tries to format the contents of the last contents appropriately based on the type of cell and
  * the discovered numeric format.
  *
  * @return
  */
 String formattedContents() {
   switch (currentCell.getType()) {
     case "s": // string stored in shared table
       int idx = Integer.parseInt(lastContents);
       return new XSSFRichTextString(sst.getEntryAt(idx)).toString();
     case "inlineStr": // inline string (not in sst)
       return new XSSFRichTextString(lastContents).toString();
     case "str": //
       return lastContents;
     case "e": // error type
       return StringUtils.EMPTY; // "ERROR:  " + lastContents;
     case "n": // numeric type
       if (currentCell.getNumericFormat() != null && lastContents.length() > 0) {
         return dataFormatter.formatRawCellContents(
             Double.parseDouble(lastContents),
             currentCell.getNumericFormatIndex(),
             currentCell.getNumericFormat());
       } else {
         return lastContents;
       }
     default:
       return lastContents;
   }
 }
コード例 #4
0
  /**
   * Handles a Stream event.
   *
   * @param event
   * @throws SAXException
   */
  private void handleEvent(XMLEvent event) throws SAXException {
    if (event.getEventType() == XMLStreamConstants.CHARACTERS) {
      Characters c = event.asCharacters();
      lastContents += c.getData();
    } else if (event.getEventType() == XMLStreamConstants.START_ELEMENT) {
      StartElement startElement = event.asStartElement();
      String tagLocalName = startElement.getName().getLocalPart();

      if ("row".equals(tagLocalName)) {
        Attribute rowIndex = startElement.getAttributeByName(new QName("r"));
        if (firstRowIndex == -1) {
          firstRowIndex = Integer.parseInt(rowIndex.getValue());
        }
        currentRow = new StreamingRow(Integer.parseInt(rowIndex.getValue()) - 1);
      } else if ("cols".equals(tagLocalName)) {
        parsingCols = true;
      } else if ("col".equals(tagLocalName) && parsingCols) {
        colNumber = colNumber + 1;
      } else if ("c".equals(tagLocalName)) {
        Attribute ref = startElement.getAttributeByName(new QName("r"));

        String[] coord = ref.getValue().split("(?<=\\D)(?=\\d)|(?<=\\d)(?=\\D)");
        currentCell =
            new StreamingCell(
                CellReference.convertColStringToIndex(coord[0]), Integer.parseInt(coord[1]) - 1);
        setFormatString(startElement, currentCell);

        Attribute type = startElement.getAttributeByName(new QName("t"));
        if (type != null) {
          currentCell.setType(type.getValue());
        } else {
          currentCell.setType("n");
        }

        Attribute style = startElement.getAttributeByName(new QName("s"));
        if (style != null) {
          String indexStr = style.getValue();
          try {
            int index = Integer.parseInt(indexStr);
            currentCell.setCellStyle(stylesTable.getStyleAt(index));
          } catch (NumberFormatException nfe) {
            LOGGER.warn("Ignoring invalid style index {}", indexStr);
          }
        }
        // we store the dimension as well to revert with this method when cols not found
        // can happen see xlsx attached here https://jira.talendforge.org/browse/TDP-1957
        // <dimension ref="A1:B60"/>
      } else if ("dimension".equals(tagLocalName)) {
        Attribute attribute = startElement.getAttributeByName(new QName("ref"));
        if (attribute != null) {
          this.dimension = attribute.getValue();
        }
      }

      // Clear contents cache
      lastContents = "";
    } else if (event.getEventType() == XMLStreamConstants.END_ELEMENT) {
      EndElement endElement = event.asEndElement();
      String tagLocalName = endElement.getName().getLocalPart();

      if ("v".equals(tagLocalName) || "t".equals(tagLocalName)) {
        currentCell.setRawContents(unformattedContents());
        currentCell.setContents(formattedContents());
      } else if ("row".equals(tagLocalName) && currentRow != null) {
        rowCache.add(currentRow);
      } else if ("c".equals(tagLocalName)) {
        currentRow.getCellMap().put(currentCell.getColumnIndex(), currentCell);
      } else if ("cols".equals(tagLocalName)) {
        parsingCols = false;
      }
    }
  }