2013-05-15 474 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()支持)