Boa tarde a todos,
Estou com uma dúvida e desde ontem não encontro uma solução. Talvez alguém possa me ajudar.
Seguinte: Eu preciso carregar um arquivo docx template e substituir alguns valores nele. Em seguida devo criar um novo docx com os dados modificados.
Vocês devem conhecer o POI que realiza esta tarefa. Bom neste caso eu estou usando outra API, a DOCX4J.
Eu já consegui afzer com que ele fizesse o mapeamento e criasse outro arquivo em outro diretório. Tranquilo. O problema é que quando existe um expression(como ${place_holder}) ele não reconhece. Reparem que no código eu usei \ antes de cada caractere especial. Quando faço isso com um caracter apenas ele reconhece. Mas quando faço com dois, como está no código, ele não reconhece. Alguém poderia me dar uma direção?
Muito Obrigado
[code]package com.gui.read;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import javax.xml.bind.JAXBElement;
import org.docx4j.openpackaging.exceptions.Docx4JException;
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
import org.docx4j.wml.Text;
public class ReadModify {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args)throws Docx4JException, IOException {
/**
* Open word document c:\Sample.docx, replace a piece of text and save
* the result to c:\Sample-OUT.docx.
*
* The text #POLNO# will be replaced with TI28000001-00.
*
* @param args
*/
// Text nodes begin with w:t in the word document
final String XPATH_TO_SELECT_TEXT_NODES = "//w:t";
try {
// Open the input file
WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(new java.io.File("c:\\input\\template.docx"));
// Build a list of "text" elements
List texts = wordMLPackage.getMainDocumentPart().getJAXBNodesViaXPath(XPATH_TO_SELECT_TEXT_NODES, true);
// Loop through all "text" elements
for (Object obj : texts) {
Text text = (Text) ((JAXBElement<?>) obj).getValue();
// Get the text value
String textValueBefore = text.getValue();
// Perform the replacement
String antiga ="\\$\\{";
String nova = "seq";
String textValueAfter = textValueBefore.replaceAll(antiga, nova);
// Show the element before and after the replacement
System.out.println("textValueBefore = " + textValueBefore);
System.out.println("textValueAfter = " + textValueAfter);
// Update the text element now that we have performed the replacement
text.setValue(textValueAfter);
System.out.println("$[?_________________");
}
wordMLPackage.save(new java.io.File("C:\\output\\templateFinal.docx"));
} catch (Docx4JException e) {
} catch (Exception e) {
}
}
}
[/code]