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();
}
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;
}
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();
}
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());
}
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();
}
Aggregations