我正在尝试在创建docx文件的android上创建一个应用程序。最初,我在设置docx4j库时遇到了一些困难,但是在添加了几个其他库之后,它似乎可以正常工作。
我尝试使用以下代码创建docx文件:
private class GenerateWord extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... voids) {
String name = "Test.docx";
try {
WordprocessingMLPackage wordPackage = WordprocessingMLPackage.createPackage();
MainDocumentPart mainDocumentPart = wordPackage.getMainDocumentPart();
mainDocumentPart.addStyledParagraphOfText("Title", "Some Title");
mainDocumentPart.addParagraphOfText("Some paragraph");
String avaliablePath = getFilesDir().getAbsolutePath();
String doc = avaliablePath + "/" + "Test.docx";
File exportFile = new File(doc);
wordPackage.save(exportFile);
}catch (Exception e){
e.printStackTrace();
}
return null;
}
}
这将返回错误:
10-25 22:12:39.807 3216-3303/com.example.examples W/System.err: log4j:WARN No appenders could be found for logger (org.docx4j.utils.ResourceUtils).
10-25 22:12:39.807 3216-3303/com.example.examples W/System.err: log4j:WARN Please initialize the log4j system properly.
10-25 22:12:39.807 3216-3303/com.example.examples W/System.err: log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
10-25 22:12:43.471 3216-3303/com.example.examples W/System.err: java.lang.NullPointerException: Attempt to invoke virtual method 'java.util.List org.docx4j.wml.Styles.getStyle()' on a null object reference
10-25 22:12:43.471 3216-3303/com.example.examples W/System.err: at org.docx4j.model.PropertyResolver.initialiseLiveStyles(PropertyResolver.java:1223)
10-25 22:12:43.471 3216-3303/com.example.examples W/System.err: at org.docx4j.model.PropertyResolver.init(PropertyResolver.java:180)
10-25 22:12:43.471 3216-3303/com.example.examples W/System.err: at org.docx4j.model.PropertyResolver.<init>(PropertyResolver.java:145)
10-25 22:12:43.471 3216-3303/com.example.examples W/System.err: at org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart.getPropertyResolver(MainDocumentPart.java:162)
10-25 22:12:43.471 3216-3303/com.example.examples W/System.err: at org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart.createStyledParagraphOfText(MainDocumentPart.java:634)
10-25 22:12:43.471 3216-3303/com.example.examples W/System.err: at org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart.addStyledParagraphOfText(MainDocumentPart.java:614)
10-25 22:12:43.471 3216-3303/com.example.examples W/System.err: at com.example.examples.ActivityForTest$GenerateWord.doInBackground(ActivityForTest.java:47)
10-25 22:12:43.471 3216-3303/com.example.examples W/System.err: at com.example.examples.ActivityForTest$GenerateWord.doInBackground(ActivityForTest.java:35)
10-25 22:12:43.471 3216-3303/com.example.examples W/System.err: at android.os.AsyncTask$2.call(AsyncTask.java:292)
10-25 22:12:43.471 3216-3303/com.example.examples W/System.err: at java.util.concurrent.FutureTask.run(FutureTask.java:237)
10-25 22:12:43.471 3216-3303/com.example.examples W/System.err: at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
10-25 22:12:43.471 3216-3303/com.example.examples W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
10-25 22:12:43.471 3216-3303/com.example.examples W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
10-25 22:12:43.471 3216-3303/com.example.examples W/System.err: at java.lang.Thread.run(Thread.java:818)
该错误发生在以下行:mainDocumentPart.addStyledParagraphOfText(“ Title”,“ Some Title”);
即使删除此行,也会发生其他错误。最后,我只想知道使用docx4j创建新docx文件的正确方法。