Search in sources :

Example 11 with CTWorksheet

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

the class TestColumnHelper method testAddCleanColIntoCols.

@Test
public void testAddCleanColIntoCols() {
    CTWorksheet worksheet = CTWorksheet.Factory.newInstance();
    ColumnHelper helper = new ColumnHelper(worksheet);
    CTCols cols1 = CTCols.Factory.newInstance();
    CTCol col1 = cols1.addNewCol();
    col1.setMin(1);
    col1.setMax(1);
    col1.setWidth(88);
    col1.setHidden(true);
    CTCol col2 = cols1.addNewCol();
    col2.setMin(2);
    col2.setMax(3);
    CTCol col3 = cols1.addNewCol();
    col3.setMin(13);
    col3.setMax(16750);
    assertEquals(3, cols1.sizeOfColArray());
    CTCol col4 = cols1.addNewCol();
    col4.setMin(8);
    col4.setMax(9);
    assertEquals(4, cols1.sizeOfColArray());
    // No overlap
    helper.addCleanColIntoCols(cols1, createCol(4, 5));
    assertEquals(5, cols1.sizeOfColArray());
    // Overlaps with 8 - 9 (overlap and after replacements required)
    CTCol col6 = createCol(8, 11);
    col6.setHidden(true);
    helper.addCleanColIntoCols(cols1, col6);
    assertEquals(6, cols1.sizeOfColArray());
    // Overlaps with 8 - 9 (before and overlap replacements required)
    CTCol col7 = createCol(6, 8);
    col7.setWidth(17.0);
    helper.addCleanColIntoCols(cols1, col7);
    assertEquals(8, cols1.sizeOfColArray());
    // Overlaps with 13 - 16750 (before, overlap and after replacements required)
    helper.addCleanColIntoCols(cols1, createCol(20, 30));
    assertEquals(10, cols1.sizeOfColArray());
    // Overlaps with 20 - 30 (before, overlap and after replacements required)
    helper.addCleanColIntoCols(cols1, createCol(25, 27));
    // TODO - assert something interesting
    assertEquals(12, cols1.sizeOfColArray());
    assertEquals(1, cols1.getColArray(0).getMin());
    assertEquals(16750, cols1.getColArray(11).getMax());
}
Also used : CTCols(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCols) CTWorksheet(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorksheet) CTCol(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCol) Test(org.junit.Test)

Example 12 with CTWorksheet

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

the class TestColumnHelper method testCloneCol.

@Test
public void testCloneCol() {
    CTWorksheet worksheet = CTWorksheet.Factory.newInstance();
    ColumnHelper helper = new ColumnHelper(worksheet);
    CTCols cols = CTCols.Factory.newInstance();
    CTCol col = CTCol.Factory.newInstance();
    col.setMin(2);
    col.setMax(8);
    col.setHidden(true);
    col.setWidth(13.4);
    CTCol newCol = helper.cloneCol(cols, col);
    assertEquals(2, newCol.getMin());
    assertEquals(8, newCol.getMax());
    assertTrue(newCol.getHidden());
    assertEquals(13.4, newCol.getWidth(), 0.0);
}
Also used : CTCols(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCols) CTWorksheet(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorksheet) CTCol(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCol) Test(org.junit.Test)

Example 13 with CTWorksheet

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

the class XSSFWorkbook method cloneSheet.

/**
     * Create an XSSFSheet from an existing sheet in the XSSFWorkbook.
     *  The cloned sheet is a deep copy of the original but with a new given
     *  name.
     *
     * @param sheetNum The index of the sheet to clone
     * @param newName The name to set for the newly created sheet
     * @return XSSFSheet representing the cloned sheet.
     * @throws IllegalArgumentException if the sheet index or the sheet
     *         name is invalid
     * @throws POIXMLException if there were errors when cloning
     */
