Hello Everyone,

I am using apache POI 3.7. I am trying to replace the value of a table column in a word document (docx). However, what I have done is it keeps appending the value of the current value in the document. But if a table column value is null, it places the value. Can you give me some thoughts how to resolve this. Below is the code I have done so far.

Thanks in advance.

package test.doc;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;

import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.apache.poi.xwpf.usermodel.XWPFTableCell;
import org.apache.poi.xwpf.usermodel.XWPFTableRow;

public class POIDocXTableTest {
	
	public static void main(String[] args)throws IOException {
		String fileName = "C:\\Test.docx";
		InputStream fis = new FileInputStream(fileName);
		XWPFDocument document = new XWPFDocument(fis);
		List<XWPFParagraph> paragraphs = document.getParagraphs();
		
		for (int x=0; x<paragraphs.size();x++)
		{
			XWPFParagraph paragraph = paragraphs.get(x);
			System.out.println(paragraph.getParagraphText());
		}
		List<XWPFTable> tables = document.getTables();
		for (int x=0; x<tables.size();x++)
		{
			XWPFTable table = tables.get(x);
			List<XWPFTableRow> tableRows = table.getRows();
			tableRows.remove(x);
			for (int r=0; r<tableRows.size();r++)
			{
				System.out.println("Row "+ (r+1)+ ":");
				XWPFTableRow tableRow = tableRows.get(r);
				List<XWPFTableCell> tableCells = tableRow.getTableCells();
				for (int c=0; c<tableCells.size();c++)
				{
					System.out.print("Column "+ (c+1)+ ": ");
					XWPFTableCell tableCell = tableCells.get(c);
					//tableCell.setText("TAE");
					String tableCellVal = tableCell.getText();
					if ((c+1)==2){
						
						if (tableCellVal!=null){
							if (tableCellVal.length()>0){
								 char c1 = tableCellVal.charAt(0);
								 String s2 = "-TEST";
								 char c2 = s2.charAt(0);
								 String test = tableCell.getText().replace(tableCellVal,s2);
								 tableCell.setText(test);
							}else{
								//tableCell.setText("NULL");
							}
						}
					}
					System.out.println("tableCell.getText(" + (c) + "):" + tableCellVal);
				}
			}
			System.out.println("\n");
		}
		OutputStream out = new FileOutputStream(fileName);
		document.write(out);
		out.close();
	}
}

Recommended Answers

OK, looking at POI code XWPFTableCell consists of number of XWPFParagraphs of which you need to take care. So you need to remove all paragraphs in order to have text replace. You can do something like this

if ((cellNum + 1) == 2) {

      if (tableCellVal != ā€¦
Jump to Post

All 4 Replies

Can you attach sample word document you trying to work on?

Can you attach sample word document you trying to work on?

Thanks for the reply. I attached the document sample.

OK, looking at POI code XWPFTableCell consists of number of XWPFParagraphs of which you need to take care. So you need to remove all paragraphs in order to have text replace. You can do something like this

if ((cellNum + 1) == 2) {

      if (tableCellVal != null) {
        if (tableCellVal.length() > 0) {
          removeParagraphs(tableCell);
          tableCell.setText("CHANGE");
        } else {
          //tableCell.setText("NULL");
        }
      }
    }

where removeParagraphs can look like this

private static void removeParagraphs(XWPFTableCell tableCell) {
    int count = tableCell.getParagraphs().size();
    for(int i = 0; i < count; i++){
      tableCell.removeParagraph(i);
    }
  }

The removing of paragraph worked. Thanks very much

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.