2013-05-15 469 views
1

这里是示例代码中,“\ t”的不是这个方法的setText工作?:XWPFRun.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(); 
} 

回答

2

这不是你如何添加选项卡或换行到一个运行。 Microsoft Words生成文件的方式是添加特殊的中断样式元素,因此这也是您需要在Apache POI中执行的操作,因为这是格式的工作方式。

您可以在testAddTabsAndLineBreaks() of TestXWPFRun中看到添加标签的示例。你的代码需要:

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

(你需要使用的Apache POI的新副本也为addTab()支持)