Search in sources :

Example 1 with CTTable

use of org.openxmlformats.schemas.drawingml.x2006.main.CTTable in project poi by apache.

the class CreateTable method main.

public static void main(String[] args) throws IOException {
    Workbook wb = new XSSFWorkbook();
    XSSFSheet sheet = (XSSFSheet) wb.createSheet();
    //Create 
    XSSFTable table = sheet.createTable();
    table.setDisplayName("Test");
    CTTable cttable = table.getCTTable();
    //Style configurations
    CTTableStyleInfo style = cttable.addNewTableStyleInfo();
    style.setName("TableStyleMedium2");
    style.setShowColumnStripes(false);
    style.setShowRowStripes(true);
    //Set which area the table should be placed in
    AreaReference reference = new AreaReference(new CellReference(0, 0), new CellReference(2, 2));
    cttable.setRef(reference.formatAsString());
    cttable.setId(1);
    cttable.setName("Test");
    cttable.setTotalsRowCount(1);
    CTTableColumns columns = cttable.addNewTableColumns();
    columns.setCount(3);
    CTTableColumn column;
    XSSFRow row;
    XSSFCell cell;
    for (int i = 0; i < 3; i++) {
        //Create column
        column = columns.addNewTableColumn();
        column.setName("Column");
        column.setId(i + 1);
        //Create row
        row = sheet.createRow(i);
        for (int j = 0; j < 3; j++) {
            //Create cell
            cell = row.createCell(j);
            if (i == 0) {
                cell.setCellValue("Column" + j);
            } else {
                cell.setCellValue("0");
            }
        }
    }
    FileOutputStream fileOut = new FileOutputStream("ooxml-table.xlsx");
    wb.write(fileOut);
    fileOut.close();
    wb.close();
}
Also used : AreaReference(org.apache.poi.ss.util.AreaReference) CTTableColumns(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTTableColumns) CellReference(org.apache.poi.ss.util.CellReference) XSSFTable(org.apache.poi.xssf.usermodel.XSSFTable) CTTableColumn(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTTableColumn) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) Workbook(org.apache.poi.ss.usermodel.Workbook) XSSFSheet(org.apache.poi.xssf.usermodel.XSSFSheet) XSSFRow(org.apache.poi.xssf.usermodel.XSSFRow) CTTableStyleInfo(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTTableStyleInfo) FileOutputStream(java.io.FileOutputStream) CTTable(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTTable) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) XSSFCell(org.apache.poi.xssf.usermodel.XSSFCell)

Example 2 with CTTable

use of org.openxmlformats.schemas.drawingml.x2006.main.CTTable in project poi by apache.

the class XSLFTable method prototype.

static CTGraphicalObjectFrame prototype(int shapeId) {
    CTGraphicalObjectFrame frame = CTGraphicalObjectFrame.Factory.newInstance();
    CTGraphicalObjectFrameNonVisual nvGr = frame.addNewNvGraphicFramePr();
    CTNonVisualDrawingProps cnv = nvGr.addNewCNvPr();
    cnv.setName("Table " + shapeId);
    cnv.setId(shapeId + 1);
    nvGr.addNewCNvGraphicFramePr().addNewGraphicFrameLocks().setNoGrp(true);
    nvGr.addNewNvPr();
    frame.addNewXfrm();
    CTGraphicalObjectData gr = frame.addNewGraphic().addNewGraphicData();
    XmlCursor grCur = gr.newCursor();
    grCur.toNextToken();
    grCur.beginElement(new QName(DRAWINGML_URI, "tbl"));
    CTTable tbl = CTTable.Factory.newInstance();
    tbl.addNewTblPr();
    tbl.addNewTblGrid();
    XmlCursor tblCur = tbl.newCursor();
    tblCur.moveXmlContents(grCur);
    tblCur.dispose();
    grCur.dispose();
    gr.setUri(TABLE_URI);
    return frame;
}
Also used : CTGraphicalObjectData(org.openxmlformats.schemas.drawingml.x2006.main.CTGraphicalObjectData) CTGraphicalObjectFrame(org.openxmlformats.schemas.presentationml.x2006.main.CTGraphicalObjectFrame) QName(javax.xml.namespace.QName) CTNonVisualDrawingProps(org.openxmlformats.schemas.drawingml.x2006.main.CTNonVisualDrawingProps) CTTable(org.openxmlformats.schemas.drawingml.x2006.main.CTTable) CTGraphicalObjectFrameNonVisual(org.openxmlformats.schemas.presentationml.x2006.main.CTGraphicalObjectFrameNonVisual) XmlCursor(org.apache.xmlbeans.XmlCursor)

Example 3 with CTTable

use of org.openxmlformats.schemas.drawingml.x2006.main.CTTable in project poi by apache.

the class TestXSSFTable method bug56274.