public XSSFSheet cloneSheet(int sheetNum, String newName) {
    validateSheetIndex(sheetNum);
    XSSFSheet srcSheet = sheets.get(sheetNum);
    if (newName == null) {
        String srcName = srcSheet.getSheetName();
        newName = getUniqueSheetName(srcName);
    } else {
        validateSheetName(newName);
    }
    XSSFSheet clonedSheet = createSheet(newName);
    // copy sheet's relations
    List<RelationPart> rels = srcSheet.getRelationParts();
    // if the sheet being cloned has a drawing then rememebr it and re-create it too
    XSSFDrawing dg = null;
    for (RelationPart rp : rels) {
        POIXMLDocumentPart r = rp.getDocumentPart();
        // do not copy the drawing relationship, it will be re-created
        if (r instanceof XSSFDrawing) {
            dg = (XSSFDrawing) r;
            continue;
        }
        addRelation(rp, clonedSheet);
    }
    try {
        for (PackageRelationship pr : srcSheet.getPackagePart().getRelationships()) {
            if (pr.getTargetMode() == TargetMode.EXTERNAL) {
                clonedSheet.getPackagePart().addExternalRelationship(pr.getTargetURI().toASCIIString(), pr.getRelationshipType(), pr.getId());
            }
        }
    } catch (InvalidFormatException e) {
        throw new POIXMLException("Failed to clone sheet", e);
    }
    try {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        srcSheet.write(out);
        clonedSheet.read(new ByteArrayInputStream(out.toByteArray()));
    } catch (IOException e) {
        throw new POIXMLException("Failed to clone sheet", e);
    }
    CTWorksheet ct = clonedSheet.getCTWorksheet();
    if (ct.isSetLegacyDrawing()) {
        logger.log(POILogger.WARN, "Cloning sheets with comments is not yet supported.");
        ct.unsetLegacyDrawing();
    }
    if (ct.isSetPageSetup()) {
        logger.log(POILogger.WARN, "Cloning sheets with page setup is not yet supported.");
        ct.unsetPageSetup();
    }
    clonedSheet.setSelected(false);
    // clone the sheet drawing alongs with its relationships
    if (dg != null) {
        if (ct.isSetDrawing()) {
            // unset the existing reference to the drawing,
            // so that subsequent call of clonedSheet.createDrawingPatriarch() will create a new one
            ct.unsetDrawing();
        }
        XSSFDrawing clonedDg = clonedSheet.createDrawingPatriarch();
        // copy drawing contents
        clonedDg.getCTDrawing().set(dg.getCTDrawing());
        clonedDg = clonedSheet.createDrawingPatriarch();
        // Clone drawing relations
        List<RelationPart> srcRels = srcSheet.createDrawingPatriarch().getRelationParts();
        for (RelationPart rp : srcRels) {
            addRelation(rp, clonedDg);
        }
    }
    return clonedSheet;
}
Also used : POIXMLDocumentPart(org.apache.poi.POIXMLDocumentPart) CTWorksheet(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorksheet) POIXMLException(org.apache.poi.POIXMLException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) InvalidFormatException(org.apache.poi.openxml4j.exceptions.InvalidFormatException) PackageRelationship(org.apache.poi.openxml4j.opc.PackageRelationship) ByteArrayInputStream(java.io.ByteArrayInputStream)

Example 14 with CTWorksheet

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

the class TestXSSFSheet method createRow.

/**
     * Rows and cells can be created in random order,
     * but CTRows are kept in ascending order
     */
