• POI XWPFDocument 实现word中内容换行


     这里是示例代码中,“\ n”的不是这个方法的setText工作?XWPFRun.setText()似乎不遵守换行符或制表符?http://cn.voidcc.com/question/p-gcptddrd-zg.html

    1. XWPFDocument document = new XWPFDocument();
    2. XWPFParagraph tp = document.createParagraph();
    3. XWPFRun tRun = tp.createRun();
    4. tRun.setText("a");
    5. tRun.setText("\n"); // not work
    6. tRun.setText("b");
    7. FileOutputStream outStream = null;
    8. try {
    9. outStream = new FileOutputStream("testTabWithPOI.doc");
    10. document.write(outStream);
    11. outStream.close();
    12. } catch (FileNotFoundException e) {
    13. e.printStackTrace();
    14. } catch (IOException e) {
    15. e.printStackTrace();
    16. }

    这不是你如何添加选项卡或换行到一个运行。 Microsoft Words生成文件的方式是添加特殊的中断样式元素,因此这也是您需要在Apache POI中执行的操作,因为这是格式的工作方式。testAddTabsAndLineBreaks() of TestXWPFRunhttp://svn.apache.org/repos/asf/poi/trunk/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFRun.java您可以在中看到添加标签的示例。你的代码需要:

    1. XWPFRun tRun = tp.createRun();
    2. tRun.setText("a");
    3. tRun.addTab();
    4. tRun.setText("b");

    例子:如下图生成的word文档的格式需要实现换行的效果。

    1. //判断文本是否需要进行替换
    2. if (checkText(text)) {
    3. for (Map.Entry entry : textMap.entrySet()) {
    4. //匹配模板与替换值 格式${key}
    5. String key = "${" + entry.getKey() + "}";
    6. // String key =entry.getKey() ;
    7. Object value = entry.getValue();
    8. if (text.contains(key)) {
    9. if (value instanceof String) { //文字替换
    10. if (value != null && value.toString().contains("\n")) {
    11. String[] lines = value.toString().split("\n");
    12. if (lines.length > 0) {
    13. // set first line into XWPFRun
    14. run.setText(lines[0], 0);
    15. for (int i = 1; i < lines.length; i++) {
    16. // add break and insert new text
    17. run.addBreak();
    18. run.setText(lines[i]);
    19. }
    20. }
    21. } else {
    22. text = text.replace(key, (String) value);
    23. }
    24. } else if (value instanceof Map) { //图片替换
    25. text = text.replace(key, "");
    26. Map picMap = (Map) value;
    27. int width = Integer.parseInt(picMap.get("width").toString());
    28. int height = Integer.parseInt(picMap.get("height").toString());
    29. int picType = getPictureType(picMap.get("type").toString());
    30. FileInputStream fis = (FileInputStream) picMap.get("content");
    31. try {
    32. paragraph.setAlignment(ParagraphAlignment.CENTER);//样式居中
    33. String blipId = document.addPictureData(fis, picType);
    34. int id = document.getNextPicNameNumber(picType);
    35. XWPFUtils.createPicture(id, blipId, width, height, run);
    36. } catch (Exception e) {
    37. e.printStackTrace();
    38. }
    39. } else if (value instanceof List) {
    40. text = text.replace(key, "");
    41. List> tableData = (List>) value;
    42. XmlCursor cursor = paragraph.getCTP().newCursor();
    43. XWPFTable tableOne = document.insertNewTbl(cursor);// ---这个是关键
    44. //表格生成 6行5列.
    45. int rows = tableData.size();
    46. int cols = tableData.get(0).size();
    47. XWPFHelperTable xwpfHelperTable = new XWPFHelperTable();
    48. xwpfHelperTable.setTableWidthAndHAlign(tableOne, "9000", STJc.CENTER);
    49. // xwpfHelperTable.setTableHeight(tableOne, 560, STVerticalJc.CENTER);
    50. //表格创建
    51. XWPFTableRow tableRowTitle = tableOne.getRow(0);
    52. for (int i = 0; i < tableData.get(0).size(); i++) {
    53. XWPFParagraph cellParagraph;
    54. if (i == 0) {
    55. // tableRowTitle.getCell(0).setText(tableData.get(0).get(i).toString());
    56. cellParagraph = tableRowTitle.getCell(0).addParagraph();
    57. } else {
    58. // tableRowTitle.addNewTableCell().setText(tableData.get(0).get(i).toString());
    59. cellParagraph = tableRowTitle.addNewTableCell().addParagraph();
    60. }
    61. XWPFRun cellParagraphRun = cellParagraph.createRun();
    62. cellParagraph.setAlignment(ParagraphAlignment.CENTER);//样式居中
    63. cellParagraphRun.setFontSize(15); //字体大小
    64. cellParagraphRun.setFontFamily("仿宋");//设置字体
    65. cellParagraphRun.setText(changeText(tableData.get(0).get(i)));
    66. }
    67. for (int i = 1; i < rows; i++) {
    68. XWPFTableRow createRow = tableOne.createRow();
    69. for (int j = 0; j < cols; j++) {
    70. // createRow.getCell(j).setText(changeText(tableData.get(i).get(j)));
    71. XWPFParagraph cellParagraph = createRow.getCell(j).addParagraph();
    72. XWPFRun cellParagraphRun = cellParagraph.createRun();
    73. cellParagraph.setAlignment(ParagraphAlignment.CENTER);//样式居中
    74. cellParagraphRun.setFontSize(15); //字体大小
    75. cellParagraphRun.setFontFamily("仿宋");//设置字体
    76. cellParagraphRun.setText(changeText(tableData.get(i).get(j)));
    77. }
    78. }
    79. }
    80. }
    81. }
    82. //替换模板原来位置
    83. run.setText(text, 0);
    84. }

     增加了截图代码后,生成的模板word文档出现了换行效果。

  • 相关阅读:
    BUUCTF: [极客大挑战 2019]EasySQL 1
    BSN IPFS(星际文件系统)专网简介、功能、架构及特性、接入说明
    A tour of gRPC:07 - gRPC bidirectional straming 双向流
    ubuntu20.4 执行sudo apt-get update出现错误 libnettle.so.6 动态链接库错误
    Python控制程控电源
    基于最近电平逼近的开环MMC逆变器Simulink仿真模型
    【Java】异常
    Selenium原理深度解析
    RocketMQ各种消息的生产与消费Demo
    【网络爬虫 | Python】数字货币ok链上bitcoin大额交易实时爬取,存入 mysql 数据库
  • 原文地址:https://blog.csdn.net/fyc__iOS/article/details/126055075