How to add text to a .docx file using Apache POI in Java

Viewed 71

I am new to Java and want to add data to an already existing .docx file and so far i've been unsucessful. I've been looking for an answer to this for 3 days now and i decided to come to stackoverflow and ask how i could add text to an existing file using apache poi.

My program is supposed to generate a bunch of random data and store it in a file. Before Apache, i was using .write() to log my data and it was working well but the file didnt have formatting which was fine in the moment but now i need formatting to be able to get a good look at the thousands of data entries.

Here is the code i was using to log the data before:

FileWriter fwriteFinal = new FileWriter("obf_reg.docx", true);
fwriteFinal.write(lbrk);
fwriteFinal.write("\n");
fwriteFinal.write("Your Engine is called: " + engine_Name);
fwriteFinal.write("\n");
fwriteFinal.write("Your Engine Cycle is: " + nuclear_Cycle_Chosen + " " + engine_Cycle_Chosen);
fwriteFinal.write("\n");
fwriteFinal.write("Your Propellant is: " + propellant_List_Chosen);
fwriteFinal.write("\n");
fwriteFinal.write("Your Reactor's Fission Fuel is: " + reactor_Fuel_Chosen);
fwriteFinal.write("\n");
fwriteFinal.write("Your engine's nozzle type should be : " + nozzle_Type_List_Chosen);
fwriteFinal.write("\n");
fwriteFinal.write("Tank repressurisation would be done " + tank_Repressurisation_Chosen);
fwriteFinal.write("\n");
fwriteFinal.close();

and this is what im trying to use:

XWPFDocument docx = new XWPFDocument();
XWPFParagraph par = docx.createParagraph();
XWPFRun run = par.createRun();
run.setText(lbrk + "\n" + "Your Engine is called: " + engine_Name + "\n" +
            "\n" + "Your Engine Cycle is: " + nuclear_Cycle_Chosen + " " + engine_Cycle_Chosen +
            "\n" + "Your Propellant is: " + propellant_List_Chosen +
            "\n" + "Your Reactor's Fission Fuel is: " + reactor_Fuel_Chosen +
            "\n" + "Your engine's nozzle type should be : " + nozzle_Type_List_Chosen +            
            "\n" + "Tank repressurisation would be done " + tank_Repressurisation_Chosen);
FileOutputStream out = new FileOutputStream("C:\\Users\\sfsga\\OneDrive\\Desktop\\obf_reg.docx");
docx.write(out);
out.close();

its prolly a dumb mistake but im still gonna ask.

1 Answers

The string literal "\n" does not have the usual special meaning in Microsoft Word. There are special settings for line breaks in Word.

You could use XWPFRun.addBreak to add a line break into one text run. To do so each "\n" in your code would must be replaced by run.addBreak().

Something like:

...
  run.setText(lbrk);
  run.addBreak();
  run.setText("Your Engine is called: " + engine_Name);
  run.addBreak();
  run.setText("Your Engine Cycle is: " + nuclear_Cycle_Chosen + " " + engine_Cycle_Chosen);
  run.addBreak();
  run.setText("Your Propellant is: " + propellant_List_Chosen);
  run.addBreak();
  run.setText("Your Reactor's Fission Fuel is: " + reactor_Fuel_Chosen);
  run.addBreak();
  run.setText("Your engine's nozzle type should be : " + nozzle_Type_List_Chosen);
  run.addBreak();
  run.setText("Tank repressurisation would be done " + tank_Repressurisation_Chosen);
...

But more Word-lke is to use an additional paragraph for each time you would press Enter in Word's GUI. Word also adds a new paragraph when the user press Enter in Word's GUI.

Using different single XWPFRun per text run is also better for formatting later. All needed different text formatting needs their own XWPFRun.

If the need is to append data into the same file each time the code runs, then we need check whether the file already exists and, if so, create the XWPFDocument from that file. To do so constructor public XWPFDocument(java.io.InputStream is) can be used.

That would look like follows in code:

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

import org.apache.poi.xwpf.usermodel.*;

public class CreateWord {

 public static void main(String[] args) throws Exception {
     
  String lbrk = "Sample lbrk";
  String engine_Name = "Sample engine_Name";
  String nuclear_Cycle_Chosen = "Sample nuclear_Cycle_Chosen";
  String engine_Cycle_Chosen = "Sample engine_Cycle_Chosen";
  String propellant_List_Chosen = "Sample propellant_List_Chosen";
  String reactor_Fuel_Chosen = "Sample reactor_Fuel_Chosen";
  String nozzle_Type_List_Chosen = "Sample nozzle_Type_List_Chosen";
  String tank_Repressurisation_Chosen = "Sample tank_Repressurisation_Chosen";
  
  File file = new File("./obf_reg.docx");
  XWPFDocument docx = new XWPFDocument();
  if (file.exists()) {
   docx = new  XWPFDocument(new FileInputStream(file));
  } 

  XWPFParagraph par = docx.createParagraph();
  XWPFRun run = par.createRun();

/*
  run.setText(lbrk);
  run.addBreak();
  run.setText("Your Engine is called: " + engine_Name);
  run.addBreak();
  run.setText("Your Engine Cycle is: " + nuclear_Cycle_Chosen + " " + engine_Cycle_Chosen);
  run.addBreak();
  run.setText("Your Propellant is: " + propellant_List_Chosen);
  run.addBreak();
  run.setText("Your Reactor's Fission Fuel is: " + reactor_Fuel_Chosen);
  run.addBreak();
  run.setText("Your engine's nozzle type should be : " + nozzle_Type_List_Chosen);
  run.addBreak();
  run.setText("Tank repressurisation would be done " + tank_Repressurisation_Chosen);

  par = docx.createParagraph();
*/

  par = docx.createParagraph(); run = par.createRun();
  run.setText(lbrk);
  par = docx.createParagraph(); run = par.createRun();
  run.setText("Your Engine is called: " + engine_Name);
  par = docx.createParagraph(); run = par.createRun();
  run.setText("Your Engine Cycle is: " + nuclear_Cycle_Chosen + " " + engine_Cycle_Chosen);
  par = docx.createParagraph(); run = par.createRun();
  run.setText("Your Propellant is: " + propellant_List_Chosen);
  par = docx.createParagraph(); run = par.createRun();
  run.setText("Your Reactor's Fission Fuel is: " + reactor_Fuel_Chosen);
  par = docx.createParagraph(); run = par.createRun();
  run.setText("Your engine's nozzle type should be : " + nozzle_Type_List_Chosen);
  par = docx.createParagraph(); run = par.createRun();
  run.setText("Tank repressurisation would be done " + tank_Repressurisation_Chosen);

  FileOutputStream out = new FileOutputStream(file);
  docx.write(out);
  out.close();
  docx.close();

 }
}
Related