@Override
@Test
public void createRow() throws IOException {
    XSSFWorkbook wb1 = new XSSFWorkbook();
    XSSFSheet sheet = wb1.createSheet();
    CTWorksheet wsh = sheet.getCTWorksheet();
    CTSheetData sheetData = wsh.getSheetData();
    assertEquals(0, sheetData.sizeOfRowArray());
    XSSFRow row1 = sheet.createRow(2);
    row1.createCell(2);
    row1.createCell(1);
    XSSFRow row2 = sheet.createRow(1);
    row2.createCell(2);
    row2.createCell(1);
    row2.createCell(0);
    XSSFRow row3 = sheet.createRow(0);
    row3.createCell(3);
    row3.createCell(0);
    row3.createCell(2);
    row3.createCell(5);
    CTRow[] xrow = sheetData.getRowArray();
    assertEquals(3, xrow.length);
    //rows are sorted: {0, 1, 2}
    assertEquals(4, xrow[0].sizeOfCArray());
    assertEquals(1, xrow[0].getR());
    assertTrue(xrow[0].equals(row3.getCTRow()));
    assertEquals(3, xrow[1].sizeOfCArray());
    assertEquals(2, xrow[1].getR());
    assertTrue(xrow[1].equals(row2.getCTRow()));
    assertEquals(2, xrow[2].sizeOfCArray());
    assertEquals(3, xrow[2].getR());
    assertTrue(xrow[2].equals(row1.getCTRow()));
    CTCell[] xcell = xrow[0].getCArray();
    assertEquals("D1", xcell[0].getR());
    assertEquals("A1", xcell[1].getR());
    assertEquals("C1", xcell[2].getR());
    assertEquals("F1", xcell[3].getR());
    //re-creating a row does NOT add extra data to the parent
    row2 = sheet.createRow(1);
    assertEquals(3, sheetData.sizeOfRowArray());
    //existing cells are invalidated
    assertEquals(0, sheetData.getRowArray(1).sizeOfCArray());
    assertEquals(0, row2.getPhysicalNumberOfCells());
    XSSFWorkbook wb2 = XSSFTestDataSamples.writeOutAndReadBack(wb1);
    wb1.close();
    sheet = wb2.getSheetAt(0);
    wsh = sheet.getCTWorksheet();
    xrow = sheetData.getRowArray();
    assertEquals(3, xrow.length);
    //rows are sorted: {0, 1, 2}
    assertEquals(4, xrow[0].sizeOfCArray());
    assertEquals(1, xrow[0].getR());
    //cells are now sorted
    xcell = xrow[0].getCArray();
    assertEquals("A1", xcell[0].getR());
    assertEquals("C1", xcell[1].getR());
    assertEquals("D1", xcell[2].getR());
    assertEquals("F1", xcell[3].getR());
    assertEquals(0, xrow[1].sizeOfCArray());
    assertEquals(2, xrow[1].getR());
    assertEquals(2, xrow[2].sizeOfCArray());
    assertEquals(3, xrow[2].getR());
    wb2.close();
}
Also used : CTWorksheet(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorksheet) CTCell(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCell) CTSheetData(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTSheetData) SXSSFWorkbook(org.apache.poi.xssf.streaming.SXSSFWorkbook) CTRow(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRow) Test(org.junit.Test)

Example 15 with CTWorksheet

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

the class TestXSSFPrintSetup method testSetGetCopies.

public void testSetGetCopies() {
    CTWorksheet worksheet = CTWorksheet.Factory.newInstance();
    CTPageSetup pSetup = worksheet.addNewPageSetup();
    pSetup.setCopies(9);
    XSSFPrintSetup printSetup = new XSSFPrintSetup(worksheet);
    assertEquals(9, printSetup.getCopies());
    printSetup.setCopies((short) 15);
    assertEquals(15, pSetup.getCopies());
}
Also used : CTWorksheet(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorksheet) CTPageSetup(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTPageSetup)

Aggregations

CTWorksheet (org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorksheet)32 CTPageSetup (org.openxmlformats.schemas.spreadsheetml.x2006.main.CTPageSetup)13 Test (org.junit.Test)11 CTCol (org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCol)7 SXSSFWorkbook (org.apache.poi.xssf.streaming.SXSSFWorkbook)6 CTCols (org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCols)6 POIXMLException (org.apache.poi.POIXMLException)2 InvalidFormatException (org.apache.poi.openxml4j.exceptions.InvalidFormatException)2 PackageRelationship (org.apache.poi.openxml4j.opc.PackageRelationship)2 CellRangeAddress (org.apache.poi.ss.util.CellRangeAddress)2 StylesTable (org.apache.poi.xssf.model.StylesTable)2 XSSFSheet (org.apache.poi.xssf.usermodel.XSSFSheet)2 XSSFWorkbook (org.apache.poi.xssf.usermodel.XSSFWorkbook)2 CTXf (org.openxmlformats.schemas.spreadsheetml.x2006.main.CTXf)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 QName (javax.xml.namespace.QName)1