import java.io.FileInputStream;
import java.io.FileOutputStream;
import org.apache.poi.util.Units;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
public class test {
public static void main(String[] args) throws Exception {
XWPFDocument doc = new XWPFDocument();
XWPFParagraph p = doc.createParagraph();
XWPFRun xwpfRun = p.createRun();
String[] IMageargs={
"c:/1.jpg","c:/2.jpg","c:/3.jpg","c:/4.jpg"
};
for (String imgFile : IMageargs) {
int format=XWPFDocument.PICTURE_TYPE_JPEG;
xwpfRun.setText(imgFile);
xwpfRun.addBreak();
xwpfRun.addPicture (new FileInputStream(imgFile), format, imgFile, Units.toEMU(200), Units.toEMU(200)); // 200x200 pixels
//xwpfRun.addBreak(BreakType.PAGE);
}
FileOutputStream out = new FileOutputStream("C:\test.docx");
doc.write(out);
out.close();
}
}
将文件放在路径[src文件夹]中的链接java file中,切记不要更改包结构.
我已经用Word 2007(apache poi 3.10)对其他版本进行了测试.