org.apache.poi.xssf.usermodel.XSSFRichTextString类的使用及代码示例

x33g5p2x  于2022-02-03 转载在 其他  
字(9.8k)|赞(0)|评价(0)|浏览(1427)

本文整理了Java中org.apache.poi.xssf.usermodel.XSSFRichTextString类的一些代码示例,展示了XSSFRichTextString类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。XSSFRichTextString类的具体详情如下:
包路径:org.apache.poi.xssf.usermodel.XSSFRichTextString
类名称:XSSFRichTextString

XSSFRichTextString介绍

[英]Rich text unicode string. These strings can have fonts applied to arbitary parts of the string.

Most strings in a workbook have formatting applied at the cell level, that is, the entire string in the cell has the same formatting applied. In these cases, the formatting for the cell is stored in the styles part, and the string for the cell can be shared across the workbook. The following code illustrates the example.

cell1.setCellValue(new XSSFRichTextString("Apache POI")); 
cell2.setCellValue(new XSSFRichTextString("Apache POI")); 
cell3.setCellValue(new XSSFRichTextString("Apache POI"));

In the above example all three cells will use the same string cached on workbook level.

Some strings in the workbook may have formatting applied at a level that is more granular than the cell level. For instance, specific characters within the string may be bolded, have coloring, italicizing, etc. In these cases, the formatting is stored along with the text in the string table, and is treated as a unique entry in the workbook. The following xml and code snippet illustrate this.

XSSFRichTextString s1 = new XSSFRichTextString("Apache POI"); 
s1.applyFont(boldArial); 
cell1.setCellValue(s1); 
XSSFRichTextString s2 = new XSSFRichTextString("Apache POI"); 
s2.applyFont(italicCourier); 
cell2.setCellValue(s2);

[中]富文本unicode字符串。这些字符串可以将字体应用于字符串的任意部分。
工作簿中的大多数字符串都在单元格级别应用了格式,也就是说,单元格中的整个字符串都应用了相同的格式。在这些情况下,单元格的格式存储在样式部分,单元格的字符串可以在工作簿中共享。下面的代码演示了该示例。

cell1.setCellValue(new XSSFRichTextString("Apache POI")); 
cell2.setCellValue(new XSSFRichTextString("Apache POI")); 
cell3.setCellValue(new XSSFRichTextString("Apache POI"));

在上述示例中,所有三个单元格都将使用工作簿级别缓存的同一字符串。
工作簿中的某些字符串可能在比单元格级别更精细的级别上应用了格式。例如,字符串中的特定字符可能是粗体、有颜色、斜体等。在这些情况下,格式与字符串表中的文本一起存储,并被视为工作簿中的唯一条目。下面的xml和代码片段说明了这一点。

XSSFRichTextString s1 = new XSSFRichTextString("Apache POI"); 
s1.applyFont(boldArial); 
cell1.setCellValue(s1); 
XSSFRichTextString s2 = new XSSFRichTextString("Apache POI"); 
s2.applyFont(italicCourier); 
cell2.setCellValue(s2);

代码示例

代码示例来源:origin: alibaba/easyexcel

private void endCellValue(String name) throws SAXException {
  // ensure size
  if (curCol >= curRowContent.length) {
    curRowContent = Arrays.copyOf(curRowContent, (int)(curCol * 1.5));
  }
  if (CELL_VALUE_TAG.equals(name)) {
    switch (currentCellType) {
      case STRING:
        int idx = Integer.parseInt(currentCellValue);
        currentCellValue = new XSSFRichTextString(sst.getEntryAt(idx)).toString();
        currentCellType = FieldType.EMPTY;
        break;
    }
    curRowContent[curCol] = currentCellValue;
  } else if (CELL_VALUE_TAG_1.equals(name)) {
    curRowContent[curCol] = currentCellValue;
  }
}

代码示例来源:origin: looly/hutool

break;
case INLINESTR:
  result = new XSSFRichTextString(value.toString()).toString();
  break;
