Java Code Examples for org.apache.poi.xssf.usermodel.XSSFRichTextString#toString()

The following examples show how to use org.apache.poi.xssf.usermodel.XSSFRichTextString#toString() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CellValueHelper.java    From autopoi with Apache License 2.0 6 votes vote down vote up
/**
 * 07版本复杂数据
 * 
 * @param rich
 * @return
 */
private String getXSSFRichString(XSSFRichTextString rich) {
	int nums = rich.numFormattingRuns();
	StringBuilder sb = new StringBuilder();
	String text = rich.toString();
	int currentIndex = 0, lastIndex = 0;
	for (int i = 1; i <= nums; i++) {
		sb.append("<span ");
		try {
			sb.append("class='font_" + getFontIndex(rich.getFontOfFormattingRun(i - 1)));
			sb.append("_");
			sb.append(cssRandom);
			sb.append("'");
		} catch (Exception e) {
		}
		sb.append(">");
		currentIndex = rich.getIndexOfFormattingRun(i) == -1 ? text.length() : rich.getIndexOfFormattingRun(i);
		sb.append(XmlEscapers.xmlContentEscaper().escape(text.substring(lastIndex, currentIndex)));
		sb.append("</span>");
		lastIndex = currentIndex;
	}
	return sb.toString();
}
 
Example 2
Source File: CellValueHelper.java    From jeasypoi with Apache License 2.0 6 votes vote down vote up
/**
 * 07版本复杂数据
 * 
 * @param rich
 * @return
 */
private String getXSSFRichString(XSSFRichTextString rich) {
	int nums = rich.numFormattingRuns();
	StringBuilder sb = new StringBuilder();
	String text = rich.toString();
	int currentIndex = 0, lastIndex = 0;
	for (int i = 1; i <= nums; i++) {
		sb.append("<span ");
		try {
			sb.append("class='font_" + getFontIndex(rich.getFontOfFormattingRun(i - 1)));
			sb.append("_");
			sb.append(cssRandom);
			sb.append("'");
		} catch (Exception e) {
		}
		sb.append(">");
		currentIndex = rich.getIndexOfFormattingRun(i) == -1 ? text.length() : rich.getIndexOfFormattingRun(i);
		sb.append(XmlEscapers.xmlContentEscaper().escape(text.substring(lastIndex, currentIndex)));
		sb.append("</span>");
		lastIndex = currentIndex;
	}
	return sb.toString();
}
 
Example 3
Source File: CellValueHelper.java    From easypoi with Apache License 2.0 6 votes vote down vote up
/**
 * 07版本复杂数据
 * @param rich
 * @return
 */
private String getXSSFRichString(XSSFRichTextString rich) {
    int nums = rich.numFormattingRuns();
    StringBuilder sb = new StringBuilder();
    String text = rich.toString();
    int currentIndex = 0, lastIndex = 0;
    for (int i = 1; i <= nums; i++) {
        sb.append("<span ");
        try {
            sb.append("class='font_" + getFontIndex(rich.getFontOfFormattingRun(i - 1)));
            sb.append("_");
            sb.append(cssRandom);
            sb.append("'");
        } catch (Exception e) {
        }
        sb.append(">");
        currentIndex = rich.getIndexOfFormattingRun(i) == -1 ? text.length() : rich
            .getIndexOfFormattingRun(i);
        sb.append(XmlEscapers.xmlContentEscaper().escape(
            text.substring(lastIndex, currentIndex)));
        sb.append("</span>");
        lastIndex = currentIndex;
    }
    return sb.toString();
}
 
Example 4
Source File: XlsxHandler.java    From easyexcel with Apache License 2.0 4 votes vote down vote up
/**
 * 对解析出来的数据进行类型处理
 * 
 * @param value
 *            单元格的值(这时候是一串数字)
 * @param thisStr
 *            一个空字符串
 * @return
 */
@SuppressWarnings("deprecation")
private String getDataValue(String value, String thisStr) {
	switch (nextDataType) {
	// 这几个的顺序不能随便交换,交换了很可能会导致数据错误
	case BOOL:
		char first = value.charAt(0);
		thisStr = first == '0' ? "FALSE" : "TRUE";
		break;
	case ERROR:
		thisStr = "\"ERROR:" + value.toString() + '"';
		break;
	case FORMULA:
		thisStr = '"' + value.toString() + '"';
		break;
	case INLINESTR:
		XSSFRichTextString rtsi = new XSSFRichTextString(value.toString());
		thisStr = rtsi.toString();
		rtsi = null;
		break;
	case SSTINDEX:
		String sstIndex = value.toString();
		try {
			int idx = Integer.parseInt(sstIndex);
			XSSFRichTextString rtss = new XSSFRichTextString(sharedStringsTable.getEntryAt(idx));
			thisStr = rtss.toString();
			rtss = null;
		} catch (NumberFormatException ex) {
			thisStr = value.toString();
		}
		break;
	case NUMBER:
		if (formatString != null) {
			thisStr = sdf.formatRawCellContents(Double.parseDouble(value), formatIndex, formatString).trim();
		} else {
			thisStr = value;
		}

		thisStr = thisStr.replace("_", "").trim();
		break;
	case DATE:
		thisStr = sdf.formatRawCellContents(Double.parseDouble(value), formatIndex, formatString);
		// 对日期字符串作特殊处理
		thisStr = thisStr.replace(" ", "T");
		break;
	default:
		thisStr = " ";
		break;
	}
	return thisStr;
}
 
