Пример #1
0
    public StyleVO(HSSFCellStyle style, Workbook workbook) {
      alignment = style.getAlignment();
      borderBottom = style.getBorderBottom();
      borderLeft = style.getBorderLeft();
      borderRight = style.getBorderRight();
      borderTop = style.getBorderTop();
      bottomBorderColor = style.getBottomBorderColor();
      dataFormat = style.getDataFormat();
      fillBackgroundColor = style.getFillBackgroundColor();
      fillForegroundColor = style.getFillForegroundColor();
      fillPattern = style.getFillPattern();
      fontIndex = style.getFontIndex();

      hidden = style.getHidden();
      indention = style.getIndention();
      leftBorderColor = style.getLeftBorderColor();
      locked = style.getLocked();
      rightBorderColor = style.getRightBorderColor();
      rotation = style.getRotation();
      topBorderColor = style.getTopBorderColor();
      verticalAlignment = style.getVerticalAlignment();
      wrapText = style.getWrapText();

      fontVal = new FontVO(style.getFont(workbook));
    }
Пример #2
0
 /**
  * Создает новый стиль в документе Excel который полностью копирует свойства некоторого исходного
  * стиля этого же документа. Данный метод используется для того чтобы в последствии поменять в
  * новом стиле один или несколько свойств не затрагивая при этом свойства исходного стиля.
  *
  * @param wb документ Excel.
  * @param src стиль ячейки взятый в качестве шаблона.
  * @return созданная копия исходного стиля в этом же документе.
  */
 public static HSSFCellStyle copyStyle(final HSSFWorkbook wb, final HSSFCellStyle src) {
   final HSSFCellStyle dst = wb.createCellStyle();
   dst.setAlignment(src.getAlignment());
   dst.setBorderBottom(src.getBorderBottom());
   dst.setBorderLeft(src.getBorderLeft());
   dst.setBorderRight(src.getBorderRight());
   dst.setBorderTop(src.getBorderTop());
   dst.setBottomBorderColor(src.getBottomBorderColor());
   dst.setDataFormat(src.getDataFormat());
   dst.setFillForegroundColor(src.getFillForegroundColor());
   dst.setFillBackgroundColor(src.getFillBackgroundColor());
   dst.setFillPattern(src.getFillPattern());
   dst.setFont(src.getFont(wb));
   dst.setHidden(src.getHidden());
   dst.setIndention(src.getIndention());
   dst.setLeftBorderColor(src.getLeftBorderColor());
   dst.setLocked(src.getLocked());
   dst.setRightBorderColor(src.getRightBorderColor());
   dst.setRotation(src.getRotation());
   dst.setTopBorderColor(src.getTopBorderColor());
   dst.setVerticalAlignment(src.getVerticalAlignment());
   dst.setWrapText(src.getWrapText());
   return dst;
 }
Пример #3
0
  /** Retrieves the text contents of the file */
  public String getText() {
    StringBuffer text = new StringBuffer();

    // We don't care about the difference between
    //  null (missing) and blank cells
    _wb.setMissingCellPolicy(HSSFRow.RETURN_BLANK_AS_NULL);

    // Process each sheet in turn
    for (int i = 0; i < _wb.getNumberOfSheets(); i++) {
      HSSFSheet sheet = _wb.getSheetAt(i);
      if (sheet == null) {
        continue;
      }

      if (_includeSheetNames) {
        String name = _wb.getSheetName(i);
        if (name != null) {
          text.append(name);
          text.append("\n");
        }
      }

      // Header text, if there is any
      if (_includeHeadersFooters) {
        text.append(_extractHeaderFooter(sheet.getHeader()));
      }

      int firstRow = sheet.getFirstRowNum();
      int lastRow = sheet.getLastRowNum();
      for (int j = firstRow; j <= lastRow; j++) {
        HSSFRow row = sheet.getRow(j);
        if (row == null) {
          continue;
        }

        // Check each cell in turn
        int firstCell = row.getFirstCellNum();
        int lastCell = row.getLastCellNum();
        if (_includeBlankCells) {
          firstCell = 0;
        }

        for (int k = firstCell; k < lastCell; k++) {
          HSSFCell cell = row.getCell(k);
          boolean outputContents = true;

          if (cell == null) {
            // Only output if requested
            outputContents = _includeBlankCells;
          } else {
            switch (cell.getCellType()) {
              case HSSFCell.CELL_TYPE_STRING:
                text.append(cell.getRichStringCellValue().getString());
                break;
              case HSSFCell.CELL_TYPE_NUMERIC:
                text.append(_formatter.formatCellValue(cell));
                break;
              case HSSFCell.CELL_TYPE_BOOLEAN:
                text.append(cell.getBooleanCellValue());
                break;
              case HSSFCell.CELL_TYPE_ERROR:
                text.append(ErrorEval.getText(cell.getErrorCellValue()));
                break;
              case HSSFCell.CELL_TYPE_FORMULA:
                if (!_shouldEvaluateFormulas) {
                  text.append(cell.getCellFormula());
                } else {
                  switch (cell.getCachedFormulaResultType()) {
                    case HSSFCell.CELL_TYPE_STRING:
                      HSSFRichTextString str = cell.getRichStringCellValue();
                      if (str != null && str.length() > 0) {
                        text.append(str.toString());
                      }
                      break;
                    case HSSFCell.CELL_TYPE_NUMERIC:
                      HSSFCellStyle style = cell.getCellStyle();
                      if (style == null) {
                        text.append(cell.getNumericCellValue());
                      } else {
                        text.append(
                            _formatter.formatRawCellContents(
                                cell.getNumericCellValue(),
                                style.getDataFormat(),
                                style.getDataFormatString()));
                      }
                      break;
                    case HSSFCell.CELL_TYPE_BOOLEAN:
                      text.append(cell.getBooleanCellValue());
                      break;
                    case HSSFCell.CELL_TYPE_ERROR:
                      text.append(ErrorEval.getText(cell.getErrorCellValue()));
                      break;
                  }
                }
                break;
              default:
                throw new RuntimeException("Unexpected cell type (" + cell.getCellType() + ")");
            }

            // Output the comment, if requested and exists
            HSSFComment 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 " + comment.getAuthor() + ": " + commentText);
            }
          }

          // Output a tab if we're not on the last cell
          if (outputContents && k < (lastCell - 1)) {
            text.append("\t");
          }
        }

        // Finish off the row
        text.append("\n");
      }

      // Finally Footer text, if there is any
      if (_includeHeadersFooters) {
        text.append(_extractHeaderFooter(sheet.getFooter()));
      }
    }

    return text.toString();
  }