1
我试图从一个Java应用程序创建一个word文档,并且使用相同的Apache POI稳定版本3.7。当我尝试更改段落的字体时,即使字体系列存在,我也遇到空指针异常。事实上,如果我调用任何字体家族的功能,它会给出一个n。。继承人的代码:Apache POI SetFontFamily
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();
}
}
}
这里是个例外:
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如何可靠地创建格式化的文档?
是的,我发现它。感谢反正oers!我已经下载了版本3.8.5,这是一个开发版本,它似乎在那里工作。 Apache POI如何可靠地创建Word文档? – Will 2012-02-10 09:41:46
@我不知道我只用它作为excel,但它通常是一个很好的框架 – oers 2012-02-10 09:45:20
好的谢谢你的帮助@oers !!! – Will 2012-02-10 09:58:43