Search in sources :

Example 1 with XSSFRichTextString

use of org.apache.poi.xssf.usermodel.XSSFRichTextString in project poi by apache.

the class XSSFSheetXMLHandler method endElement.

@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
    if (uri != null && !uri.equals(NS_SPREADSHEETML)) {
        return;
    }
    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);
                    XSSFRichTextString rtss = new XSSFRichTextString(sharedStringsTable.getEntryAt(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)
                    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 = commentsTable != null ? commentsTable.findCellComment(new CellAddress(cellRef)) : null;
        // Output
        output.cell(cellRef, thisStr, comment);
    } 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);
    } 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);
    }
}
Also used : CellAddress(org.apache.poi.ss.util.CellAddress) XSSFRichTextString(org.apache.poi.xssf.usermodel.XSSFRichTextString) XSSFRichTextString(org.apache.poi.xssf.usermodel.XSSFRichTextString) XSSFComment(org.apache.poi.xssf.usermodel.XSSFComment)

Example 2 with XSSFRichTextString

use of org.apache.poi.xssf.usermodel.XSSFRichTextString in project poi by apache.

the class SXSSFCell method getRichStringCellValue.

/**
     * Get the value of the cell as a XSSFRichTextString
     * <p>
     * For numeric cells we throw an exception. For blank cells we return an empty string.
     * For formula cells we return the pre-calculated value if a string, otherwise an exception.
     * </p>
     * @return the value of the cell as a XSSFRichTextString
     */
@Override
public RichTextString getRichStringCellValue() {
    CellType cellType = getCellTypeEnum();
    if (getCellTypeEnum() != CellType.STRING)
        throw typeMismatch(CellType.STRING, cellType, false);
    StringValue sval = (StringValue) _value;
    if (sval.isRichText())
        return ((RichTextValue) _value).getValue();
    else {
        String plainText = getStringCellValue();
        return getSheet().getWorkbook().getCreationHelper().createRichTextString(plainText);
    }
}
Also used : CellType(org.apache.poi.ss.usermodel.CellType) XSSFRichTextString(org.apache.poi.xssf.usermodel.XSSFRichTextString) RichTextString(org.apache.poi.ss.usermodel.RichTextString)

Example 3 with XSSFRichTextString

use of org.apache.poi.xssf.usermodel.XSSFRichTextString in project poi by apache.

the class SXSSFCell method setCellValue.

/**
     * Set a rich string value for the cell.
     *
     * @param value  value to set the cell to.  For formulas we'll set the formula
     * string, for String cells we'll set its value.  For other types we will
     * change the cell to a string cell and set its value.
     * If value is null then we will change the cell to a Blank cell.
     */
@Override
public void setCellValue(RichTextString value) {
    XSSFRichTextString xvalue = (XSSFRichTextString) value;
    if (xvalue != null && xvalue.getString() != null) {
        ensureRichTextStringType();
        if (xvalue.length() > SpreadsheetVersion.EXCEL2007.getMaxTextLength()) {
            throw new IllegalArgumentException("The maximum length of cell contents (text) is 32,767 characters");
        }
        if (xvalue.hasFormatting())
            logger.log(POILogger.WARN, "SXSSF doesn't support Shared Strings, rich text formatting information has be lost");
        ((RichTextValue) _value).setValue(xvalue);
    } else {
        setCellType(CellType.BLANK);
    }
}
Also used : XSSFRichTextString(org.apache.poi.xssf.usermodel.XSSFRichTextString)

Example 4 with XSSFRichTextString

use of org.apache.poi.xssf.usermodel.XSSFRichTextString in project poi by apache.

the class SheetDataWriter method writeCell.

