/** * Append new text to this text run and apply the specify font to it * * @param text the text to append * @param font the font to apply to the appended text or <code>null</code> if no formatting is required */ public void append(String text, XSSFFont font){ if(st.sizeOfRArray() == 0 && st.isSetT()) { //convert <t>string</t> into a text run: <r><t>string</t></r> CTRElt lt = st.addNewR(); lt.setT(st.getT()); preserveSpaces(lt.xgetT()); st.unsetT(); } CTRElt lt = st.addNewR(); lt.setT(text); preserveSpaces(lt.xgetT()); if (font != null) { CTRPrElt pr = lt.addNewRPr(); setRunAttributes(font.getCTFont(), pr); } }
/** * Returns the number of characters this format run covers. * * @param index the index of the formatting run * @return the number of characters this format run covers */ public int getLengthOfFormattingRun(int index) { if(st.sizeOfRArray() == 0 || index >= st.sizeOfRArray()) { return -1; } CTRElt r = st.getRArray(index); return r.getT().length(); }
/** * Returns the plain string representation. */ public String getString() { if(st.sizeOfRArray() == 0) { return utfDecode(st.getT()); } StringBuilder buf = new StringBuilder(); //noinspection deprecation - for performance reasons! for(CTRElt r : st.getRArray()){ buf.append(r.getT()); } return utfDecode(buf.toString()); }
/** * Does this string have any explicit formatting applied, or is * it just text in the default style? */ public boolean hasFormatting() { //noinspection deprecation - for performance reasons! CTRElt[] rs = st.getRArray(); if (rs == null || rs.length == 0) { return false; } for (CTRElt r : rs) { if (r.isSetRPr()) return true; } return false; }
private SharedStringsTable mockSharedStringsTable( String... strings ) { SharedStringsTable sst = new SharedStringsTable(); for ( String str : strings ) { CTRst st = CTRst.Factory.newInstance(); st.setT( str ); sst.addEntry( st ); } return sst; }
SharedStringsTable sst = r1.getSharedStringsTable();//r1 being your XSSFReader CTRst st = sst.getEntryAt(k);//k is the index that you have in <v> tag st.getT();//getT() gets your value
/** * @return The number of formatting runs used. */ public int numFormattingRuns() { return st.sizeOfRArray(); }
CTRst buildCTRst(String text, TreeMap<Integer, CTRPrElt> formats){ if(text.length() != formats.lastKey()) { throw new IllegalArgumentException("Text length was " + text.length() + " but the last format index was " + formats.lastKey()); } CTRst stf = CTRst.Factory.newInstance(); int runStartIdx = 0; for (Map.Entry<Integer, CTRPrElt> me : formats.entrySet()) { int runEndIdx = me.getKey(); CTRElt run = stf.addNewR(); String fragment = text.substring(runStartIdx, runEndIdx); run.setT(fragment); preserveSpaces(run.xgetT()); CTRPrElt fmt = me.getValue(); if (fmt != null) { run.setRPr(fmt); } runStartIdx = runEndIdx; } return stf; }
/** * Add an entry to this Shared String table (a new value is appended to the end). * * <p> * If the Shared String table already contains this <code>CTRst</code> bean, its index is returned. * Otherwise a new entry is aded. * </p> * * @param st the entry to add * @return index the index of added entry * @deprecated use <code>addSharedStringItem(RichTextString string)</code> instead */ @Removal(version = "4.2") //make private in 4.2 public int addEntry(CTRst st) { String s = xmlText(st); count++; if (stmap.containsKey(s)) { return stmap.get(s); } uniqueCount++; //create a CTRst bean attached to this SstDocument and copy the argument CTRst into it CTRst newSt = _sstDoc.getSst().addNewSi(); newSt.set(st); int idx = strings.size(); stmap.put(s, idx); strings.add(newSt); return idx; }
/** * Returns the plain string representation. */ public String getString() { if(st.sizeOfRArray() == 0) { return utfDecode(st.getT()); } StringBuilder buf = new StringBuilder(); //noinspection deprecation - for performance reasons! for(CTRElt r : st.getRArray()){ buf.append(r.getT()); } return utfDecode(buf.toString()); }
TreeMap<Integer, CTRPrElt> getFormatMap(CTRst entry){ int length = 0; TreeMap<Integer, CTRPrElt> formats = new TreeMap<>(); //noinspection deprecation - for performance reasons! for (CTRElt r : entry.getRArray()) { String txt = r.getT(); CTRPrElt fmt = r.getRPr(); length += txt.length(); formats.put(length, fmt); } return formats; }
ctstr.setT("DataRow " + i); int sRef = sharedstringstable.addEntry(ctstr); ctcell.setT(STCellType.S);
public SST(UTF8OutputStream out,SharedStringsTable strings) throws IOException{ this.out = out; long cnt = 0; for( CTRst item : strings.getItems()){ if(item != null){ cache.put(item.getT(),cnt); write(out,item.getT()); cnt++; } } position = cnt; unique = strings.getUniqueCount(); count = strings.getCount(); }
/** * @return The number of formatting runs used. */ public int numFormattingRuns() { return st.sizeOfRArray(); }
CTRst buildCTRst(String text, TreeMap<Integer, CTRPrElt> formats){ if(text.length() != formats.lastKey()) { throw new IllegalArgumentException("Text length was " + text.length() + " but the last format index was " + formats.lastKey()); } CTRst st = CTRst.Factory.newInstance(); int runStartIdx = 0; for (Iterator<Integer> it = formats.keySet().iterator(); it.hasNext();) { int runEndIdx = it.next(); CTRElt run = st.addNewR(); String fragment = text.substring(runStartIdx, runEndIdx); run.setT(fragment); preserveSpaces(run.xgetT()); CTRPrElt fmt = formats.get(runEndIdx); if(fmt != null) run.setRPr(fmt); runStartIdx = runEndIdx; } return st; }
/** * Add an entry to this Shared String table (a new value is appened to the end). * * <p> * If the Shared String table already contains this <code>CTRst</code> bean, its index is returned. * Otherwise a new entry is aded. * </p> * * @param st the entry to add * @return index the index of added entry */ public int addEntry(CTRst st) { String s = getKey(st); count++; if (stmap.containsKey(s)) { return stmap.get(s); } uniqueCount++; //create a CTRst bean attached to this SstDocument and copy the argument CTRst into it CTRst newSt = _sstDoc.getSst().addNewSi(); newSt.set(st); int idx = strings.size(); stmap.put(s, idx); strings.add(newSt); return idx; } /**
/** * 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); }