Package org.apache.poi.xssf.usermodel

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


            if (!xpath.matches(".*\\[.*")) {

                // Exports elements and attributes mapped with simpleXmlCell
                if (simpleXmlCell!=null) {
                    XSSFCell cell = simpleXmlCell.getReferencedCell();
                    if (cell!=null) {
                        Node currentNode = getNodeByXPath(xpath,doc.getFirstChild(),doc,false);
                        STXmlDataType.Enum dataType = simpleXmlCell.getXmlDataType();
                        mapCellOnNode(cell,currentNode,dataType);
                       
                        //remove nodes which are empty in order to keep the output xml valid
                        if("".equals(currentNode.getTextContent()) && currentNode.getParentNode() != null) {
                          currentNode.getParentNode().removeChild(currentNode);
                        }
                    }
                }

                // Exports elements and attributes mapped with tables
                if (table!=null) {

                    List<XSSFXmlColumnPr> tableColumns = table.getXmlColumnPrs();

                    XSSFSheet sheet = table.getXSSFSheet();

                    int startRow = table.getStartCellReference().getRow();
                    // In mappings created with Microsoft Excel the first row contains the table header and must be skipped
                    startRow +=1;

                    int endRow = table.getEndCellReference().getRow();

                    for(int i = startRow; i<= endRow; i++) {
                        XSSFRow row = sheet.getRow(i);

                        Node tableRootNode = getNodeByXPath(table.getCommonXpath(),doc.getFirstChild(),doc,true);

                        short startColumnIndex = table.getStartCellReference().getCol();
                        for(int j = startColumnIndex; j<= table.getEndCellReference().getCol();j++) {
                            XSSFCell cell = row.getCell(j);
                            if (cell!=null) {
                                XSSFXmlColumnPr pointer = tableColumns.get(j-startColumnIndex);
                                String localXPath = pointer.getLocalXPath();
                                Node currentNode = getNodeByXPath(localXPath,tableRootNode,doc,false);
                                STXmlDataType.Enum dataType = pointer.getXmlDataType();
View Full Code Here


        assertEquals(8, cellFill.getPatternType().intValue());
    }

    public void testColorFromTheme() {
        XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("styles.xlsx");
        XSSFCell cellWithThemeColor = wb.getSheetAt(0).getRow(10).getCell(0);
        //color RGB will be extracted from theme
        XSSFColor foregroundColor = cellWithThemeColor.getCellStyle().getFillForegroundXSSFColor();
        byte[] rgb = foregroundColor.getRgb();
        byte[] rgbWithTint = foregroundColor.getRgbWithTint();
        // Dk2
        assertEquals(rgb[0],31);
        assertEquals(rgb[1],73);
View Full Code Here

            sCell.setCellValue(str);
            assertEquals(sCell.getStringCellValue(), str);

            // read back as XSSF and check that xml:spaces="preserve" is set
            XSSFWorkbook xwb = (XSSFWorkbook)SXSSFITestDataProvider.instance.writeOutAndReadBack(swb);
            XSSFCell xCell = xwb.getSheetAt(0).getRow(0).getCell(0);

            CTRst is = xCell.getCTCell().getIs();
            XmlCursor c = is.newCursor();
            c.toNextToken();
            String t = c.getAttributeText(new QName("http://www.w3.org/XML/1998/namespace", "space"));
            c.dispose();
            assertEquals("expected xml:spaces=\"preserve\" \"" + str + "\"", "preserve", t);
View Full Code Here

        }
    }

    private void updateRowFormulas(XSSFRow row, FormulaShifter shifter) {
        for (Cell c : row) {
            XSSFCell cell = (XSSFCell) c;

            CTCell ctCell = cell.getCTCell();
            if (ctCell.isSetF()) {
                CTCellFormula f = ctCell.getF();
                String formula = f.getStringValue();
                if (formula.length() > 0) {
                    String shiftedFormula = shiftFormula(row, formula, shifter);
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

        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 ERROR:
        if (cell.getValue() != null) {
          Object value = cell.getValue();
          c.setCellType(org.apache.poi.ss.usermodel.Cell.CELL_TYPE_ERROR);
          c.setCellValue(value.toString());
        } 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

          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) {
View Full Code Here

                        xhtml.characters(
                            formatter.formatRawCellContents(cell.getNumericCellValue(),
                                                            style.getDataFormat(),
                                                            style.getDataFormatString()));
                    } else {
                        XSSFCell xc = (XSSFCell) cell;
                        String rawValue = xc.getRawValue();
                        if (rawValue != null) {
                            xhtml.characters(rawValue);
                        }

                    }
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.