我正在打开一个word文件并将其用作模板。我已经运行了我的测试程序和我的实际程序。在我的JUnit测试中,它在Groovy中,一切正常,我输出文件没有问题。当我在我的应用程序中运行完全相同的代码并尝试打开单词doc(实际上是docx)时,我收到错误消息,“由于内容存在问题,无法打开文件TestGenerated.docx。”在详细信息下,它说“参数不正确。位置:部分:/word/document.xml,行:2,列:0”
代码如下:
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.util.Units;
import org.apache.poi.xwpf.usermodel.*;
import org.apache.xmlbeans.XmlCursor;
import javax.swing.*;
import javax.swing.filechooser.FileSystemView;
import java.awt.*;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.logging.Logger;
public class TestInJava {
XWPFDocument document;
FileOutputStream out;
File output;
public TestInJava() {
try {
//This file is the first you find when you search for sample word docx file
File file = new File("C:\\Users\\usmcphysicist\\Downloads\\trans_jour.docx");
FileInputStream in = new FileInputStream(file);
document = new XWPFDocument(in);
output = new File(FileSystemView.getFileSystemView().getHomeDirectory() + "/TestGenerated.docx");
out = new FileOutputStream(output);
XWPFRun titleRun = document.getParagraphs().get(7).getRuns().get(0);
XWPFRun wordsRun = document.getParagraphs().get(20).createRun();
XWPFParagraph pictureParagraph = document.getParagraphs().get(40);
titleRun.setText("Outputting Apache POI", 0);
wordsRun.setText("Something in the body", 0);
wordsRun.setColor("42f45c");
File picture = new File("C:\\Users\\usmcphysicist\\Pictures\\flag200.png");
XWPFRun pictureRun = pictureParagraph.createRun();
try {
pictureRun.addPicture(new FileInputStream(picture), XWPFDocument.PICTURE_TYPE_PNG, file.getAbsolutePath(),
Units.toEMU(200), Units.toEMU(112));
} catch (InvalidFormatException e) {
JOptionPane.showMessageDialog(null, "Picture didn't print");
}
XWPFParagraph tableParagraph = document.getParagraphs().get(50);
XmlCursor cursor = tableParagraph.getCTP().newCursor();
XWPFTable table = document.insertNewTbl(cursor);
XWPFTableRow row1 = table.getRow(0);
row1.setRepeatHeader(true);
row1.getCell(0).setText("Release Order");
row1.addNewTableCell();
row1.getCell(1).setText("Episode");
row1.addNewTableCell();
row1.getCell(2).setText("Name");
XWPFTableRow row = table.createRow();
row.getCell(0).setText(Integer.toString(1));
row.getCell(1).setText(Integer.toString(4));
row.getCell(2).setText("A New Hope");
row = table.createRow();
row.getCell(0).setText(Integer.toString(2));
row.getCell(1).setText(Integer.toString(5));
row.getCell(2).setText("Empire Strikes Back");
row = table.createRow();
row.getCell(0).setText(Integer.toString(3));
row.getCell(1).setText(Integer.toString(6));
row.getCell(2).setText("Return of the Jedi");
row = table.createRow();
row.getCell(0).setText(Integer.toString(4));
row.getCell(1).setText(Integer.toString(1));
row.getCell(2).setText("The Phantom Menace");
row = table.createRow();
row.getCell(0).setText(Integer.toString(5));
row.getCell(1).setText(Integer.toString(2));
row.getCell(2).setText("Attack of the Clones");
row = table.createRow();
row.getCell(0).setText(Integer.toString(6));
row.getCell(1).setText(Integer.toString(3));
row.getCell(2).setText("Revenge of the Sith");
document.write(out);
out.close();
Desktop.getDesktop().open(output);
} catch (IOException e) {
JOptionPane.showMessageDialog(null, "IoException");
}
}
}
答案 0 :(得分:0)
这最终在Apache Poi 3.16中将图像插入文档时出错。当我升级到POI 3.17时,所有问题都得到了解决。