Search in sources :

Example 1 with CTRst

use of org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRst in project poi by apache.

the class SharedStringsTable method addEntry.

/**
     * 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;
}
Also used : CTRst(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRst)

Example 2 with CTRst

use of org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRst in project poi by apache.

the class TestSharedStringsTable method testCreateNew.

public void testCreateNew() {
    SharedStringsTable sst = new SharedStringsTable();
    CTRst st;
    int idx;
    // Check defaults
    assertNotNull(sst.getItems());
    assertEquals(0, sst.getItems().size());
    assertEquals(0, sst.getCount());
    assertEquals(0, sst.getUniqueCount());
    st = CTRst.Factory.newInstance();
    st.setT("Hello, World!");
    idx = sst.addEntry(st);
    assertEquals(0, idx);
    assertEquals(1, sst.getCount());
    assertEquals(1, sst.getUniqueCount());
    //add the same entry again
    idx = sst.addEntry(st);
    assertEquals(0, idx);
    assertEquals(2, sst.getCount());
    assertEquals(1, sst.getUniqueCount());
    //and again
    idx = sst.addEntry(st);
    assertEquals(0, idx);
    assertEquals(3, sst.getCount());
    assertEquals(1, sst.getUniqueCount());
    st = CTRst.Factory.newInstance();
    st.setT("Second string");
    idx = sst.addEntry(st);
    assertEquals(1, idx);
    assertEquals(4, sst.getCount());
    assertEquals(2, sst.getUniqueCount());
    //add the same entry again
    idx = sst.addEntry(st);
    assertEquals(1, idx);
    assertEquals(5, sst.getCount());
    assertEquals(2, sst.getUniqueCount());
    st = CTRst.Factory.newInstance();
    CTRElt r = st.addNewR();
    CTRPrElt pr = r.addNewRPr();
    //red
    pr.addNewColor().setRgb(new byte[] { (byte) 0xFF, 0, 0 });
    //bold
    pr.addNewI().setVal(true);
    //italic
    pr.addNewB().setVal(true);
    r.setT("Second string");
    idx = sst.addEntry(st);
    assertEquals(2, idx);
    assertEquals(6, sst.getCount());
    assertEquals(3, sst.getUniqueCount());
    idx = sst.addEntry(st);
    assertEquals(2, idx);
    assertEquals(7, sst.getCount());
    assertEquals(3, sst.getUniqueCount());
    //OK. the sst table is filled, check the contents
    assertEquals(3, sst.getItems().size());
    assertEquals("Hello, World!", new XSSFRichTextString(sst.getEntryAt(0)).toString());
    assertEquals("Second string", new XSSFRichTextString(sst.getEntryAt(1)).toString());
    assertEquals("Second string", new XSSFRichTextString(sst.getEntryAt(2)).toString());
}
Also used : XSSFRichTextString(org.apache.poi.xssf.usermodel.XSSFRichTextString) CTRst(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRst) CTRElt(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRElt) CTRPrElt(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRPrElt)

Example 3 with CTRst

use of org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRst in project poi by apache.

the class TestSharedStringsTable method testReadWrite.

public void testReadWrite() throws IOException {
    XSSFWorkbook wb1 = XSSFTestDataSamples.openSampleWorkbook("sample.xlsx");
    SharedStringsTable sst1 = wb1.getSharedStringSource();
    //serialize, read back and compare with the original
    XSSFWorkbook wb2 = XSSFTestDataSamples.writeOutAndReadBack(wb1);
    SharedStringsTable sst2 = wb2.getSharedStringSource();
    assertEquals(sst1.getCount(), sst2.getCount());
    assertEquals(sst1.getUniqueCount(), sst2.getUniqueCount());
    List<CTRst> items1 = sst1.getItems();
    List<CTRst> items2 = sst2.getItems();
    assertEquals(items1.size(), items2.size());
    for (int i = 0; i < items1.size(); i++) {
        CTRst st1 = items1.get(i);
        CTRst st2 = items2.get(i);
        assertEquals(st1.toString(), st2.toString());
    }
    XSSFWorkbook wb3 = XSSFTestDataSamples.writeOutAndReadBack(wb2);
    assertNotNull(wb3);
    wb3.close();
    wb2.close();
    wb1.close();
}
Also used : CTRst(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRst) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook)

Example 4 with CTRst

use of org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRst in project poi by apache.

the class TestSXSSFCell method testPreserveSpaces.

@Test
public void testPreserveSpaces() throws IOException {
    String[] samplesWithSpaces = { " POI", "POI ", " POI ", "\nPOI", "\n\nPOI \n" };
    for (String str : samplesWithSpaces) {
        Workbook swb = _testDataProvider.createWorkbook();
        Cell sCell = swb.createSheet().createRow(0).createCell(0);
        sCell.setCellValue(str);
        assertEquals(sCell.getStringCellValue(), str);
        // read back as XSSF and check that xml:spaces="preserve" is set
        XSSFWorkbook xwb = (XSSFWorkbook) _testDataProvider.writeOutAndReadBack(swb);
        XSSFCell xCell = xwb.getSheetAt(0).getRow(0).getCell(0);
        CTRst is = xCell.getCTCell().getIs();
        XmlCursor c = is.newCursor();
        c.toNextToken();
        String t = c.getAttributeText(new QName("http://www.w3.org/XML/1998/namespace", "space"));
        c.dispose();
        assertEquals("expected xml:spaces=\"preserve\" \"" + str + "\"", "preserve", t);
        xwb.close();
        swb.close();
    }
}
Also used : CTRst(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRst) QName(javax.xml.namespace.QName) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) XSSFCell(org.apache.poi.xssf.usermodel.XSSFCell) BaseTestXCell(org.apache.poi.ss.usermodel.BaseTestXCell) Cell(org.apache.poi.ss.usermodel.Cell) XSSFCell(org.apache.poi.xssf.usermodel.XSSFCell) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) Workbook(org.apache.poi.ss.usermodel.Workbook) XmlCursor(org.apache.xmlbeans.XmlCursor) Test(org.junit.Test)

Example 5 with CTRst

use of org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRst in project poi by apache.

the class XSSFRichTextString method getFormatMap.

TreeMap<Integer, CTRPrElt> getFormatMap(CTRst entry) {
    int length = 0;
    TreeMap<Integer, CTRPrElt> formats = new TreeMap<Integer, CTRPrElt>();
    //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;
}
Also used : CTRElt(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRElt) CTRPrElt(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRPrElt) RichTextString(org.apache.poi.ss.usermodel.RichTextString)

Aggregations

CTRst (org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRst)14 CTRPrElt (org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRPrElt)5 RichTextString (org.apache.poi.ss.usermodel.RichTextString)3 CTRElt (org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRElt)3 XSSFWorkbook (org.apache.poi.xssf.usermodel.XSSFWorkbook)2 Test (org.junit.Test)2 IOException (java.io.IOException)1 QName (javax.xml.namespace.QName)1 OPCPackage (org.apache.poi.openxml4j.opc.OPCPackage)1 PackagePart (org.apache.poi.openxml4j.opc.PackagePart)1 BaseTestXCell (org.apache.poi.ss.usermodel.BaseTestXCell)1 Cell (org.apache.poi.ss.usermodel.Cell)1 Workbook (org.apache.poi.ss.usermodel.Workbook)1 CellAddress (org.apache.poi.ss.util.CellAddress)1 SharedStringsTable (org.apache.poi.xssf.model.SharedStringsTable)1 XSSFCell (org.apache.poi.xssf.usermodel.XSSFCell)1 XSSFRichTextString (org.apache.poi.xssf.usermodel.XSSFRichTextString)1 XmlCursor (org.apache.xmlbeans.XmlCursor)1 XmlException (org.apache.xmlbeans.XmlException)1 CTComment (org.openxmlformats.schemas.spreadsheetml.x2006.main.CTComment)1