How to replace .docx table values using Apache POI

Here is an example on using Apache POI to read data from tables inside a .docx file and then write some data in it, the source also deletes the original file and replaces it for the corrected one. important thing about this code is that once you have downloaded the POI, the libs that you will get will not be enough to make it work!

You will need to download the ooxml-schemas-1.1.jar file and add it to your classpath, you can find it right here.

package br.com.vrsbrazil;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

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 ReadWriteFile {

 /**
  * @param args
  * @throws IOException
  */

 public static void main(String[] args) throws IOException {

  final String[] folders = new String[]{"/SourceFolder"};
  final String newValue = "Test value\nFor new cell";

  for(String pastaPath:folders){

   final File folder = new File(pastaPath);

   for(File file:folder.listFiles()){

    if(file.getName().endsWith(".docx")) replaceTableValues(file,newValue);

   }

  }

 }

 public static void replaceTableValues(final File source,final String newValue) throws IOException {

  final File target = new File(source.getAbsolutePath()+".tmp");

  final XWPFDocument document = new XWPFDocument(new FileInputStream(source));

  for(int index=0;index<document.getTables().size();index++){

   final XWPFTable table = document.getTables().get(index);

   final XWPFTableCell cell = table.getRow(0).getCell(0);

   System.out.println("Current cell value: " + cell.getText());

   table.removeRow(0);

   final XWPFTableRow row = table.createRow();

   final XWPFTableCell novaCelula = row.createCell();

   final String[] linhas = newValue.split("\n");

   for(String s:linhas){

    final XWPFParagraph paragrafo =  novaCelula.addParagraph();

    paragrafo.createRun().setText(s);

   }

  }

  document.write(new FileOutputStream(target));

  final String newtargetPath = source.getAbsolutePath();

  source.delete();

  target.renameTo(new File(newtargetPath));

 }

}

Thank you for reading!

Advertisements

How to change your SOA Suite project name

This is a simple task that can be rather annoying you do not make it right.
All you have to do is:

  1. On Jdev select your project then click on the upper menu File -> Rename…;
  2. On the Rename screen change the .jpr name on the File name field then OK;
  3. Now that only changes the name of the project, but now you have the change the name of your composite, to do that open your composite.xml file;
  4. Click on the Source tab;
  5. On the <composite/> tag change the name attribute to your new project name and save it;
  6. You will have to create a new deployment profile too, to do that right click on your project then select Project Properties…;
  7. Select Deployment;
  8. Delete the deployment profile that has your old project name;
  9. Click on the New button;
  10. On the Archive type select the SOA-SAR File;
  11. Type a name for your new profile an click OK.
That´s it. Now the project folder still has the old name, you will have to change it manually and then reopen the project on your application, make sure you close your application before you do that.
Thank you for reading!