Package org.apache.poi.ss.usermodel

Examples of org.apache.poi.ss.usermodel.CellStyle


  public CellStyle createCellStyle(final InstanceID id, final StyleSheet element, final CellBackground bg)
  {
    if (id == null)
    {
      CellStyle cellStyle = backgroundCache.get(bg);
      if (cellStyle != null)
      {
        return cellStyle;
      }
    }
    else
    {
      CellStyle cellStyle = contentCache.get(new CacheKey(id, bg));
      if (cellStyle != null)
      {
        return cellStyle;
      }
    }

    CellStyle cellStyle = backend.createCellStyle(id, element, bg);
    if (cellStyle == null)
    {
      return null;
    }
    if (id == null)
View Full Code Here


   *@param propertyValue The value of the property that is to be changed.
   *@param cell The cell that needs it's style changes
   */
  public static void setCellStyleProperty(Cell cell, Workbook workbook, String propertyName,
      Object propertyValue) {
    CellStyle originalStyle = cell.getCellStyle();
    CellStyle newStyle = null;
    Map<String, Object> values = getFormatProperties(originalStyle);
    values.put(propertyName, propertyValue);

    // index seems like what index the cellstyle is in the list of styles for a workbook.
    // not good to compare on!
    short numberCellStyles = workbook.getNumCellStyles();

    for (short i = 0; i < numberCellStyles; i++) {
      CellStyle wbStyle = workbook.getCellStyleAt(i);
      Map<String, Object> wbStyleMap = getFormatProperties(wbStyle);

      if (wbStyleMap.equals(values)) {
        newStyle = wbStyle;
        break;
View Full Code Here

       // Check the current ones
       assertEquals(0, normal.getCellStyle().getRotation());
       assertEquals(0xff, rotated.getCellStyle().getRotation());
      
       // Add a new style, also rotated
       CellStyle cs = wb.createCellStyle();
       cs.setRotation((short)0xff);
       Cell nc = r.createCell(2);
       nc.setCellValue("New Rotated Text");
       nc.setCellStyle(cs);
       assertEquals(0xff, nc.getCellStyle().getRotation());
      
View Full Code Here

  protected void extractColourStyle(Cell c, StringBuilder out) {
    if (c == null) {
      return;
    }
       
    CellStyle style = c.getCellStyle();
    XSSFColor fgColor = ((XSSFFont) getFont(c)).getXSSFColor();
   
    if ((fgColor != null) && (fgColor.getIndexed() != defaultFGColor)) {
      convertColour(fgColor, "color", out);     
    }
   
    XSSFColor bgColor = (XSSFColor) style.getFillForegroundColorColor();
    if ((bgColor != null) && (bgColor.getIndexed() != defaultBGColor)) {
      convertColour(bgColor, "background-color", out);
    }
  }
View Full Code Here

      out.append("font-weight: bold; ");
    }
  }
 
  protected Font getFont(Cell c) {
    CellStyle style = c.getCellStyle();
    short fontIndex = style.getFontIndex();
    Sheet s = c.getSheet();
    Font f = s.getWorkbook().getFontAt(fontIndex);
    return f;
  }
View Full Code Here

                value = null;
              }
            }
            else if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC)
            {
              final CellStyle hssfCellStyle = cell.getCellStyle();
              final short dataFormat = hssfCellStyle.getDataFormat();
              final String dataFormatString = hssfCellStyle.getDataFormatString();
              if (isDateFormat(dataFormat, dataFormatString))
              {
                value = cell.getDateCellValue();
              }
              else
View Full Code Here

                cell = row.getCell(region.getFirstColumn());
                colspan = 1 + region.getLastColumn() - region.getFirstColumn();
            }
        }

        CellStyle style = cell.getCellStyle();
        int cellType = cell.getCellType();

        // for formula cells we compute the cell width for the cached formula result
        if(cellType == Cell.CELL_TYPE_FORMULA) cellType = cell.getCachedFormulaResultType();

        Font font = wb.getFontAt(style.getFontIndex());

        AttributedString str;
        TextLayout layout;

        double width = -1;
        if (cellType == Cell.CELL_TYPE_STRING) {
            RichTextString rt = cell.getRichStringCellValue();
            String[] lines = rt.getString().split("\\n");
            for (int i = 0; i < lines.length; i++) {
                String txt = lines[i] + defaultChar;

                str = new AttributedString(txt);
                copyAttributes(font, str, 0, txt.length());

                if (rt.numFormattingRuns() > 0) {
                    // TODO: support rich text fragments
                }

                layout = new TextLayout(str.getIterator(), fontRenderContext);
                if(style.getRotation() != 0){
                    /*
                     * Transform the text using a scale so that it's height is increased by a multiple of the leading,
                     * and then rotate the text before computing the bounds. The scale results in some whitespace around
                     * the unrotated top and bottom of the text that normally wouldn't be present if unscaled, but
                     * is added by the standard Excel autosize.
                     */
                    AffineTransform trans = new AffineTransform();
                    trans.concatenate(AffineTransform.getRotateInstance(style.getRotation()*2.0*Math.PI/360.0));
                    trans.concatenate(
                    AffineTransform.getScaleInstance(1, fontHeightMultiple)
                    );
                    width = Math.max(width, ((layout.getOutline(trans).getBounds().getWidth() / colspan) / defaultCharWidth) + cell.getCellStyle().getIndention());
                } else {
                    width = Math.max(width, ((layout.getBounds().getWidth() / colspan) / defaultCharWidth) + cell.getCellStyle().getIndention());
                }
            }
        } else {
            String sval = null;
            if (cellType == Cell.CELL_TYPE_NUMERIC) {
                // Try to get it formatted to look the same as excel
                try {
                    sval = formatter.formatCellValue(cell, dummyEvaluator);
                } catch (Exception e) {
                    sval = String.valueOf(cell.getNumericCellValue());
                }
            } else if (cellType == Cell.CELL_TYPE_BOOLEAN) {
                sval = String.valueOf(cell.getBooleanCellValue()).toUpperCase();
            }
            if(sval != null) {
                String txt = sval + defaultChar;
                str = new AttributedString(txt);
                copyAttributes(font, str, 0, txt.length());

                layout = new TextLayout(str.getIterator(), fontRenderContext);
                if(style.getRotation() != 0){
                    /*
                     * Transform the text using a scale so that it's height is increased by a multiple of the leading,
                     * and then rotate the text before computing the bounds. The scale results in some whitespace around
                     * the unrotated top and bottom of the text that normally wouldn't be present if unscaled, but
                     * is added by the standard Excel autosize.
                     */
                    AffineTransform trans = new AffineTransform();
                    trans.concatenate(AffineTransform.getRotateInstance(style.getRotation()*2.0*Math.PI/360.0));
                    trans.concatenate(
                    AffineTransform.getScaleInstance(1, fontHeightMultiple)
                    );
                    width = Math.max(width, ((layout.getOutline(trans).getBounds().getWidth() / colspan) / defaultCharWidth) + cell.getCellStyle().getIndention());
                } else {
View Full Code Here

                }
   
                Cell cell = row.createCell(2);
   
                cell.setCellValue(date);
                CellStyle cellStyle = row.getSheet().getWorkbook().createCellStyle();
   
                String javaDateFormatPattern = ((SimpleDateFormat)dateFormat).toPattern();
                String excelFormatPattern = DateFormatConverter.convert(locale, javaDateFormatPattern);
   
                DataFormat poiFormat = row.getSheet().getWorkbook().createDataFormat();
                cellStyle.setDataFormat(poiFormat.getFormat(excelFormatPattern));
                row.createCell(3).setCellValue(dateFormat.format(date));
   
                cell.setCellStyle(cellStyle);
   
                // the formula returns TRUE is the formatted date in column C equals to the string in column D
View Full Code Here

       // Check all the colours
       for(int sn=0; sn<wb.getNumberOfSheets(); sn++) {
          Sheet s = wb.getSheetAt(sn);
          for(Row r : s) {
             for(Cell c : r) {
                CellStyle cs = c.getCellStyle();
                if(cs != null) {
                   cs.getFillForegroundColor();
                }
             }
          }
       }
      
View Full Code Here

       richTextString.applyFont(font3);
       richTextString.applyFont(0, 3, font1);
       cell.setCellValue(richTextString);

       // To enable newlines you need set a cell styles with wrap=true
       CellStyle cs = wb.createCellStyle();
       cs.setWrapText(true);
       cell.setCellStyle(cs);

       // Check the text has the
       assertEquals(text, cell.getStringCellValue());
      
View Full Code Here

TOP

Related Classes of org.apache.poi.ss.usermodel.CellStyle

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.