Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6586587
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T16:48:36+00:00 2026-05-25T16:48:36+00:00

I have a Microsoft Word 2007/xml .docx file that I am trying to edit

  • 0

I have a Microsoft Word 2007/xml .docx file that I am trying to edit using Apache POI 3.8beta4. The document contains, among other things, a table that contains cells that hold place holders in the form ${place.holder} that I need to replace. What I’ve got so far is;

    InputStream resourceAsStream =  getClass().getResourceAsStream("/path/to/templates/rma.docx");       
    try {
        XWPFDocument xwpfdoc = new XWPFDocument(resourceAsStream); 

        FileOutputStream fos = new FileOutputStream(new File("C:\\temp\\newTemplate.docx"));

        for (XWPFTable table : xwpfdoc.getTables()) {

             for (XWPFTableRow row : table.getRows()) {

                 for (XWPFTableCell cell : row.getTableCells()) {

                     String data = cell.getText();

                     if (data.contains("${rma.number}")) {
                         cell.setText("08739");
                     }

                     if (data.contains("${customer.name}")) {
                         cell.setText("Roger Swann");
                     }
                 }
             }
        }
        xwpfdoc.write(fos);
        fos.flush();
        fos.close();

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    } 

The problem is that cell.setText(“replacement text”} is appending to the string data already present instead of replacing it, so what I end up with in the finished document is the string “{place.holder}replacement text”.

How do I replace the text rather than append to it?

Regards

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T16:48:37+00:00Added an answer on May 25, 2026 at 4:48 pm

    The quick fix is to get the underlying text run of the cell, and change that. It’s a bit fiddly, but it can be done. You’d likely want to call cell.getBodyElements() and iterate through them to find the thing with your text in. Then, change the text on that, rather than trying to change the cell directly

    The longer term one is to open a new bug in the POI bugzilla, and upload a failing unit test. This should probably include your file, and show the “replacement” of the text, followed by saving, reloading, and reading. The issue can then later be fixed

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been using Apache POI to manipulate Microsoft Word .docx files — ie
I have been given two different Microsoft Word document that my virus scanner has
I have quite big document in html format that generated from Microsoft Word. It
I have automated word document creation from templates using the Microsoft Word 12.0 Object
I have created a document template file (.dotx) in Word 2007 and in this
I'm working on creating a macro in Microsoft Word (2007) for a document that
Hello I have the Addin for Word 2007 that is creted using VSTO. I
I have seen in a few cases that converting a file from Microsoft Word
I am trying to put the Microsoft Word document in emacs using org-mode. I
I have a requirement to implement a toolbar similar to microsoft word 2007 using

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.