Copying an XSLFTableRow to XSLFTable using Apache POI

65 views Asked by At

I have successfully added/copied rows to XWPFTable using Apache POI but unable to achieve the same in XSLFTable. Following is the code for XWPFTable

    public void addNewTemplateRowToTable(XWPFTableRow rowTemplate, XWPFTable table, int templateRowId, FormLetter formletter, int rowsCount, XWPFDocument document) {
        try {
            AtomicInteger rowCount = new AtomicInteger();
            //calculating the position to add a new row to the table.
            int newRowPosition = templateRowId+1;
            while(rowCount.get()<rowsCount) {
                XWPFTableRow oldRow = rowTemplate;
                CTRow ctrow = CTRow.Factory.parse(oldRow.getCtRow().newInputStream());
                XWPFTableRow newRow = new XWPFTableRow(ctrow, table);
                List<XWPFTableCell> cells = new ArrayList<XWPFTableCell>();
                newRow.getTableCells().forEach(cell-> {
                    cells.add(cell);
                });
                for(XWPFTableCell cell : cells) {
                    if(cell.getText().contains(HASH_DELIMITER)) {
                        String cellText = cell.getText().trim();
                        //split the text with # sign to get the key.
                        String fieldName = getFieldNameFromPlaceHolder(cellText);
                        String[] textArray = fieldName.split(HASH_DELIMITER);
                        String key = textArray[0];
                        FormLetterTableField tableField = (FormLetterTableField) formletter.getFormLetterFields().get(key);
                        List<FormLetterData> tableRowData = tableField.getTableRowData();
                        Map<String, Object> formletterFieldsRow = tableRowData.get(rowCount.get()).getFormLetterFields();
                        replaceXWPFTableCellValues(cell, formletterFieldsRow, document);
                    } else if(cell.getText().startsWith(PLACEHOLDER_FIELD_PREFIX)&&cell.getText().endsWith(PLACEHOLDER_FIELD_SUFFIX)) {
                        Map<String, Object> fields = formletter.getFormLetterFields();
                        replaceXWPFTableCellValues(cell, fields, document);
                    }
                }
                //adding new row to the table at newRowPosition.
                table.addRow(newRow, newRowPosition);
                newRowPosition+=1;
                rowCount.getAndIncrement();
            }
            table.removeRow(templateRowId);
        } catch(Exception e) {
            e.printStackTrace();
            log.error(e.getMessage(), e);
            return;
        }
    }

But I want to achieve the same for XSLFTable because I am currently working with .pptx files. The issue is there is no row.getCTRow() function in XSLFTableRow and also I cannot create a new XSLFTableRow like I did in XWPFTableRow newRow = new XWPFTableRow(ctrow, table); because the XSLFTableRow constructor is invisible by default and is not public. Can anyone please guide me how can I achieve the same functionality with XSLFTable for copying XSLFTableRow as I did in XWPFTable as shown in the code above?

0

There are 0 answers