Search in sources :

Example 1 with CTTableColumn

use of org.openxmlformats.schemas.spreadsheetml.x2006.main.CTTableColumn 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 CTTableColumn

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

the class XSSFTable method getXmlColumnPrs.

/**
     * Note this list is static - once read, it does not notice later changes to the underlying column structures
     * To clear the cache, call {@link #updateHeaders}
     * @return List of XSSFXmlColumnPr
     */
public List<XSSFXmlColumnPr> getXmlColumnPrs() {
    if (xmlColumnPr == null) {
        xmlColumnPr = new ArrayList<XSSFXmlColumnPr>();
        for (CTTableColumn column : getTableColumns()) {
            if (column.getXmlColumnPr() != null) {
                XSSFXmlColumnPr columnPr = new XSSFXmlColumnPr(this, column, column.getXmlColumnPr());
                xmlColumnPr.add(columnPr);
            }
        }
    }
    return xmlColumnPr;
}
Also used : XSSFXmlColumnPr(org.apache.poi.xssf.usermodel.helpers.XSSFXmlColumnPr) CTTableColumn(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTTableColumn)

Example 3 with CTTableColumn

use of org.openxmlformats.schemas.spreadsheetml.x2006.main.CTTableColumn 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 CTTableColumn

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

the class XSSFTable method getCommonXpath.

/**
     * 
     * Calculates the xpath of the root element for the table. This will be the common part
     * of all the mapping's xpaths
     * Note: this function caches the result for performance. To flush the cache {@link #updateHeaders()} must be called.
     * 
     * @return the xpath of the table's root element
     */
public String getCommonXpath() {
    if (commonXPath == null) {
        String[] commonTokens = {};
        for (CTTableColumn column : getTableColumns()) {
            if (column.getXmlColumnPr() != null) {
                String xpath = column.getXmlColumnPr().getXpath();
                String[] tokens = xpath.split("/");
                if (commonTokens.length == 0) {
                    commonTokens = tokens;
                } else {
                    final int maxLength = Math.min(commonTokens.length, tokens.length);
                    for (int i = 0; i < maxLength; i++) {
                        if (!commonTokens[i].equals(tokens[i])) {
                            List<String> subCommonTokens = Arrays.asList(commonTokens).subList(0, i);
                            String[] container = {};
                            commonTokens = subCommonTokens.toArray(container);
                            break;
                        }
                    }
                }
            }
        }
        commonTokens[0] = "";
        commonXPath = StringUtil.join(commonTokens, "/");
    }
    return commonXPath;
}
Also used : CTTableColumn(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTTableColumn)

Example 5 with CTTableColumn

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

the class XSSFTable method findColumnIndex.

/**
     * Gets the relative column index of a column in this table having the header name <code>column</code>.
     * The column index is relative to the left-most column in the table, 0-indexed.
     * Returns <code>-1</code> if <code>column</code> is not a header name in table.
     *
     * Column Header names are case-insensitive
     *
     * Note: this function caches column names for performance. To flush the cache (because columns
     * have been moved or column headers have been changed), {@link #updateHeaders()} must be called.
     *
     * @since 3.15 beta 2
     */
public int findColumnIndex(String columnHeader) {
    if (columnHeader == null)
        return -1;
    if (columnMap == null) {
        // FIXME: replace with org.apache.commons.collections.map.CaseInsensitiveMap
        final int count = getTableColumns().length;
        columnMap = new HashMap<String, Integer>(count * 3 / 2);
        int i = 0;
        for (CTTableColumn column : getTableColumns()) {
            String columnName = column.getName();
            columnMap.put(caseInsensitive(columnName), i);
            i++;
        }
    }
    // Table column names with special characters need a single quote escape
    // but the escape is not present in the column definition
    Integer idx = columnMap.get(caseInsensitive(columnHeader.replace("'", "")));
    return idx == null ? -1 : idx.intValue();
}
Also used : CTTableColumn(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTTableColumn)

Aggregations

CTTableColumn (org.openxmlformats.schemas.spreadsheetml.x2006.main.CTTableColumn)6 FileOutputStream (java.io.FileOutputStream)2 CellReference (org.apache.poi.ss.util.CellReference)2 CTTable (org.openxmlformats.schemas.spreadsheetml.x2006.main.CTTable)2 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 ArrayList (java.util.ArrayList)1 Cell (org.apache.poi.ss.usermodel.Cell)1 Workbook (org.apache.poi.ss.usermodel.Workbook)1 AreaReference (org.apache.poi.ss.util.AreaReference)1 TempFile (org.apache.poi.util.TempFile)1 SXSSFWorkbook (org.apache.poi.xssf.streaming.SXSSFWorkbook)1 XSSFCell (org.apache.poi.xssf.usermodel.XSSFCell)1 XSSFRow (org.apache.poi.xssf.usermodel.XSSFRow)1 XSSFSheet (org.apache.poi.xssf.usermodel.XSSFSheet)1 XSSFTable (org.apache.poi.xssf.usermodel.XSSFTable)1 XSSFWorkbook (org.apache.poi.xssf.usermodel.XSSFWorkbook)1 XSSFXmlColumnPr (org.apache.poi.xssf.usermodel.helpers.XSSFXmlColumnPr)1 Test (org.junit.Test)1 CTTableColumns (org.openxmlformats.schemas.spreadsheetml.x2006.main.CTTableColumns)1