/** * 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(); }
/** * 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(); }
/** * 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(); }
/** * 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(); }
public static final void main(String... args) throws Exception { InputStream is = ExcelFormatTest.class.getResourceAsStream("Test.xlsx"); Workbook wb = new XSSFWorkbook(is); Sheet sheet = wb.getSheetAt(0); Cell cell = sheet.getRow(0).getCell(0); XSSFRichTextString richText = (XSSFRichTextString)cell.getRichStringCellValue(); int formattingRuns = cell.getRichStringCellValue().numFormattingRuns(); for(int i = 0; i < formattingRuns; i++) { int startIdx = richText.getIndexOfFormattingRun(i); int length = richText.getLengthOfFormattingRun(i); System.out.println("Text: " + richText.getString().substring(startIdx, startIdx + length)); if(i == 0) { short fontIndex = cell.getCellStyle().getFontIndex(); Font f = wb.getFontAt(fontIndex); System.out.println("Bold: " + (f.getBoldweight() == Font.BOLDWEIGHT_BOLD)); System.out.println("Italics: " + f.getItalic() + "\n"); } else { Font f = richText.getFontOfFormattingRun(i); System.out.println("Bold: " + (f.getBoldweight() == Font.BOLDWEIGHT_BOLD)); System.out.println("Italics: " + f.getItalic() + "\n"); } } }
cellText.getIndexOfFormattingRun(i), cellText.getIndexOfFormattingRun(i) + cellText.getLengthOfFormattingRun(i));
int indexStart = cellText.getIndexOfFormattingRun(i); int indexEnd = indexStart + cellText.getLengthOfFormattingRun(i);