Example 5
Source File: XLSX2CSV.java    From DBus with Apache License 2.0 4 votes vote down vote up
public void endElement(String uri, String localName, String name)
        throws SAXException {

    String thisStr = null;

    // 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.
        output.print(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(',');
            }
        }

        // We're onto a new row
        output.println();
        lastColumnNumber = -1;
    }

}
 
Example 6
Source File: XSSFSheetHandler.java    From bdf3 with Apache License 2.0 4 votes vote down vote up
public void endElement(String uri, String localName, String name)
        throws SAXException {

    String thisStr = null;
    StringBuffer contents = context.getContents();

    if ("v".equals(name)) {
        switch (context.getNextDataType()) {

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

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

        case FORMULA:
            thisStr = contents.toString();
            break;

        case INLINESTR:
            XSSFRichTextString rtsi = new XSSFRichTextString(contents
                    .toString());
            thisStr = rtsi.toString();
            break;

        case SSTINDEX:
            String sstIndex = contents.toString();
            try {
                int idx = Integer.parseInt(sstIndex);
                XSSFRichTextString rtss = new XSSFRichTextString(
                        context.getStrings().getEntryAt(idx));
                thisStr = rtss.toString() ;
            } catch (NumberFormatException ex) {
                throw new RuntimeException("Failed to parse SST index '" + sstIndex
                        + "': " + ex.toString());
            }
            break;

        case NUMBER:
            String n = contents.toString();
            thisStr = n;
            if (context.getFormatString() != null)
                thisStr = context.getFormatter().formatRawCellContents(Double
                        .parseDouble(n), context.getFormatIndex(),
                        context.getFormatString());
            else
                thisStr = n;
            break;

        default:
        	throw new RuntimeException("Unexpected type: " + context.getNextDataType() + "");
        }
        
        context.getCurrentCell().setValue(thisStr);
        

    }

}
 
Example 7
Source File: XLSX2CSV.java    From bdf3 with Apache License 2.0 4 votes vote down vote up
public void endElement(String uri, String localName, String name)
        throws SAXException {

    String thisStr = null;

    // 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.
        output.print(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(',');
            }
        }

        // We're onto a new row
        output.println();
        lastColumnNumber = -1;
    }

}
 
Example 8
Source File: XSSFSheetXMLHandler.java    From CloverETL-Engine with GNU Lesser General Public License v2.1 4 votes vote down vote up
@Override
public void endElement(String uri, String localName, String name) throws SAXException {
	String thisStr = null;
	int cellType;

	// 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 = Character.toString(first);
			cellType = Cell.CELL_TYPE_BOOLEAN;
			break;

		case ERROR:
			thisStr = "ERROR:" + value.toString();
			cellType = Cell.CELL_TYPE_ERROR;
			break;
			
		case FORMULA:
               if(formulasNotResults) {
                  thisStr = formula.toString();
               } else {
                  thisStr = value.toString();
               }
               cellType = Cell.CELL_TYPE_FORMULA;
               break;

		case INLINE_STRING:
			// TODO: Can these ever have formatting on them?
			XSSFRichTextString rtsi = new XSSFRichTextString(value.toString());
			thisStr = rtsi.toString();
			cellType = Cell.CELL_TYPE_STRING;
			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) {
				System.err.println("Failed to parse SST index '" + sstIndex + "': " + ex.toString());
			}
			cellType = Cell.CELL_TYPE_STRING;
			break;

		case NUMBER:
			String n = value.toString();
			thisStr = n;
			cellType = Cell.CELL_TYPE_NUMERIC;
			break;

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

		// Output
		output.cell(cellRef, cellType, formulaType, thisStr, styleIndex);
		formulaType = -1;
	} else if ("is".equals(localName)) {
		isIsOpen = false;
	} else if ("row".equals(localName)) {
		output.endRow();
	} 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);
	}
}
 
Example 9
Source File: SheetXmlParser.java    From timbuctoo with GNU General Public License v3.0 4 votes vote down vote up
@Override
public void endElement(String uri, String localName, String qualifiedName)
  throws SAXException {

  if (uri != null && ! uri.equals(NS_SPREADSHEETML)) {
    return;
  }

  String thisStr = null;

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

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

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

      case FORMULA:
        thisStr = value.toString();
        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:
        thisStr = value.toString();
        break;

      default:
        thisStr = "(TODO: Unexpected type: " + nextDataType + ")";
        break;
    }
    output.cell(column, thisStr, cellStyleStr);

  } else if ("is".equals(localName)) {
    isIsOpen = false;
  } else if ("row".equals(localName)) {
    // 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;
  }
}