【问题标题】:XWPFRun.setText() doesn't seem to respect line breaks or tabs?XWPFRun.setText() 似乎不尊重换行符或制表符?
【发布时间】:2013-05-15 10:33:41
【问题描述】:

这是示例代码,“\t”不适用于此 setText 方法?:

XWPFDocument document = new XWPFDocument();
XWPFParagraph tp = document.createParagraph();
XWPFRun tRun = tp.createRun();
tRun.setText("a");
tRun.setText("\t"); // 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();
}

【问题讨论】:

    标签: java ms-office apache-poi doc


    【解决方案1】:

    这不是在运行中添加制表符或换行符的方式。 Microsoft Words 生成文件的方式是添加特殊的中断样式元素,因此这也是您需要在 Apache POI 中执行的操作,因为格式就是这样工作的。

    您可以在testAddTabsAndLineBreaks() of TestXWPFRun 中查看添加标签的示例。您的代码必须是:

    XWPFRun tRun = tp.createRun();
    tRun.setText("a");
    tRun.addTab();
    tRun.setText("b");
    

    (您还需要使用新的 Apache POI 副本以获得 addTab() 支持)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-07-12
      • 1970-01-01
      • 1970-01-01
      • 2010-11-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多