Page 1 of 1

xlsx4j - variableReplace() do nothing !

PostPosted: Fri Jun 16, 2017 12:07 am
by schlebe
Hello everybody,

it is the first time that I use doc4j to generate an Excel file from another Excel file (pseudo template file) in changing some values in some fields.

I have found an example in and I have made some minor changes.

The program run correctly without crashing but the generated Excel file doesn't contains new values.

In the input template Excel file, some cells contains the following strings


The goal of this little test is to replace the 2 previous pseudo variables by 2 specific values.

You can find the program below.

When I debug the program on Netbeans 8.1, I can see that oSharingString variable contains the 2 pseudo variable and I can also see that after calling variableReplace() function nothing have been changed !

What is also very strange is that if I don't call oSharedString.getXML(); the oSharedString is not populated !!!

What do I do wrong ?

For your information, I have tested with doc4j version 3.3.3 and 3.3.2 and 3.2.2 that I have found on Maven repository.

Thanks for any help

Code: Select all
* GenerateAuditReportForAuthority()


import java.util.HashMap;

import org.docx4j.openpackaging.packages.SpreadsheetMLPackage;

import java.nio.file.Paths;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

public class GenerateAuditReportForAuthority
    public static void main(String[] args) throws Exception
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd.HHmmss");
        String sDateTime = dateFormat.format(new Date());
        String sCurrentDir = Paths.get(".").toAbsolutePath().normalize().toString();
        String sTestDir = sCurrentDir + "/test";
        String sInputTemplate = sTestDir + "/AuditReport.TEMPLATE.xlsm";
        String sOutputFile = sTestDir + "/AuditReport." + sDateTime + ".xlsm";
        SpreadsheetMLPackage oExcel;
        try {
            oExcel = SpreadsheetMLPackage.load(new File(sInputTemplate));
            JaxbSmlPart oSharedString = (JaxbSmlPart)oExcel.getParts().get(new PartName("/xl/sharedStrings.xml"));

            HashMap<String, String> mappings = new HashMap<String, String>();

            mappings.put("#{ctc.BRAND-NAME}", "DUNLOP");
            mappings.put("#{ctc.TIRE-SIZE}", "125/45R18");


        catch(Exception ex)
            int iTrace = 0;
    } // END-OF-CLASS

Re: xlsx4j - variableReplace() do nothing !

PostPosted: Fri Jun 16, 2017 12:17 am
by schlebe
I use the following POM file.

Code: Select all
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="" xmlns:xsi="" xsi:schemaLocation="">

        <!-- -->


Re: xlsx4j - variableReplace() do nothing !

PostPosted: Fri Jun 16, 2017 10:13 am
by jason
Code: Select all
        mappings.put("ctc.BRAND-NAME", "DUNLOP");
        mappings.put("ctc.TIRE-SIZE", "125/45R18");

with the attached sample input xlsx.

When I added entries to the sheet, I used for example:

Code: Select all

(note the preceding single quote)

Re: xlsx4j - variableReplace() do nothing !

PostPosted: Fri Jun 16, 2017 4:55 pm
by schlebe
@Jason: thanks for your help.

I now use ${variable-name} instead #{variable-name} and I indicate only variable-name in mapping and this work fine.


Question: the ${variable-name} notation is an Excel standard OR a xlsx4j notation ?

Re: xlsx4j - variableReplace() do nothing !

PostPosted: Fri Jun 16, 2017 9:21 pm
by jason
an xlsx4j notation