case SSTINDEX:
  try {
    final int index = Integer.parseInt(value);
    result = new XSSFRichTextString(sharedStringsTable.getEntryAt(index)).getString();
  } catch (NumberFormatException e) {
    result = value;

代码示例来源:origin: org.apache.poi/poi-ooxml

/**
 * Applies a font to the specified characters of a string.
 *
 * @param startIndex    The start index to apply the font to (inclusive)
 * @param endIndex      The end index to apply to font to (exclusive)
 * @param font          The index of the font to use.
 */
public void applyFont(int startIndex, int endIndex, Font font) {
  if (startIndex > endIndex)
    throw new IllegalArgumentException("Start index must be less than end index, but had " + startIndex + " and " + endIndex);
  if (startIndex < 0 || endIndex > length())
    throw new IllegalArgumentException("Start and end index not in range, but had " + startIndex + " and " + endIndex);
  if (startIndex == endIndex)
    return;
  if(st.sizeOfRArray() == 0 && st.isSetT()) {
    //convert <t>string</t> into a text run: <r><t>string</t></r>
    st.addNewR().setT(st.getT());
    st.unsetT();
  }
  String text = getString();
  XSSFFont xssfFont = (XSSFFont)font;
  TreeMap<Integer, CTRPrElt> formats = getFormatMap(st);
  CTRPrElt fmt = CTRPrElt.Factory.newInstance();
  setRunAttributes(xssfFont.getCTFont(), fmt);
  applyFont(formats, startIndex, endIndex, fmt);
  CTRst newSt = buildCTRst(text, formats);
  st.set(newSt);
}

代码示例来源:origin: org.apache.poi/poi-ooxml

/**
 * Sets the font of the entire string.
 * @param font          The font to use.
 */
public void applyFont(Font font) {
  String text = getString();
  applyFont(0, text.length(), font);
}

代码示例来源:origin: org.apache.poi/poi-ooxml

/**
 * Creates a new XSSFRichTextString for you.
 */
@Override
public XSSFRichTextString createRichTextString(String text) {
  XSSFRichTextString rt = new XSSFRichTextString(text);
  rt.setStylesTableReference(workbook.getStylesSource());
  return rt;
}

代码示例来源:origin: org.apache.poi/poi-ooxml

@Override
protected RichTextString createRichTextString(String str) {
  return new XSSFRichTextString(str);
}

代码示例来源:origin: org.apache.poi/poi-ooxml

case STRING:
  int sstIndex = Integer.parseInt(_cell.getV());
  XSSFRichTextString rt = new XSSFRichTextString(_sharedStringSource.getEntryAt(sstIndex));
  String text = rt.getString();
  return Boolean.parseBoolean(text);
case NUMERIC:

代码示例来源:origin: org.jeecg/easypoi-base

/**
 * 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();
}

代码示例来源:origin: stackoverflow.com

XSSFRichTextString rt1 = new XSSFRichTextString("Apache POI is");
rt1.applyFont(plainArial);
XSSFRichTextString rt2 = new XSSFRichTextString(" great!");
rt2.applyFont(boldArial);
String text = rt2.getString();

cell1.setCellValue(rt1.append(text, boldArial));

代码示例来源:origin: stackoverflow.com

HSSFCell hssfCell = row.createCell(idx);
//rich text consists of two runs
HSSFRichTextString richString = new HSSFRichTextString( "Hello, World!" );
richString.applyFont( 0, 6, font1 );
richString.applyFont( 6, 13, font2 );
hssfCell.setCellValue( richString );

XSSFRichTextString s1 = new XSSFRichTextString("Apache POI");
s1.applyFont(boldArial);
cell1.setCellValue(s1);

代码示例来源:origin: org.apache.poi/poi-ooxml

/**
 * Returns the plain string representation.
 */
public String toString() {
  return getString();
}

代码示例来源:origin: org.apache.poi/poi-examples

XSSFRichTextString rt = new XSSFRichTextString("The quick brown fox");
rt.applyFont(0, 10, font1);
font2.setUnderline(XSSFFont.U_DOUBLE);
font2.setColor(new XSSFColor(new java.awt.Color(0, 255, 0), wb.getStylesSource().getIndexedColors()));
rt.applyFont(10, 19, font2);
rt.append(" Jumped over the lazy dog", font3);

代码示例来源:origin: org.apache.poi/poi-ooxml

str.setStylesTableReference(wb.getStylesSource());
if (str.numFormattingRuns() == 0) {
  CTRegularTextRun r = p.addNewR();
  CTTextCharacterProperties rPr = r.addNewRPr();
  rPr.setLang("en-US");
  rPr.setSz(1100);
  r.setT(str.getString());
  for (int i = 0; i < str.getCTRst().sizeOfRArray(); i++) {
    CTRElt lt = str.getCTRst().getRArray(i);
    CTRPrElt ltPr = lt.getRPr();
    if (ltPr == null) {

代码示例来源:origin: stackoverflow.com

for (int i = 0; i < cellText.numFormattingRuns(); i++) {
  try {
    htmlCode += getFormatFromFont(cellText.getFontAtIndex(i));
  } catch (NullPointerException ex) {
        .getFontOfFormattingRun(i));
  } catch (NullPointerException ex) {
  htmlCode += cellText.getString().substring(
      cellText.getIndexOfFormattingRun(i),
      cellText.getIndexOfFormattingRun(i)
          + cellText.getLengthOfFormattingRun(i));

代码示例来源:origin: org.apache.poi/poi-ooxml

return getRichStringCellValue().toString();
case FORMULA:
  return getCellFormula();

代码示例来源:origin: org.apache.poi/poi-ooxml

CTTextParagraph p = txBody.addNewP();
if (str.numFormattingRuns() == 0) {
  CTRegularTextRun r = p.addNewR();
  CTTextCharacterProperties rPr = r.addNewRPr();
  rPr.setLang("en-US");
  rPr.setSz(1100);
  r.setT(str.getString());
  for (int i = 0; i < str.getCTRst().sizeOfRArray(); i++) {
    CTRElt lt = str.getCTRst().getRArray(i);
    CTRPrElt ltPr = lt.getRPr();
    if (ltPr == null) {

代码示例来源:origin: org.apache.poi/poi-ooxml

/**
 * 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");
    }
    ((RichTextValue)_value).setValue(xvalue);
  } else {
    setCellType(CellType.BLANK);
  }
}

代码示例来源:origin: org.apache.poi/poi-ooxml

/**
 * Applies a font to the specified characters of a string.
 *
 * @param startIndex    The start index to apply the font to (inclusive)
 * @param endIndex      The end index to apply the font to (exclusive)
 * @param fontIndex     The font to use.
 */
public void applyFont(int startIndex, int endIndex, short fontIndex) {
  XSSFFont font;
  if(styles == null) {
    //style table is not set, remember fontIndex and set the run properties later,
    //when setStylesTableReference is called
    font = new XSSFFont();
    font.setFontName("#" + fontIndex);
  } else {
    font = styles.getFontAt(fontIndex);
  }
  applyFont(startIndex, endIndex, font);
}

代码示例来源:origin: primefaces/primefaces

@Override
protected RichTextString createRichTextString(String value) {
  return new XSSFRichTextString(value);
}

代码示例来源:origin: org.apache.poi/poi-ooxml

case STRING:
  int sstIndex = Integer.parseInt(_cell.getV());
  XSSFRichTextString rt = new XSSFRichTextString(_sharedStringSource.getEntryAt(sstIndex));
  return rt.getString();
case NUMERIC:
case ERROR:

相关文章