首页 文章

Apache POI SetFontFamily

提问于
浏览
1

我正在尝试从Java应用程序创建一个word文档,并使用Apache POI稳定版本3.7 . 当我尝试更改段落的字体时,即使字体系列存在,我也遇到空指针异常 . 事实上,如果我用任何字体系列调用该函数,它会给出一个npe . 下面是代码:

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;

/**
 *
 * @author william
 */
public class CreateDocumentFromScratch 
{

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

        XWPFParagraph paragraphOne = document.createParagraph();


        XWPFRun paragraphOneRunOne = paragraphOne.createRun();

        paragraphOneRunOne.setFontFamily("Arial");
        paragraphOneRunOne.setText("Hello");



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

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

}

Here is the exception:

run:
Exception in thread "main" java.lang.NullPointerException
    at org.apache.poi.xwpf.usermodel.XWPFRun.setFontFamily(XWPFRun.java:413)
    at pdftest.CreateDocumentFromScratch.main(CreateDocumentFromScratch.java:30)

知道我做错了什么吗?另外,Apache POI创建格式化的word文档有多可靠?

1 回答

相关问题