@Test
public void bug56274() throws IOException {
    // read sample file
    XSSFWorkbook wb1 = XSSFTestDataSamples.openSampleWorkbook("56274.xlsx");
    // read the original sheet header order
    XSSFRow row = wb1.getSheetAt(0).getRow(0);
    List<String> headers = new ArrayList<String>();
    for (Cell cell : row) {
        headers.add(cell.getStringCellValue());
    }
    // save the worksheet as-is using SXSSF
    File outputFile = TempFile.createTempFile("poi-56274", ".xlsx");
    SXSSFWorkbook outputWorkbook = new SXSSFWorkbook(wb1);
    FileOutputStream fos = new FileOutputStream(outputFile);
    outputWorkbook.write(fos);
    fos.close();
    outputWorkbook.close();
    // re-read the saved file and make sure headers in the xml are in the original order
    FileInputStream fis = new FileInputStream(outputFile);
    XSSFWorkbook wb2 = new XSSFWorkbook(fis);
    fis.close();
    CTTable ctTable = wb2.getSheetAt(0).getTables().get(0).getCTTable();
    CTTableColumn[] ctTableColumnArray = ctTable.getTableColumns().getTableColumnArray();
    assertEquals("number of headers in xml table should match number of header cells in worksheet", headers.size(), ctTableColumnArray.length);
    for (int i = 0; i < headers.size(); i++) {
        assertEquals("header name in xml table should match number of header cells in worksheet", headers.get(i), ctTableColumnArray[i].getName());
    }
    assertTrue(outputFile.delete());
    wb2.close();
    wb1.close();
}
Also used : ArrayList(java.util.ArrayList) CTTableColumn(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTTableColumn) FileInputStream(java.io.FileInputStream) SXSSFWorkbook(org.apache.poi.xssf.streaming.SXSSFWorkbook) FileOutputStream(java.io.FileOutputStream) CTTable(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTTable) SXSSFWorkbook(org.apache.poi.xssf.streaming.SXSSFWorkbook) Cell(org.apache.poi.ss.usermodel.Cell) TempFile(org.apache.poi.util.TempFile) File(java.io.File) Test(org.junit.Test)

Example 4 with CTTable

use of org.openxmlformats.schemas.drawingml.x2006.main.CTTable in project poi by apache.

the class TestXSSFTable method getRowCount.

@Test
public void getRowCount() {
    XSSFWorkbook wb = new XSSFWorkbook();
    XSSFSheet sh = wb.createSheet();
    XSSFTable table = sh.createTable();
    CTTable ctTable = table.getCTTable();
    assertEquals(0, table.getRowCount());
    ctTable.setRef("B2:B2");
    // update cell references to clear the cache
    table.updateReferences();
    assertEquals(1, table.getRowCount());
    ctTable.setRef("B2:B12");
    // update cell references to clear the cache
    table.updateReferences();
    assertEquals(11, table.getRowCount());
}
Also used : CTTable(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTTable) SXSSFWorkbook(org.apache.poi.xssf.streaming.SXSSFWorkbook) Test(org.junit.Test)

Example 5 with CTTable

use of org.openxmlformats.schemas.drawingml.x2006.main.CTTable in project poi by apache.

the class TestXSSFTable method testCTTableStyleInfo.

@Test
public void testCTTableStyleInfo() throws IOException {
    XSSFWorkbook outputWorkbook = new XSSFWorkbook();
    XSSFSheet sheet = outputWorkbook.createSheet();
    //Create
    XSSFTable outputTable = sheet.createTable();
    outputTable.setDisplayName("Test");
    CTTable outputCTTable = outputTable.getCTTable();
    //Style configurations
    CTTableStyleInfo outputStyleInfo = outputCTTable.addNewTableStyleInfo();
    outputStyleInfo.setName("TableStyleLight1");
    outputStyleInfo.setShowColumnStripes(false);
    outputStyleInfo.setShowRowStripes(true);
    XSSFWorkbook inputWorkbook = XSSFTestDataSamples.writeOutAndReadBack(outputWorkbook);
    List<XSSFTable> tables = inputWorkbook.getSheetAt(0).getTables();
    assertEquals("Tables number", 1, tables.size());
    XSSFTable inputTable = tables.get(0);
    assertEquals("Table display name", outputTable.getDisplayName(), inputTable.getDisplayName());
    CTTableStyleInfo inputStyleInfo = inputTable.getCTTable().getTableStyleInfo();
    assertEquals("Style name", outputStyleInfo.getName(), inputStyleInfo.getName());
    assertEquals("Show column stripes", outputStyleInfo.getShowColumnStripes(), inputStyleInfo.getShowColumnStripes());
    assertEquals("Show row stripes", outputStyleInfo.getShowRowStripes(), inputStyleInfo.getShowRowStripes());
    inputWorkbook.close();
    outputWorkbook.close();
}
Also used : CTTableStyleInfo(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTTableStyleInfo) CTTable(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTTable) SXSSFWorkbook(org.apache.poi.xssf.streaming.SXSSFWorkbook) Test(org.junit.Test)

Aggregations

CTTable (org.openxmlformats.schemas.spreadsheetml.x2006.main.CTTable)5 SXSSFWorkbook (org.apache.poi.xssf.streaming.SXSSFWorkbook)4 Test (org.junit.Test)4 CTTable (org.openxmlformats.schemas.drawingml.x2006.main.CTTable)4 FileOutputStream (java.io.FileOutputStream)2 ArrayList (java.util.ArrayList)2 CellReference (org.apache.poi.ss.util.CellReference)2 XmlCursor (org.apache.xmlbeans.XmlCursor)2 CTGraphicalObjectData (org.openxmlformats.schemas.drawingml.x2006.main.CTGraphicalObjectData)2 CTGraphicalObjectFrame (org.openxmlformats.schemas.presentationml.x2006.main.CTGraphicalObjectFrame)2 CTTableColumn (org.openxmlformats.schemas.spreadsheetml.x2006.main.CTTableColumn)2 CTTableStyleInfo (org.openxmlformats.schemas.spreadsheetml.x2006.main.CTTableStyleInfo)2 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 QName (javax.xml.namespace.QName)1 POIXMLException (org.apache.poi.POIXMLException)1 DrawPaint (org.apache.poi.sl.draw.DrawPaint)1 SolidPaint (org.apache.poi.sl.usermodel.PaintStyle.SolidPaint)1 Cell (org.apache.poi.ss.usermodel.Cell)1 Workbook (org.apache.poi.ss.usermodel.Workbook)1