Hi I am trying to copy a table from a docx file to another but what happens is that the value of the table are copied down bellow the table in the new document and outside of it (see pictures bellow)
Table in original docx
Talbe in the new docx
As you can see the values of the table are copied outside the table.
I am using Libre Office, apache poi version 3.17 and my computer runs Ubuntu 16.04
The code I am using to perform the copy is the following
public static void copyTable(XWPFDocument input_doc,XWPFDocument output_doc,
int table_index_input, int table_index_output) {
XWPFTable template_table = input_doc.getTables().get(table_index_input);
CTTbl ctTbl = CTTbl.Factory.newInstance(); // Create a new CTTbl for the new table
ctTbl.set(template_table.getCTTbl()); // Copy the template table's CTTbl
XWPFTable new_table = new XWPFTable(ctTbl, output_doc); // Create a new table using the CTTbl upon
output_doc.createParagraph();
output_doc.createTable();// Create a empty table in the document
output_doc.setTable(table_index_output, new_table); // Replace the empty table to table2
}
XWPFTable newTbl = output_doc.insertNewTbl(cursor);
copyTable(table, newTbl);
and the copyTable() method
private void copyTable(XWPFTable source, XWPFTable target) {
target.getCTTbl().setTblPr(source.getCTTbl().getTblPr());
target.getCTTbl().setTblGrid(source.getCTTbl().getTblGrid());
for (int r = 0; r<source.getRows().size(); r++) {
XWPFTableRow targetRow = target.createRow();
XWPFTableRow row = source.getRows().get(r);
targetRow.getCtRow().setTrPr(row.getCtRow().getTrPr());
for (int c=0; c<row.getTableCells().size(); c++) {
//newly created row has 1 cell
XWPFTableCell targetCell = c==0 ? targetRow.getTableCells().get(0) : targetRow.createCell();
XWPFTableCell cell = row.getTableCells().get(c);
targetCell.getCTTc().setTcPr(cell.getCTTc().getTcPr());
XmlCursor cursor = targetCell.getParagraphArray(0).getCTP().newCursor();
for (int p = 0; p < cell.getBodyElements().size(); p++) {
IBodyElement elem = cell.getBodyElements().get(p);
if (elem instanceof XWPFParagraph) {
XWPFParagraph targetPar = targetCell.insertNewParagraph(cursor);
cursor.toNextToken();
XWPFParagraph par = (XWPFParagraph) elem;
copyParagraph(par, targetPar);
} else if (elem instanceof XWPFTable) {
XWPFTable targetTable = targetCell.insertNewTbl(cursor);
XWPFTable table = (XWPFTable) elem;
copyTable(table, targetTable);
cursor.toNextToken();
}
}
//newly created cell has one default paragraph we need to remove
targetCell.removeParagraph(targetCell.getParagraphs().size()-1);
}
}
//newly created table has one row by default. we need to remove the default row.
target.removeRow(0);
}
the copyParagraph()
private void copyParagraph(XWPFParagraph source, XWPFParagraph target) {
target.getCTP().setPPr(source.getCTP().getPPr());
for (int i=0; i<source.getRuns().size(); i++ ) {
XWPFRun run = source.getRuns().get(i);
XWPFRun targetRun = target.createRun();
//copy formatting
targetRun.getCTR().setRPr(run.getCTR().getRPr());
//no images just copy text
targetRun.setText(run.getText(0));
}
}
I think easier and more reliable form of copyTable function would be something like this.
private void copyTable(XWPFTable source, XWPFTable target) {
CTTbl sourceCTTbl = source.getCTTbl();
CTTbl targetCTTbl = target.getCTTbl();
targetCTTbl.setTblPr(sourceCTTbl.getTblPr());
targetCTTbl.setTrArray(sourceCTTbl.getTrArray());
}
(It works for me at least)
Related
I have a table in the docx template.
Depending on the number of objects, I have to duplicate the table as many times as I have objects. Duplicate tables must be after the table from the template.
I have several tables in the template that should behave like this.
XmlCursor take the place of the first table from the template and put the next one there. I want to insert the next table after the previous one, which I added myself, but xmlcursor does not return the table item I added, but returns "STARTDOC"
XmlCursor cursor = docx.getTables().get(pointer).getCTTbl().newCursor();
cursor.toEndToken();
while (cursor.toNextToken() != XmlCursor.TokenType.START) ;
XWPFParagraph newParagraph = docx.insertNewParagraph(cursor);
newParagraph.createRun().setText("", 0);
cursor.toParent();
cursor.toEndToken();
while (cursor.toNextToken() != XmlCursor.TokenType.START) ;
docx.insertNewTbl(cursor);
CTTbl ctTbl = CTTbl.Factory.newInstance();
ctTbl.set(docx.getTables().get(numberTableFromTemplate).getCTTbl());
XWPFTable tableCopy = new XWPFTable(ctTbl, docx);
docx.setTable(index + 1, tableCopy);
Not clear what you are aiming for with the cursor.toParent();. And I also cannot reproduce the issue having only your small code snippet. But having a complete working example may possible help you.
Assuming we have following template:
Then following code:
import java.io.FileOutputStream;
import java.io.FileInputStream;
import org.apache.poi.xwpf.usermodel.*;
import org.apache.xmlbeans.XmlObject;
import org.apache.xmlbeans.XmlCursor;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTbl;
public class WordCopyTableAfterTable {
static XmlCursor setCursorToNextStartToken(XmlObject object) {
XmlCursor cursor = object.newCursor();
cursor.toEndToken(); //Now we are at end of the XmlObject.
//There always must be a next start token.
while(cursor.hasNextToken() && cursor.toNextToken() != org.apache.xmlbeans.XmlCursor.TokenType.START);
//Now we are at the next start token and can insert new things here.
return cursor;
}
static void removeCellValues(XWPFTableCell cell) {
for (XWPFParagraph paragraph : cell.getParagraphs()) {
for (int i = paragraph.getRuns().size()-1; i >= 0; i--) {
paragraph.removeRun(i);
}
}
}
public static void main(String[] args) throws Exception {
//The data. Each row a new table.
String[][] data= new String[][] {
new String[] {"John Doe", "5/23/2019", "1234.56"},
new String[] {"Jane Doe", "12/2/2019", "34.56"},
new String[] {"Marie Template", "9/20/2019", "4.56"},
new String[] {"Hans Template", "10/2/2019", "4567.89"}
};
String value;
XWPFDocument document = new XWPFDocument(new FileInputStream("WordTemplate.docx"));
XWPFTable tableTemplate;
CTTbl cTTblTemplate;
XWPFTable tableCopy;
XWPFTable table;
XWPFTableRow row;
XWPFTableCell cell;
XmlCursor cursor;
XWPFParagraph paragraph;
XWPFRun run;
//get first table (the template)
tableTemplate = document.getTableArray(0);
cTTblTemplate = tableTemplate.getCTTbl();
cursor = setCursorToNextStartToken(cTTblTemplate);
//fill in first data in first table (the template)
for (int c = 0; c < data[0].length; c++) {
value = data[0][c];
row = tableTemplate.getRow(1);
cell = row.getCell(c);
removeCellValues(cell);
cell.setText(value);
}
paragraph = document.insertNewParagraph(cursor); //insert new empty paragraph
cursor = setCursorToNextStartToken(paragraph.getCTP());
//fill in next data, each data row in one table
for (int t = 1; t < data.length; t++) {
table = document.insertNewTbl(cursor); //insert new empty table at position t
cursor = setCursorToNextStartToken(table.getCTTbl());
tableCopy = new XWPFTable((CTTbl)cTTblTemplate.copy(), document); //copy the template table
//fill in data in tableCopy
for (int c = 0; c < data[t].length; c++) {
value = data[t][c];
row = tableCopy.getRow(1);
cell = row.getCell(c);
removeCellValues(cell);
cell.setText(value);
}
document.setTable(t, tableCopy); //set tableCopy at position t instead of table
paragraph = document.insertNewParagraph(cursor); //insert new empty paragraph
cursor = setCursorToNextStartToken(paragraph.getCTP());
}
paragraph = document.insertNewParagraph(cursor);
run = paragraph.createRun();
run.setText("Inserted new text below last table.");
cursor = setCursorToNextStartToken(paragraph.getCTP());
FileOutputStream out = new FileOutputStream("WordResult.docx");
document.write(out);
out.close();
document.close();
}
}
leads to following result:
Is that about what you wanted to achieve?
Please note how I insert the additional tables.
Using table = document.insertNewTbl(cursor); a new empty table is inserted at position t. This table is placed into the document body. So this table must be taken for adjusting the cursor.
Then tableCopy = new XWPFTable((CTTbl)cTTblTemplate.copy(), document); copys the template table. Then this copy is filled with data. And then it is set into the document at position t using document.setTable(t, tableCopy);.
Unfortunately apache poi is incomplete here. XWPFDocument.setTable only sets the internally ArrayLists but not the underlying XML. XWPFDocument.insertNewTbl sets the underlying XML but only using an empty table. So we must do it that ugly complicated way.
I am trying to fetch the cell using named range.But After trying the below code,not able to get consistent cell in a row of the sheet that's getting null exception while using r.getCell().
String cname = "TestName";
Workbook wb = getMyWorkbook(); // retrieve workbook
// retrieve the named range
int namedCellIdx = wb.getNameIndex(cellName);
Name aNamedCell = wb.getNameAt(namedCellIdx);
// retrieve the cell at the named range and test its contents
AreaReference aref = new AreaReference(aNamedCell.getRefersToFormula());
CellReference[] crefs = aref.getAllReferencedCells();
for (int i = 0; i < crefs.length; i++) {
Sheet s = wb.getSheet(crefs[i].getSheetName());
Row r = sheet.getRow(crefs[i].getRow());
Cell c = r.getCell(crefs[i].getCol());
// extract the cell contents based on cell type etc.
}
For the sake of memory consuming, totally empty rows are not stored on the sheet. Also totally empty cells are not stored in rows of the sheet.
Sheet.getRow returns null if the row is not defined on the sheet. Also Row.getCell returns null if the cell is undefined in that row.
So we always need check:
...
Row r = sheet.getRow(crefs[i].getRow());
if (r == null) {
//row is empty
} else {
Cell c = r.getCell(crefs[i].getCol());
if (c == null) {
//cell is empty
} else {
//do something with c
}
}
...
I have a big DTO with exactly 234 fields, and I have to display values of each fields of this DTO in a column of an Excel file created with apache-poi.
This is my code :
// Blank workbook
XSSFWorkbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet("Export values");
// Get the Entity
Simfoot simEntity = simService.findById(simId).get();
Row row = sheet.createRow(0);
row.createCell(1).setCellValue("Consult our values");
// and after this I want to convert my Simfoot object to a column in the third column ( so creteCell(2) ..... ).
I want to have in my first column : nothing , in my second only the String display ( "Consult our values" ) and in my third column I need to have my 234 fields. With an field ( the value of the field ) in one cell. So, 234 rows displaying one value in the third column.
I hope that it is clear.
Thanks a lot for your help.
Using some reflection:
// Blank workbook
XSSFWorkbook workbook = new XSSFWorkbook();
final Sheet sheet = workbook.createSheet("Export values");
// Get the Entity
final Simfoot simEntity = simService.findById(simId).get();
Row row = sheet.createRow(0);
row.createCell(1).setCellValue("Consult our values");
// and after this I want to convert my Simfoot object to a column in the third column ( so creteCell(2) ..... ).
Arrays.stream(simEntity.getClass().getDeclaredMethods())
.filter(m -> m.getName().startsWith("get") && m.getParameterTypes().length == 0 && !void.class.equals(m.getReturnType()))
.forEach(m -> {
try {
Object value = m.invoke(simEntity, null);
Row r = sheet.createRow(sheet.getLastRowNum()+1);
r.createCell(2).setCellValue(value == null ? "" : value.toString());
}
catch (Exception ex) {
// Manage Exception....
}
});
I'll add a method on Simfoot to return all the values:
public List<String> getAllValues() {
return Arrays.asList(getAtt1(), getAtt2(), .. , getAtt234());
}
Then create a row per attribute, and then you can merge the rows of the first 2 columns. Example here with 6 attributes:
int n = 6; // would be 234 for you
XSSFCellStyle styleAlignTop = workbook.createCellStyle();
styleAlignTop.setVerticalAlignment(VerticalAlignment.TOP);
Row row;
for(int i=0; i<n; i++) {
row = sheet.createRow(i);
if(i==0) {
Cell cell = row.createCell(1);
cell.setCellStyle(styleAlignTop);
cell.setCellValue("Consult our values");
}
row.createCell(2).setCellValue(simEntity.getAllValues().get(i));
}
sheet.addMergedRegion(new CellRangeAddress(0, n-1, 0, 0));
sheet.addMergedRegion(new CellRangeAddress(0, n-1, 1, 1));
It shows like this:
Another way to list your attributes would be to use Reflection but I find it very clunky:
Simfoot simEntity = new Simfoot("pap", "pep", "pip", "pop", "pup", "pyp");
for(PropertyDescriptor propertyDescriptor :
Introspector.getBeanInfo(Simfoot.class).getPropertyDescriptors()) {
System.out.println(propertyDescriptor.getReadMethod().invoke(simEntity));
}
Outputs:
pap
pep
pip
pop
pup
pyp
class Simfoot
so you have to filter out getClass and any other unwanted methods and getters
I have a xlsx file which has multiple sheet i am using apache poi for writing excel, in sheet2 i have 2 columns
each column i want to populate by running a for loop , but i see that only last for loop get written previous one get blank in final written output file, i want to write both column by these for loop please help .
for(int i=0;i<fileNamesArray.length;i++)
{
XSSFRow row = worksheet1.createRow(i+1);
cell = row.createCell(0);
cell.setCellValue(fileNamesArray[i].toString());
}//this dont get written
for(int i=0;i<fileDatesArray.length;i++)
{
XSSFRow row = worksheet1.createRow(i+1);
cell = row.createCell(1);
cell.setCellValue(fileDatesArray[i].toString());
}//only this get written
this is complete code
public class DashBoard {
public void writeDashBoard() throws IOException, SQLException
{
CODToolUtil codToolUtil = new CODToolUtil();
// Read property file to initialize constants
String templateDashBoardFile = codToolUtil.getPropValues("templateDashBoardFile");
String outputDir = codToolUtil.getPropValues("outputDir");
String dirSeprator = codToolUtil.getPropValues("dirSeprator");
String fdate = CODToolUtil.getDate();
CODDAO coddao=new CODDAO();
LinkedHashSet<String> hs= new LinkedHashSet<String>();
LinkedHashSet<String> hs1= new LinkedHashSet<String>();
FileInputStream fsIP= new FileInputStream(new File(templateDashBoardFile)); //Template file
XSSFWorkbook wb = new XSSFWorkbook(fsIP);
XSSFSheet worksheet = wb.getSheetAt(0);
Cell cell = null;
cell = worksheet.getRow(1).getCell(0);
cell.setCellValue(CODToolUtil.getDate());//Date
cell = worksheet.getRow(1).getCell(1);
int allfiles=coddao.getAllfiles();
cell.setCellValue(allfiles);//All Files
cell = worksheet.getRow(1).getCell(2);
int callfilesY=coddao.getAllProcessedfilesCallY();
cell.setCellValue(callfilesY);//All Y Files
cell = worksheet.getRow(1).getCell(3);
int callfilesN=coddao.getAllProcessedfilesCallN();
cell.setCellValue(callfilesN);//All N Files
cell = worksheet.getRow(1).getCell(4);
int allLTE=coddao.getAllProcessedfilesLTE();
cell.setCellValue(allLTE);//All LTE Files
cell = worksheet.getRow(1).getCell(5);
int allWCDMA=coddao.getAllProcessedfilesWCDMA();
cell.setCellValue(allWCDMA);//All WCDMA Files
//Sheet 0 OverView Complete
//Sheet 1 Successfull CT
XSSFSheet worksheet1 = wb.getSheetAt(1);
hs=coddao.getAllProcessedfilesNameY();
hs1=coddao.getAllProcessedfilesDateY();
Object[] fileNamesArray = hs.toArray();
Object[] fileDatesArray = hs1.toArray();
for(int i=0;i<fileNamesArray.length;i++)
{
XSSFRow row = worksheet1.createRow(i+1);
cell = row.createCell(0);
cell.setCellValue(fileNamesArray[i].toString());
}//this dont get written
for(int i=0;i<fileDatesArray.length;i++)
{
XSSFRow row = worksheet1.createRow(i+1);
cell = row.createCell(1);
cell.setCellValue(fileDatesArray[i].toString());
}//only this get written
fsIP.close();
File saveDirectory = new File(outputDir);// Create OutPutDirectory
saveDirectory.mkdir();
String savefilePath = saveDirectory.getAbsolutePath();
FileOutputStream output_file = newFileOutputStream(newFile(savefilePath+dirSeprator+fdate+"-"+templateDashBoardFile)); // save in output
wb.write(output_file); // write changes save it.
output_file.close(); // close the stream
}
public static void main(String[] args) throws IOException, SQLException {
new DashBoard().writeDashBoard();
}
}
You are creating the same row twice - probably overriding the "first" row created in the first loop, with the "second" row created in the second loop.
If fileNamesArray and fileDatesArray are the same size, you can combine the loops as:
for(int i=0;i<fileNamesArray.length;i++)
{
XSSFRow row = worksheet1.createRow(i+1);
cell1 = row.createCell(0);
cell1.setCellValue(fileNamesArray[i].toString());
cell2 = row.createCell(1);
cell2.setCellValue(fileDatesArray[i].toString());
}
check which array is bigger and loop through it first, then loop through the second array, but instead of using worksheet1.createRow(i+1) - use worksheet1.getRow(i+1), reusing the row element you created in the first loop.
Note: in theory, even if the arrays are of different sizes you can still use one loop, just make sure you apply relevant checks to avoid ArrayIndexOutOfBoundsException.
Try
for(int i=0;i<fileNamesArray.length;i++)
{
XSSFRow row = worksheet1.createRow(i+1);
cell = row.createCell(0);
cell.setCellValue(fileNamesArray[i].toString());
cell = row.createCell(1);
cell.setCellValue(fileDatesArray[i].toString());
}
Instead of of using those 2 loops. I would imagine you are overwriting the your row when you call worksheet1.createRow in the second loop.
gradeList is an ArrayList of strings with the value "80", "81" ... "85"
for(int y = 0; y < gradeList.size(); y++){
HSSFRow row1 = worksheet.createRow((short) 1);//1
HSSFCell cell1 =row1.createCell((short) y+1);//2
cell1.setCellValue("" + gradeList.get(y));//3
HSSFCellStyle cellStylei = workbook.createCellStyle();//4
cellStylei.setFillForegroundColor(HSSFColor.GREEN.index);
cell1.setCellStyle(cellStylei);//6
}
Output of Code: _, _, _, _, _, 85.
intended Output: 80, 81, 82, 83, 84, 85.
After changing the code to
HSSFRow row1 = worksheet.createRow((short) 1);//1
HSSFCell cell1;
for(int y = 0; y < gradeList.size(); y++){
cell1 = row1.createCell((short) y+1);//2
cell1.setCellValue("" + gradeList.get(y));//3
}
HSSFCellStyle cellStylei = workbook.createCellStyle();//4
cellStylei.setFillForegroundColor(HSSFColor.GREEN.index);//5
the code prints 80, 81, 82, 83, 84, and 85 as intended but using the previous six line code it only prints 85. Can someone please explain to me why is first one wrong or not working, and if possible also can you please also explain what lines 4,5, and 6 do.
I am trying to get the column values for a specific row in a excel using poi methods.
I am able to get the values but the problem is I want the values only from second column.
public static ArrayList<String> GetBusinessComponentList() throws IOException{
String Tcname = "TC02_AggregateAutoByPassRO_CT";
ArrayList<String> arrayListBusinessFlow ;
arrayListBusinessFlow = new ArrayList<String>();
FileInputStream fileInput = new FileInputStream(oFile);
wb = new HSSFWorkbook(fileInput);
sheet = wb.getSheet("Business Flow");
int rownr = findRow(sheet, Tcname);
row = sheet.getRow(rownr);
for (Cell cell : row) {
String arr = cell.getStringCellValue();
arrayListBusinessFlow.add(arr);
}
return arrayListBusinessFlow;
}
private static int findRow(HSSFSheet sheet, String cellContent){
for (Row row : sheet) {
for (Cell cell : row) {
if (cell.getCellType() == Cell.CELL_TYPE_STRING) {
if (cell.getRichStringCellValue().getString().trim().equals(cellContent)) {
return row.getRowNum();
}
}
}
}
return 0;
}
}
OUTPUT:
[TC02_AggregateAutoByPassRO_CT,
StrategicUINewBusiness.Login,
StrategicUINewBusiness.CustomerSearch,
StrategicUINewBusiness.NamedInsured,
StrategicUINewBusiness.InsuranceScoreByPass,
StrategicUINewBusiness.VehiclePage,
StrategicUINewBusiness.DriverPage,
StrategicUINewBusiness.ViolationPage,
StrategicUINewBusiness.UnderwritingPage,
StrategicUINewBusiness.CoveragePage,
StrategicUINewBusiness.Portfolio,
StrategicUINewBusiness.BillingPage,
StrategicUINewBusiness.FinalSalePage,
StrategicUINewBusiness.PolicyConfirmation, , , ]
But I do not want my test case name when I am getting.
Please help me what changes i needed to do. thanks!
Currently, the code you're using to iterate over cells only returns cells with content or styling, and skips totally empty ones. You need to change to one of the other ways of iterating over cells, so you can control it to read from the second column onwards.
If you look at the Apache POI Documentation on iterating over rows and cells, you'll see a lot more details on the two main ways to iterate.
For your case, you'll want something like:
// We want to read from the 2nd column onwards, zero based
int firstColumn = 1;
// Always fetch at least 4 columns
int MY_MINIMUM_COLUMN_COUNT = 5;
// Work out the last column to go to
int lastColumn = Math.max(r.getLastCellNum(), MY_MINIMUM_COLUMN_COUNT);
// To format cells into strings
DataFormatter df = new DataFormatter();
// Iterate over the cells
for (int cn = firstColumn; cn < lastColumn; cn++) {
Cell c = r.getCell(cn, Row.RETURN_BLANK_AS_NULL);
if (c == null) {
// The spreadsheet is empty in this cell
} else {
// Do something useful with the cell's contents
// eg get the cells value as a string
String cellAsString = df.formatCellValue(c);
}
}
Use Cell cell=row.getCell(1); and also you can use sheet.getLastRowNum() to get the number last row on the sheet.
for (int i=0;i<=row.getLastCellNum();i++) {
if (i!=1){
//your stuff
}
}