这里是示例代码中,“\ n”的不是这个方法的setText工作?XWPFRun.setText()似乎不遵守换行符或制表符?http://cn.voidcc.com/question/p-gcptddrd-zg.html
XWPFDocument document = new XWPFDocument();
XWPFParagraph tp = document.createParagraph();
XWPFRun tRun = tp.createRun();
tRun.setText("a");
tRun.setText("\n"); // not work
tRun.setText("b");
FileOutputStream outStream = null;
try {
outStream = new FileOutputStream("testTabWithPOI.doc");
document.write(outStream);
outStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
这不是你如何添加选项卡或换行到一个运行。 Microsoft Words生成文件的方式是添加特殊的中断样式元素,因此这也是您需要在Apache POI中执行的操作,因为这是格式的工作方式。testAddTabsAndLineBreaks() of TestXWPFRunhttp://svn.apache.org/repos/asf/poi/trunk/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFRun.java您可以在中看到添加标签的示例。你的代码需要:
XWPFRun tRun = tp.createRun();
tRun.setText("a");
tRun.addTab();
tRun.setText("b");
例子:如下图生成的word文档的格式需要实现换行的效果。
//判断文本是否需要进行替换
if (checkText(text)) {
for (Map.Entry<String, Object> entry : textMap.entrySet()) {
//匹配模板与替换值 格式${key}
String key = "${" + entry.getKey() + "}";
// String key =entry.getKey() ;
Object value = entry.getValue();
if (text.contains(key)) {
if (value instanceof String) { //文字替换
if (value != null && value.toString().contains("\n")) {
String[] lines = value.toString().split("\n");
if (lines.length > 0) {
// set first line into XWPFRun
run.setText(lines[0], 0);
for (int i = 1; i < lines.length; i++) {
// add break and insert new text
run.addBreak();
run.setText(lines[i]);
}
}
} else {
text = text.replace(key, (String) value);
}
} else if (value instanceof Map) { //图片替换
text = text.replace(key, "");
Map picMap = (Map) value;
int width = Integer.parseInt(picMap.get("width").toString());
int height = Integer.parseInt(picMap.get("height").toString());
int picType = getPictureType(picMap.get("type").toString());
FileInputStream fis = (FileInputStream) picMap.get("content");
try {
paragraph.setAlignment(ParagraphAlignment.CENTER);//样式居中
String blipId = document.addPictureData(fis, picType);
int id = document.getNextPicNameNumber(picType);
XWPFUtils.createPicture(id, blipId, width, height, run);
} catch (Exception e) {
e.printStackTrace();
}
} else if (value instanceof List) {
text = text.replace(key, "");
List<List<Object>> tableData = (List<List<Object>>) value;
XmlCursor cursor = paragraph.getCTP().newCursor();
XWPFTable tableOne = document.insertNewTbl(cursor);// ---这个是关键
//表格生成 6行5列.
int rows = tableData.size();
int cols = tableData.get(0).size();
XWPFHelperTable xwpfHelperTable = new XWPFHelperTable();
xwpfHelperTable.setTableWidthAndHAlign(tableOne, "9000", STJc.CENTER);
// xwpfHelperTable.setTableHeight(tableOne, 560, STVerticalJc.CENTER);
//表格创建
XWPFTableRow tableRowTitle = tableOne.getRow(0);
for (int i = 0; i < tableData.get(0).size(); i++) {
XWPFParagraph cellParagraph;
if (i == 0) {
// tableRowTitle.getCell(0).setText(tableData.get(0).get(i).toString());
cellParagraph = tableRowTitle.getCell(0).addParagraph();
} else {
// tableRowTitle.addNewTableCell().setText(tableData.get(0).get(i).toString());
cellParagraph = tableRowTitle.addNewTableCell().addParagraph();
}
XWPFRun cellParagraphRun = cellParagraph.createRun();
cellParagraph.setAlignment(ParagraphAlignment.CENTER);//样式居中
cellParagraphRun.setFontSize(15); //字体大小
cellParagraphRun.setFontFamily("仿宋");//设置字体
cellParagraphRun.setText(changeText(tableData.get(0).get(i)));
}
for (int i = 1; i < rows; i++) {
XWPFTableRow createRow = tableOne.createRow();
for (int j = 0; j < cols; j++) {
// createRow.getCell(j).setText(changeText(tableData.get(i).get(j)));
XWPFParagraph cellParagraph = createRow.getCell(j).addParagraph();
XWPFRun cellParagraphRun = cellParagraph.createRun();
cellParagraph.setAlignment(ParagraphAlignment.CENTER);//样式居中
cellParagraphRun.setFontSize(15); //字体大小
cellParagraphRun.setFontFamily("仿宋");//设置字体
cellParagraphRun.setText(changeText(tableData.get(i).get(j)));
}
}
}
}
}
//替换模板原来位置
run.setText(text, 0);
}
增加了截图代码后,生成的模板word文档出现了换行效果。