Example usage for org.apache.poi.xssf.usermodel XSSFRichTextString XSSFRichTextString

List of usage examples for org.apache.poi.xssf.usermodel XSSFRichTextString XSSFRichTextString

Introduction

In this page you can find the example usage for org.apache.poi.xssf.usermodel XSSFRichTextString XSSFRichTextString.

Prototype

@Internal
public XSSFRichTextString(CTRst st) 

Source Link

Document

Create a rich text string from the supplied XML bean

Usage

From source file:poi.xssf.usermodel.examples.FillsAndColors.java

License:Apache License

public static void main(String[] args) throws Exception {
    Workbook wb = new XSSFWorkbook(); //or new HSSFWorkbook();
    Sheet sheet = wb.createSheet("new sheet");

    // Create a row and put some cells in it. Rows are 0 based.
    Row row = sheet.createRow((short) 1);

    // Aqua background
    CellStyle style = wb.createCellStyle();
    style.setFillBackgroundColor(IndexedColors.AQUA.getIndex());
    style.setFillPattern(CellStyle.BIG_SPOTS);
    Cell cell = row.createCell((short) 1);
    cell.setCellValue(new XSSFRichTextString("X"));
    cell.setCellStyle(style);//from  www.j  av  a 2 s. c om

    // Orange "foreground", foreground being the fill foreground not the font color.
    style = wb.createCellStyle();
    style.setFillForegroundColor(IndexedColors.ORANGE.getIndex());
    style.setFillPattern(CellStyle.SOLID_FOREGROUND);
    cell = row.createCell((short) 2);
    cell.setCellValue(new XSSFRichTextString("X"));
    cell.setCellStyle(style);

    // Write the output to a file
    FileOutputStream fileOut = new FileOutputStream("fill_colors.xlsx");
    wb.write(fileOut);
    fileOut.close();

}

From source file:poi.xssf.usermodel.examples.WorkingWithRichText.java

License:Apache License

public static void main(String[] args) throws Exception {

    XSSFWorkbook wb = new XSSFWorkbook(); //or new HSSFWorkbook();

    XSSFSheet sheet = wb.createSheet();// ww  w. j ava  2 s .co  m
    XSSFRow row = sheet.createRow((short) 2);

    XSSFCell cell = row.createCell(1);
    XSSFRichTextString rt = new XSSFRichTextString("The quick brown fox");

    XSSFFont font1 = wb.createFont();
    font1.setBold(true);
    font1.setColor(new XSSFColor(new java.awt.Color(255, 0, 0)));
    rt.applyFont(0, 10, font1);

    XSSFFont font2 = wb.createFont();
    font2.setItalic(true);
    font2.setUnderline(XSSFFont.U_DOUBLE);
    font2.setColor(new XSSFColor(new java.awt.Color(0, 255, 0)));
    rt.applyFont(10, 19, font2);

    XSSFFont font3 = wb.createFont();
    font3.setColor(new XSSFColor(new java.awt.Color(0, 0, 255)));
    rt.append(" Jumped over the lazy dog", font3);

    cell.setCellValue(rt);

    // Write the output to a file
    FileOutputStream fileOut = new FileOutputStream("xssf-richtext.xlsx");
    wb.write(fileOut);
    fileOut.close();
}

From source file:service.XSSFSheetHandler.java

