コード例 #1
0
  /** Retreives the text contents of the file */
  public String getText() {
    StringBuffer text = new StringBuffer();

    for (int i = 0; i < workbook.getNumberOfSheets(); i++) {
      XSSFSheet sheet = workbook.getSheetAt(i);
      if (includeSheetNames) {
        text.append(workbook.getSheetName(i)).append("\n");
      }

      // Header(s), if present
      if (includeHeadersFooters) {
        text.append(extractHeaderFooter(sheet.getFirstHeader()));
        text.append(extractHeaderFooter(sheet.getOddHeader()));
        text.append(extractHeaderFooter(sheet.getEvenHeader()));
      }

      // Rows and cells
      for (Object rawR : sheet) {
        Row row = (Row) rawR;
        for (Iterator<Cell> ri = row.cellIterator(); ri.hasNext(); ) {
          Cell cell = ri.next();

          // Is it a formula one?
          if (cell.getCellType() == Cell.CELL_TYPE_FORMULA && formulasNotResults) {
            text.append(cell.getCellFormula());
          } else if (cell.getCellType() == Cell.CELL_TYPE_STRING) {
            text.append(cell.getRichStringCellValue().getString());
          } else {
            XSSFCell xc = (XSSFCell) cell;
            text.append(xc.getRawValue());
          }

          // Output the comment, if requested and exists
          Comment comment = cell.getCellComment();
          if (includeCellComments && comment != null) {
            // Replace any newlines with spaces, otherwise it
            //  breaks the output
            String commentText = comment.getString().getString().replace('\n', ' ');
            text.append(" Comment by ")
                .append(comment.getAuthor())
                .append(": ")
                .append(commentText);
          }

          if (ri.hasNext()) text.append("\t");
        }
        text.append("\n");
      }

      // Finally footer(s), if present
      if (includeHeadersFooters) {
        text.append(extractHeaderFooter(sheet.getFirstFooter()));
        text.append(extractHeaderFooter(sheet.getOddFooter()));
        text.append(extractHeaderFooter(sheet.getEvenFooter()));
      }
    }

    return text.toString();
  }
コード例 #2
0
  protected SCell importCell(Cell poiCell, int row, SSheet sheet) {

    SCell cell = sheet.getCell(row, poiCell.getColumnIndex());
    cell.setCellStyle(importCellStyle(poiCell.getCellStyle()));

    switch (poiCell.getCellType()) {
      case Cell.CELL_TYPE_NUMERIC:
        cell.setNumberValue(poiCell.getNumericCellValue());
        break;
      case Cell.CELL_TYPE_STRING:
        RichTextString poiRichTextString = poiCell.getRichStringCellValue();
        if (poiRichTextString != null && poiRichTextString.numFormattingRuns() > 0) {
          SRichText richText = cell.setupRichTextValue();
          importRichText(poiCell, poiRichTextString, richText);
        } else {
          cell.setStringValue(poiCell.getStringCellValue());
        }
        break;
      case Cell.CELL_TYPE_BOOLEAN:
        cell.setBooleanValue(poiCell.getBooleanCellValue());
        break;
      case Cell.CELL_TYPE_FORMULA:
        cell.setFormulaValue(poiCell.getCellFormula());
        // ZSS-873
        if (isImportCache() && !poiCell.isCalcOnLoad() && !mustCalc(cell)) {
          ValueEval val = null;
          switch (poiCell.getCachedFormulaResultType()) {
            case Cell.CELL_TYPE_NUMERIC:
              val = new NumberEval(poiCell.getNumericCellValue());
              break;
            case Cell.CELL_TYPE_STRING:
              RichTextString poiRichTextString0 = poiCell.getRichStringCellValue();
              if (poiRichTextString0 != null && poiRichTextString0.numFormattingRuns() > 0) {
                SRichText richText = new RichTextImpl();
                importRichText(poiCell, poiRichTextString0, richText);
                val = new StringEval(richText.getText());
              } else {
                val = new StringEval(poiCell.getStringCellValue());
              }
              break;
            case Cell.CELL_TYPE_BOOLEAN:
              val = BoolEval.valueOf(poiCell.getBooleanCellValue());
              break;
            case Cell.CELL_TYPE_ERROR:
              val = ErrorEval.valueOf(poiCell.getErrorCellValue());
              break;
            case Cell.CELL_TYPE_BLANK:
            default:
              // do nothing
          }
          if (val != null) {
            ((AbstractCellAdv) cell).setFormulaResultValue(val);
          }
        }
        break;
      case Cell.CELL_TYPE_ERROR:
        cell.setErrorValue(PoiEnumConversion.toErrorCode(poiCell.getErrorCellValue()));
        break;
      case Cell.CELL_TYPE_BLANK:
        // do nothing because spreadsheet model auto creates blank cells
      default:
        // TODO log: leave an unknown cell type as a blank cell.
        break;
    }

    Hyperlink poiHyperlink = poiCell.getHyperlink();
    if (poiHyperlink != null) {
      String addr = poiHyperlink.getAddress();
      String label = poiHyperlink.getLabel();
      SHyperlink hyperlink =
          cell.setupHyperlink(
              PoiEnumConversion.toHyperlinkType(poiHyperlink.getType()),
              addr == null ? "" : addr,
              label == null ? "" : label);
      cell.setHyperlink(hyperlink);
    }

    Comment poiComment = poiCell.getCellComment();
    if (poiComment != null) {
      SComment comment = cell.setupComment();
      comment.setAuthor(poiComment.getAuthor());
      comment.setVisible(poiComment.isVisible());
      RichTextString poiRichTextString = poiComment.getString();
      if (poiRichTextString != null && poiRichTextString.numFormattingRuns() > 0) {
        importRichText(poiCell, poiComment.getString(), comment.setupRichText());
      } else {
        comment.setText(poiComment.toString());
      }
    }

    return cell;
  }