Пример #1
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);
    }
  }
Пример #2
0
 @Override
 public String getNamespaceURI(String prefix) {
   return delegate.getNamespaceURI(prefix);
 }
Пример #3
0
 @Override
 public NamespaceContext getNamespaceContext() {
   return delegate.getNamespaceContext();
 }
Пример #4
0
 @Override
 public Iterator getNamespaces() {
   return delegate.getNamespaces();
 }
Пример #5
0
 @Override
 public QName getName() {
   return delegate.getName();
 }
Пример #6
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;
      }
    }
  }