java - Apache POI - Docx 输出问题

标签 java file locale apache-poi encode

我正在评估 apache poi 作为编写 docx 文件的选项。我正在寻找的具体内容是在不同语言的 docx 文件中生成内容(具体来说是印地语/马拉地语)。我面临以下问题:

当写入 docx 文件时,“Hindi/Marathi”文本显示为方框,即使字体“Arial Unicode MS”支持它。重点是,当我们选中复选框时,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();
        }
    }

}

我们将不胜感激。

最佳答案

复活一个非常古老的帖子; OP 可以确认正在使用的 MS Office 版本吗?问题似乎出在 Windows XP 上运行的 MS Office 2003。但它也可能在更高的操作系统版本上。

似乎 MS Word 为印地语脚本应用了 Mangal 字体 [编码标准:印度语:印地语 ISCII 57002 (Devanagari)]。以下链接解释了这一点:

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

建议的解决方法: 在 Windows XP 控制面板中,选择区域和语言选项。选择语言。选中“为复杂脚本和从右到左的语言(包括泰语)安装文件”复选框。

重启电脑。

但是,在 Windows 上使用 LibreOffice 版本 4.3.5.2 和在 Linux (Ubuntu) 上使用 LibreOffice 4.2.7.2 打开文件时没有观察到此类问题。

使用了以下库: 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

关于java - Apache POI - Docx 输出问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9258318/

相关文章:

Java 文件输出流 : path relative to program folder?

Java SQL 结果集数据类型

Java intellij 无效的最大堆大小

c - 我的程序在我更改后崩溃了。从文件中读取

c++ - 将数据从数据库正确加载到编辑控件中,以便我可以执行 ADO 查询

python - f 字符串中的整数表示类型不返回预期格式

java - 启动 AppEngine MapJob 出现错误 : com. google.appengine.api.modules.ModulesException:未知模块

algorithm - 给定文件名列表,返回具有相同内容的文件列表列表 - 面试题

c - 如何在c中将函数输入到文件中

angular - 为什么 Angular 日期管道不支持像 Intl.DateTimeFormat 这样的区域设置语言默认格式?