I still don't get it... Maybe I just don't really understand the problem. I try to explain it one more
I created a template with few placeholders in MS Word and try to replace them with some strings. The string are actually a result of SQL queries. Some of them ale very long and include line breaks:
-1
-2
-3
I already tried to replace "\n" with theoretical XML syntax for a line breaks but it doesn't works.
Here ist the code:
- Code: Select all
public static void main(String args[]) {
final String XPATH_TO_SELECT_TEXT_NODES = "//w:t";
SQLResultForStaff SQL = new SQLResultForStaff();
try {
map.put("projectName", "test");
map.put("headerName", "Test \n Test ");
map.put("proposedPosition", "Test");
map.put("nameOfStaff", SQL.SQLRes.get(1));
map.put("strProfession", SQL.SQLRes.get(2));
map.put("dayOfBirth", SQL.SQLRes.get(3));
map.put("withFirmYear", SQL.SQLRes.get(4));
map.put("nationality", SQL.SQLRes.get(5));
map.put("memberInProsSoc", SQL.SQLRes.get(6));
map.put("keyQualifications", Temp.tues(Temp.test));
// C:\\test\\template1.docx is the template file
WordprocessingMLPackage template = WordprocessingMLPackage
.load(new File("C:\\CV\\templateDOCX.docx"));
List texts = template.getMainDocumentPart().getJAXBNodesViaXPath(
XPATH_TO_SELECT_TEXT_NODES, true);
for (Object obj : texts) {
Text text = (Text) ((JAXBElement) obj).getValue();
String textValue = text.getValue();
for (Object key : map.keySet()) {
textValue = textValue.replaceAll("" + key + "",
(String) map.get(key));
}
text.setValue(textValue);
}
template.save(new File("C:\\CV\\" + SQL.SQLRes.get(1) + "_CV.docx"));
writeToXML();
SQL.SQLRes.clear();
System.out.println("Saved!");
} catch (Exception e) {
e.printStackTrace();
System.out.println("Errors: " + e.getCause());
}
}
And the code for replacing "\n"
- Code: Select all
public class Temp {
public static String test;
// private static String[] split;
private static String temp = "";
public static void main(String[] args) {
SQLResultForStaff SQL = new SQLResultForStaff();
test = SQL.SQLRes.get(7);
tues(test);
// Splitted(test);
}
public static String tues(String test) {
SQLResultForStaff SQL = new SQLResultForStaff();
test = SQL.SQLRes.get(7);
test = test
.replaceAll(
"\n",
"</w:t></w:r></w:p><w:p><w:pPr><w:pStyle w:val=\"BodyText\"/><w:keepNext/><w:keepLines/><w:spacing w:before=\"0\" w:after=\"0\"/></w:pPr>");
System.out.println(test);
return "<w:r><w:t>" + test;
}
}
How should it looks like:
- Waste water treatment plants
- Flue gas cleaning facilities
- Emission monitoring facilities
- Fuel oil treatment
- Sampling systems for water/steam cycle
How does it looks like now:
- Waste water treatment plants - Flue gas cleaning facilities - Emission monitoring facilities - Fuel oil treatment - Sampling systems for water/steam cycle