/** 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 CellValue getCellValueAt(int index) {
   if (index < 0 || index >= numOfCells) {
     throw new IndexOutOfBoundsException(
         "Index must be between 0 and " + (numOfCells - 1) + " (inclusive), given: " + index);
   }
   int firstRow = cellRangeAddress.getFirstRow();
   int firstCol = cellRangeAddress.getFirstColumn();
   int lastCol = cellRangeAddress.getLastColumn();
   int width = lastCol - firstCol + 1;
   int rowIndex = firstRow + index / width;
   int cellIndex = firstCol + index % width;
   Row row = sheet.getRow(rowIndex);
   return (row == null) ? null : evaluator.evaluate(row.getCell(cellIndex));
 }
  protected SRow importRow(Row poiRow, SSheet sheet) {
    SRow row = sheet.getRow(poiRow.getRowNum());
    row.setHeight(UnitUtil.twipToPx(poiRow.getHeight()));
    row.setCustomHeight(poiRow.isCustomHeight());
    row.setHidden(poiRow.getZeroHeight());
    CellStyle rowStyle = poiRow.getRowStyle();
    if (rowStyle != null) {
      row.setCellStyle(importCellStyle(rowStyle));
    }

    for (Cell poiCell : poiRow) {
      importCell(poiCell, poiRow.getRowNum(), sheet);
    }

    return row;
  }