Package org.apache.poi.xssf.usermodel

Examples of org.apache.poi.xssf.usermodel.XSSFCell


        styleBlack.setBorderRight(CellStyle.BORDER_THIN);
        styleBlack.setBorderTop(CellStyle.BORDER_THIN);
        styleBlack.setBorderBottom(CellStyle.BORDER_THIN);
    //写入标题
    for (int i = 0; i < title.size(); i++) {
      XSSFCell cell=row.createCell(i, XSSFCell.CELL_TYPE_STRING);
      cell.setCellValue(title.get(i));
      cell.setCellStyle(styletitle);
    }
   
    // 写入内容行
    for (int i = 0; i < datas.size(); i++) {
      String[] rowvalue = datas.get(i);
      XSSFRow nextrow = sheet.createRow(sheet.getLastRowNum() + 1);// 创建一行
      for (int j = 0; j < rowvalue.length; j++) {
        XSSFCell cell = nextrow
            .createCell(j, XSSFCell.CELL_TYPE_STRING);
        cell.setCellValue(new XSSFRichTextString(rowvalue[j]));
        if (rowvalue[j].equals("Fail")) {
          cell.setCellStyle(styleRed);
        } else if (rowvalue[j].equals("Pass")) {
          cell.setCellStyle(styleGreen);
        } else {
          cell.setCellStyle(styleBlack);
        }

      }
    }
   
View Full Code Here


      this.handleCell(workbook, r, cell, cellNum++);
    }
  }

  private void handleCell(final XSSFWorkbook workbook, final XSSFRow row, final Cell cell, final int cellNum) {
    XSSFCell c = row.createCell(cellNum);

    XSSFFont font = workbook.createFont();
    font.setColor(this.getColor(cell.getFontColor()));
    font.setFontName(cell.getFont());
    font.setFontHeightInPoints((short) cell.getFontSize());

    if (cell.isBold()) {
      font.setBoldweight(Font.BOLDWEIGHT_BOLD);
    }

    if (cell.isItalic()) {
      font.setItalic(true);
    }

    if (cell.isStrikeout()) {
      font.setStrikeout(true);
    }

    if (cell.isUnderline()) {
      font.setUnderline(Font.U_SINGLE);
    }

    XSSFCellStyle style = workbook.createCellStyle();
    style.setAlignment(this.getAlignment(cell.getAlignment()));
    style.setVerticalAlignment(this.getVerticalAlignment(cell.getVerticalAlignment()));
    style.setWrapText(cell.isWrap());
    style.setFont(font);

    if (cell.getLeftBorder() != null) {
      style.setBorderLeft((short) cell.getLeftBorder().getWidth());
      style.setLeftBorderColor(this.getColor(cell.getLeftBorder().getColor()));
    }

    if (cell.getTopBorder() != null) {
      style.setBorderTop((short) cell.getTopBorder().getWidth());
      style.setTopBorderColor(this.getColor(cell.getTopBorder().getColor()));
    }

    if (cell.getRightBorder() != null) {
      style.setBorderRight((short) cell.getRightBorder().getWidth());
      style.setRightBorderColor(this.getColor(cell.getRightBorder().getColor()));
    }

    if (cell.getBottomBorder() != null) {
      style.setBorderBottom((short) cell.getBottomBorder().getWidth());
      style.setBottomBorderColor(this.getColor(cell.getBottomBorder().getColor()));
    }

    if (cell.getBackgroundColor() != null) {
      style.setFillForegroundColor(this.getBackgroundColor(cell.getBackgroundColor()));
      style.setFillPattern(CellStyle.SOLID_FOREGROUND);
    }

    switch (cell.getType()) {
      case BLANK:
        c.setCellType(org.apache.poi.ss.usermodel.Cell.CELL_TYPE_BLANK);
        c.setCellValue("");
        break;
      case BOOLEAN:
        if (cell.getValue() != null) {
          Object value = cell.getValue();
          Boolean bool = null;
          if (value instanceof Boolean) {
            bool = (Boolean) cell.getValue();
          } else {
            bool = Boolean.valueOf(value.toString());
          }
          c.setCellType(org.apache.poi.ss.usermodel.Cell.CELL_TYPE_BOOLEAN);
          c.setCellValue(bool.booleanValue());
        } else {
          c.setCellType(org.apache.poi.ss.usermodel.Cell.CELL_TYPE_BOOLEAN);
          c.setCellValue("");
        }
        break;
      case FORMULA:
        if (cell.getValue() != null) {
          String formula = cell.getValue().toString();
          c.setCellType(org.apache.poi.ss.usermodel.Cell.CELL_TYPE_FORMULA);
          c.setCellValue(formula);
        } else {
          c.setCellType(org.apache.poi.ss.usermodel.Cell.CELL_TYPE_FORMULA);
          c.setCellValue("");
        }
        break;
      case NUMERIC:
        if (cell.getValue() != null) {
          Object value = cell.getValue();
          Number number = null;
          if (value instanceof Number) {
            number = (Number) cell.getValue();
          } else {
            number = new BigDecimal(value.toString());
          }
          c.setCellType(org.apache.poi.ss.usermodel.Cell.CELL_TYPE_NUMERIC);
          c.setCellValue(number.doubleValue());
        } else {
          c.setCellType(org.apache.poi.ss.usermodel.Cell.CELL_TYPE_NUMERIC);
          c.setCellValue("");
        }
        break;
      case TEXT:
        if (cell.getValue() != null) {
          XSSFRichTextString value = new XSSFRichTextString(cell.getValue().toString());
          c.setCellValue(value);
        } else {
          c.setCellType(org.apache.poi.ss.usermodel.Cell.CELL_TYPE_STRING);
          c.setCellValue("");
        }
        break;
      default:
        c.setCellType(org.apache.poi.ss.usermodel.Cell.CELL_TYPE_STRING);
        c.setCellValue("");
        break;
    }

    c.setCellStyle(style);
  }
View Full Code Here

                  if (row != null)
                  {
                     int lastcell = row.getLastCellNum();
                     for (int k = 0; k < lastcell; k++)
                     {
                        XSSFCell cell = row.getCell(k);
                        if (cell != null)
                        {
                           switch (cell.getCellType())
                           {
                              case XSSFCell.CELL_TYPE_NUMERIC : {
                                 double d = cell.getNumericCellValue();
                                 if (isCellDateFormatted(cell))
                                 {
                                    Date date = HSSFDateUtil.getJavaDate(d);
                                    String cellText = dateFormat.format(date);
                                    builder.append(cellText).append(" ");
                                 }
                                 else
                                 {
                                    builder.append(d).append(" ");
                                 }
                                 break;
                              }
                              case XSSFCell.CELL_TYPE_FORMULA :
                                 builder.append(cell.getCellFormula().toString()).append(" ");
                                 break;
                              case XSSFCell.CELL_TYPE_BOOLEAN :
                                 builder.append(cell.getBooleanCellValue()).append(" ");
                                 break;
                              case XSSFCell.CELL_TYPE_ERROR :
                                 builder.append(cell.getErrorCellValue()).append(" ");
                                 break;
                              case XSSFCell.CELL_TYPE_STRING :
                                 builder.append(cell.getStringCellValue().toString()).append(" ");
                                 break;
                              default :
                                 break;
                           }
                        }
View Full Code Here

TOP

Related Classes of org.apache.poi.xssf.usermodel.XSSFCell

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.