public void endElement(String uri, String localName, String name) throws SAXException {

    String thisStr = null;//from w  w  w.j  a  va  2  s.c om

    // v => contents of a cell
    if ("v".equals(name)) {
        // Process the value contents as required.
        // Do now, as characters() may be called more than once
        switch (nextDataType) {

        case BOOL:
            char first = value.charAt(0);
            thisStr = first == '0' ? "FALSE" : "TRUE";
            break;

        case ERROR:
            thisStr = "ERROR:" + value.toString() + '"';
            break;

        case FORMULA:
            // A formula could result in a string value,
            // so always add double-quote characters.
            thisStr = value.toString();
            break;

        case INLINESTR:
            // TODO: have seen an example of this, so it's untested.
            XSSFRichTextString rtsi = new XSSFRichTextString(value.toString());
            thisStr = rtsi.toString();
            break;

        case SSTINDEX:
            String sstIndex = value.toString();
            try {
                int idx = Integer.parseInt(sstIndex);
                XSSFRichTextString rtss = new XSSFRichTextString(sharedStringsTable.getEntryAt(idx));
                thisStr = rtss.toString();
            } catch (NumberFormatException ex) {
                output.println("Failed to parse SST index '" + sstIndex + "': " + ex.toString());
            }
            break;

        case NUMBER:
            String n = value.toString();
            if (this.formatString != null)
                thisStr = formatter.formatRawCellContents(Double.parseDouble(n), this.formatIndex,
                        this.formatString);
            else
                thisStr = n;
            break;

        default:
            thisStr = "(TODO: Unexpected type: " + nextDataType + ")";
            break;
        }

        // Output after we've seen the string contents
        // Emit commas for any fields that were missing on this row
        if (lastColumnNumber == -1) {
            lastColumnNumber = 0;
        }
        /*   for (int i = lastColumnNumber; i < thisColumn; ++i)
            output.print(',');*/

        // Might be the empty string.
        handlerInterface.processEachRecord(thisColumn, countrows, thisStr);
        /*if(thisColumn == 1 || thisColumn ==0 || thisColumn==2)
                strValues[thisColumn] = thisStr;*/
        // Update column
        if (thisColumn > -1)
            lastColumnNumber = thisColumn;

    } else if ("row".equals(name)) {

        // Print out any missing commas if needed
        if (minColumns > 0) {
            // Columns are 0 based
            if (lastColumnNumber == -1) {
                lastColumnNumber = 0;
            }
            /*  for (int i = lastColumnNumber; i < (this.minColumnCount); i++) {
                output.print(',');
            }*/
        }

        handlerInterface.rowCompleted();
        // We're onto a new row
        /* if(strValues[1].equals("Y"))
         {                   
          output.println("found : "+strValues[0]+"       "+strValues[2]);
          count++;
          }*/
        countrows++;
        lastColumnNumber = -1;

    }

}

From source file:uk.ac.manchester.cs.owl.semspreadsheets.model.xssf.impl.WorkbookXSSFImpl.java

License:Open Source License

public void visit(SetCellValue setCellValue) {
    XSSFSheet xssfSheet = workbook.getSheet(setCellValue.getSheet().getName());
    XSSFRow xssfRow = xssfSheet.getRow(setCellValue.getRow());
    if (xssfRow == null && setCellValue.getNewValue() != null) {
        xssfRow = xssfSheet.createRow(setCellValue.getRow());
    }/*from ww w . ja v a 2s  . c om*/
    XSSFCell xssfCell = xssfRow.getCell(setCellValue.getCol());
    if (xssfCell == null && setCellValue.getNewValue() != null) {
        xssfCell = xssfRow.createCell(setCellValue.getCol());
    }
    if (xssfCell != null) {
        if (setCellValue.getNewValue() != null) {
            xssfCell.setCellValue(new XSSFRichTextString(setCellValue.getNewValue().toString()));
        } else {
            xssfRow.removeCell(xssfCell);
        }
    }
}

From source file:uk.co.spudsoft.birt.emitters.excel.StyleManagerXUtils.java

License:Open Source License

@Override
public RichTextString createRichTextString(String value) {
    XSSFRichTextString result = new XSSFRichTextString(value);
    return result;
}

From source file:utilities.SmapSheetXMLHandler.java

License:Apache License