public void writeCell(int columnIndex, Cell cell) throws IOException {
    if (cell == null) {
        return;
    }
    String ref = new CellReference(_rownum, columnIndex).formatAsString();
    _out.write("<c r=\"" + ref + "\"");
    CellStyle cellStyle = cell.getCellStyle();
    if (cellStyle.getIndex() != 0) {
        // need to convert the short to unsigned short as the indexes can be up to 64k
        // ideally we would use int for this index, but that would need changes to some more 
        // APIs
        _out.write(" s=\"" + (cellStyle.getIndex() & 0xffff) + "\"");
    }
    CellType cellType = cell.getCellTypeEnum();
    switch(cellType) {
        case BLANK:
            {
                _out.write(">");
                break;
            }
        case FORMULA:
            {
                _out.write(">");
                _out.write("<f>");
                outputQuotedString(cell.getCellFormula());
                _out.write("</f>");
                switch(cell.getCachedFormulaResultTypeEnum()) {
                    case NUMERIC:
                        double nval = cell.getNumericCellValue();
                        if (!Double.isNaN(nval)) {
                            _out.write("<v>" + nval + "</v>");
                        }
                        break;
                    default:
                        break;
                }
                break;
            }
        case STRING:
            {
                if (_sharedStringSource != null) {
                    XSSFRichTextString rt = new XSSFRichTextString(cell.getStringCellValue());
                    int sRef = _sharedStringSource.addEntry(rt.getCTRst());
                    _out.write(" t=\"" + STCellType.S + "\">");
                    _out.write("<v>");
                    _out.write(String.valueOf(sRef));
                    _out.write("</v>");
                } else {
                    _out.write(" t=\"inlineStr\">");
                    _out.write("<is><t");
                    if (hasLeadingTrailingSpaces(cell.getStringCellValue())) {
                        _out.write(" xml:space=\"preserve\"");
                    }
                    _out.write(">");
                    outputQuotedString(cell.getStringCellValue());
                    _out.write("</t></is>");
                }
                break;
            }
        case NUMERIC:
            {
                _out.write(" t=\"n\">");
                _out.write("<v>" + cell.getNumericCellValue() + "</v>");
                break;
            }
        case BOOLEAN:
            {
                _out.write(" t=\"b\">");
                _out.write("<v>" + (cell.getBooleanCellValue() ? "1" : "0") + "</v>");
                break;
            }
        case ERROR:
            {
                FormulaError error = FormulaError.forInt(cell.getErrorCellValue());
                _out.write(" t=\"e\">");
                _out.write("<v>" + error.getString() + "</v>");
                break;
            }
        default:
            {
                throw new IllegalStateException("Invalid cell type: " + cellType);
            }
    }
    _out.write("</c>");
}
Also used : XSSFRichTextString(org.apache.poi.xssf.usermodel.XSSFRichTextString) FormulaError(org.apache.poi.ss.usermodel.FormulaError) CellType(org.apache.poi.ss.usermodel.CellType) STCellType(org.openxmlformats.schemas.spreadsheetml.x2006.main.STCellType) XSSFRichTextString(org.apache.poi.xssf.usermodel.XSSFRichTextString) CellStyle(org.apache.poi.ss.usermodel.CellStyle) CellReference(org.apache.poi.ss.util.CellReference)

Example 5 with XSSFRichTextString

use of org.apache.poi.xssf.usermodel.XSSFRichTextString in project poi by apache.

the class MergingCells method main.

public static void main(String[] args) throws IOException {
    //or new HSSFWorkbook();
    Workbook wb = new XSSFWorkbook();
    Sheet sheet = wb.createSheet("new sheet");
    Row row = sheet.createRow((short) 1);
    Cell cell = row.createCell((short) 1);
    cell.setCellValue(new XSSFRichTextString("This is a test of merging"));
    sheet.addMergedRegion(new CellRangeAddress(1, 1, 1, 2));
    // Write the output to a file
    FileOutputStream fileOut = new FileOutputStream("merging_cells.xlsx");
    wb.write(fileOut);
    fileOut.close();
    wb.close();
}
Also used : XSSFRichTextString(org.apache.poi.xssf.usermodel.XSSFRichTextString) FileOutputStream(java.io.FileOutputStream) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) Row(org.apache.poi.ss.usermodel.Row) CellRangeAddress(org.apache.poi.ss.util.CellRangeAddress) Sheet(org.apache.poi.ss.usermodel.Sheet) Cell(org.apache.poi.ss.usermodel.Cell) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) Workbook(org.apache.poi.ss.usermodel.Workbook)

Aggregations

XSSFRichTextString (org.apache.poi.xssf.usermodel.XSSFRichTextString)17 XSSFWorkbook (org.apache.poi.xssf.usermodel.XSSFWorkbook)7 Workbook (org.apache.poi.ss.usermodel.Workbook)6 Test (org.junit.Test)5 XSSFChart (org.apache.poi.xssf.usermodel.XSSFChart)4 Cell (org.apache.poi.ss.usermodel.Cell)3 Row (org.apache.poi.ss.usermodel.Row)3 FileOutputStream (java.io.FileOutputStream)2 CellStyle (org.apache.poi.ss.usermodel.CellStyle)2 CellType (org.apache.poi.ss.usermodel.CellType)2 Comment (org.apache.poi.ss.usermodel.Comment)2 Sheet (org.apache.poi.ss.usermodel.Sheet)2 XSSFClientAnchor (org.apache.poi.xssf.usermodel.XSSFClientAnchor)2 SAXException (org.xml.sax.SAXException)2 DataFormatException (com.cubrid.common.ui.cubrid.table.dialog.DataFormatException)1 IOException (java.io.IOException)1 SQLException (java.sql.SQLException)1 OPCPackage (org.apache.poi.openxml4j.opc.OPCPackage)1 FormulaError (org.apache.poi.ss.usermodel.FormulaError)1 RichTextString (org.apache.poi.ss.usermodel.RichTextString)1