2012-02-13 119 views
6

我正在評估apache poi作爲編寫docx文件的選項。我正在尋找的具體事情是在不同語言的docx文件中生成內容(具體而言,印地語/馬拉地語)。我面臨以下問題:Apache POI - Docx輸出問題

當docx文件被寫入時,即使字體「Arial Unicode MS」支持它,「Hindi/Marathi」文本也可以作爲方框顯示。重點是,當我們檢查框時,MS Word將字體顯示爲「Cailbri」,即使我已明確將字體設置爲「Arial Unicode MS」。如果我選擇MS Word中的框,然後將字體更改爲「Arial Unicode MS」,則可以正確顯示印地文/馬拉地語單詞。任何想法爲什麼發生這種情況請注意我使用POI的開發版本,因爲之前的穩定版本不支持設置字體系列。這裏是來源:

import java.io.FileNotFoundException; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import org.apache.poi.xwpf.usermodel.XWPFDocument; 
import org.apache.poi.xwpf.usermodel.XWPFParagraph; 
import org.apache.poi.xwpf.usermodel.XWPFRun; 

public class CreateDocumentFromScratch 
{ 

    public static void main(String[] args) 
    { 
     XWPFDocument document = new XWPFDocument(); 

     XWPFParagraph paragraphTwo = document.createParagraph(); 
     XWPFRun paragraphTwoRunOne = paragraphTwo.createRun();  
     paragraphTwoRunOne.setFontFamily("Arial Unicode MS"); 
     paragraphTwoRunOne.setText("नसल्यास"); 


     XWPFParagraph paragraphThree = document.createParagraph(); 
     XWPFRun paragraphThreeRunOne = paragraphThree.createRun(); 
     paragraphThreeRunOne.setFontFamily("Arial Unicode MS"); 
     paragraphThreeRunOne.setText("This is nice"); 

     FileOutputStream outStream = null; 
     try { 
      outStream = new FileOutputStream("c:/will/First.doc"); 
     } catch (FileNotFoundException e) { 
      e.printStackTrace(); 
     } 

     try { 
      document.write(outStream); 
      outStream.close(); 
     } catch (FileNotFoundException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 

} 

任何幫助將不勝感激。

+0

請提供POI和MS Word的確切版本 - 似乎這個問題在最近的POI版本中不會發生。 – 2015-04-18 10:16:30

+1

我用POI API 3.11試過了你的代碼,它工作正常。我用MS Word 2013打開.doc文件 – esprittn 2015-05-03 06:18:27

回答

1

復活一個很舊的帖子; OP可以確認正在使用的MS Office的版本嗎?問題似乎是在Windows XP上運行的MS Office 2003。但是,它也可能在更高版本的操作系統上。

看來,MS Word應用Mangal字體的印地文腳本[編碼標準:印度語印地語ISCII 57002(梵文)]。下面的鏈接解釋這一點:

https://support.office.com/en-ca/article/Choose-text-encoding-when-you-open-and-save-files-60d59c21-88b5-4006-831c-d536d42fd861

建議的解決方法: 在Windows XP控制面板中選擇區域和語言選項。選擇語言。勾選「安裝複雜腳本和從右到左的語言(包括泰國)文件。

重新啓動電腦。

但是,使用LibreOffice的版本4.3.5.2在打開文件時沒有觀察到這樣的問題,Windows和LibreOffice的4.2.7.2在Linux(Ubuntu的)

使用下列庫: POI-3.10-FINAL-20140208.jar,POI-OOXML-3.10-FINAL-20140208.jar,
POI,OOXML -schemas-3.10-FINAL-20140208.jar,xmlbeans-2.3.0.jar, dom4j-1.6.1.jar,stax-api-1.0.1.jar