@Override
public void endElement(String uri, String localName, String qName) throws SAXException {

    if (uri != null && !uri.equals(NS_SPREADSHEETML)) {
        return;//  w  w  w .j  a  v a  2s  . c  o m
    }

    String thisStr = null;

    // v => contents of a cell
    if (isTextTag(localName)) {
        vIsOpen = false;

        // Process the value contents as required, now we have it all
        switch (nextDataType) {
        case BOOLEAN:
            char first = value.charAt(0);
            thisStr = first == '0' ? "FALSE" : "TRUE";
            break;

        case ERROR:
            thisStr = "ERROR:" + value;
            break;

        case FORMULA:
            if (formulasNotResults) {
                thisStr = formula.toString();
            } else {
                String fv = value.toString();

                if (this.formatString != null) {
                    try {
                        // Try to use the value as a formattable number
                        double d = Double.parseDouble(fv);
                        thisStr = formatter.formatRawCellContents(d, this.formatIndex, this.formatString);
                    } catch (NumberFormatException e) {
                        // Formula is a String result not a Numeric one
                        thisStr = fv;
                    }
                } else {
                    // No formatting applied, just do raw value in all cases
                    thisStr = fv;
                }
            }
            break;

        case INLINE_STRING:
            // TODO: Can these ever have formatting on them?
            XSSFRichTextString rtsi = new XSSFRichTextString(value.toString());
            thisStr = rtsi.toString();
            break;

        case SST_STRING:
            String sstIndex = value.toString();
            try {
                int idx = Integer.parseInt(sstIndex);
                RichTextString rtss = sharedStringsTable.getItemAt(idx);
                thisStr = rtss.toString();
            } catch (NumberFormatException ex) {
                logger.log(POILogger.ERROR, "Failed to parse SST index '" + sstIndex, ex);
            }
            break;

        case NUMBER:
            String n = value.toString();
            if (this.formatString != null && n.length() > 0) {
                // smap Check for date
                boolean isDate = false;
                double d = Double.parseDouble(n);
                if (DateUtil.isValidExcelDate(d)) {
                    ExcelNumberFormat nf = ExcelNumberFormat.from(style);
                    if (nf != null) {
                        isDate = DateUtil.isADateFormat(nf);
                    }
                }
                if (isDate) {
                    try {
                        Date dv = DateUtil.getJavaDate(d);
                        thisStr = sdf.format(dv);
                    } catch (Exception e) {

                    }
                } else {
                    thisStr = formatter.formatRawCellContents(Double.parseDouble(n), this.formatIndex,
                            this.formatString);
                }
            } else
                thisStr = n;
            break;

        default:
            thisStr = "(TODO: Unexpected type: " + nextDataType + ")";
            break;
        }

        // Do we have a comment for this cell?
        checkForEmptyCellComments(EmptyCellCommentsCheckType.CELL);
        XSSFComment comment = comments != null ? comments.findCellComment(new CellAddress(cellRef)) : null;

        // Output

        output.cell(cellRef, thisStr, comment, nextDataType, formatString);
    } else if ("f".equals(localName)) {
        fIsOpen = false;
    } else if ("is".equals(localName)) {
        isIsOpen = false;
    } else if ("row".equals(localName)) {
        // Handle any "missing" cells which had comments attached
        checkForEmptyCellComments(EmptyCellCommentsCheckType.END_OF_ROW);

        // Finish up the row
        output.endRow(rowNum);

        // some sheets do not have rowNum set in the XML, Excel can read them so we should try to read them as well
        nextRowNum = rowNum + 1;
    } else if ("sheetData".equals(localName)) {
        // Handle any "missing" cells which had comments attached
        checkForEmptyCellComments(EmptyCellCommentsCheckType.END_OF_SHEET_DATA);

        // indicate that this sheet is now done
        output.endSheet();
    } else if ("oddHeader".equals(localName) || "evenHeader".equals(localName)
            || "firstHeader".equals(localName)) {
        hfIsOpen = false;
        output.headerFooter(headerFooter.toString(), true, localName);
    } else if ("oddFooter".equals(localName) || "evenFooter".equals(localName)
            || "firstFooter".equals(localName)) {
        hfIsOpen = false;
        output.headerFooter(headerFooter.toString(), false, localName);
    }
}