使用poi将图片添加到文档

时间:2014-10-03 21:57:50

标签: java apache-poi

我正处于异常

之下
Exception in thread "main" java.lang.NoSuchMethodError: org.apache.poi.util.IOUtils.toByteArray(Ljava/io/InputStream;)[B
    at org.apache.poi.xwpf.usermodel.XWPFDocument.addPictureData(XWPFDocument.java:1198)
    at org.apache.poi.xwpf.usermodel.XWPFRun.addPicture(XWPFRun.java:680)
    at signature.Main.createFile(Main.java:85)
    at signature.Main.listFilesForFolder(Main.java:52)
    at signature.Main.main(Main.java:29)

尝试使用apache POI将图片添加到docs文件时

代码如下

                XWPFDocument docx = new XWPFDocument();
                XWPFParagraph par = docx.createParagraph();
                XWPFRun run = par.createRun();
                run.setText(imageUrl);
                File picture = new File("D:\\1.jpg");
                FileInputStream in = new FileInputStream(picture);
                run.addPicture(in, Document.PICTURE_TYPE_JPEG, "1", 100, 100);
                FileOutputStream out = new FileOutputStream(f); 
                docx.write(out); 
                out.close();   

1 个答案:

答案 0 :(得分:-1)

有点晚了,但我找到了一个关于POI库的简短教程,可以帮助你将文章添加到doc,希望它有所帮助:http://developeriq.in/articles/2012/jul/03/handling-ms-word-documents-using-apache-poi/