1.POI的坑把人踩废了,4.0之后的都试过多少有点问题,着急麻黄终于用3.9的搞定了,分享一下 2.需要的lib包:下载链接 3.代码实现 import org.apache.poi.hwpf.HWPFDocument; import org.apache.poi.openxml4j.exceptions.InvalidFormatException; import org.apache.poi.xwpf.usermodel.XWPFDocument; import org.apache.poi.xwpf.usermodel.XWPFParagraph; import org.apache.poi.xwpf.usermodel.XWPFRun; import org.apache.xmlbeans.XmlException; import org.apache.xmlbeans.XmlToken; import org.openxmlformats.schemas.drawingml.x2006.main.CTPositiveSize2D; import org.openxmlformats.schemas.drawingml.x2006.wordprocessingDrawing.CTInline;
public void writeImgList2Word(List imgFile,String saveFile, boolean useA4Size,Callback result){
if(TextUtils.isEmpty(saveFile)) {
result.success(null);
return;
}
final File out = new File(saveFile);
if(writeThread != null) {
writeThread.interrupt();
}
writeThread = new Thread(){
@Override
public void run() {
OutputStream os = null;
InputStream is = null;
XWPFDocument d = null;
try {
Log.d("FileWriter", "file write start:" + imgFile);
d = new XWPFDocument();
XWPFParagraph paragraph = d.createParagraph();
XWPFRun run = paragraph.createRun();
for (int i = 0, imgFileSize = imgFile.size(); i < imgFileSize; i++) {
String s = imgFile.get(i);
File igf = new File(s);
is = new FileInputStream(igf);
//默认A4大小,留有边距,A4大小595*846
int width = 550;
int height = 740;
if (!useA4Size){
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(s, options);
width = options.outWidth;
height = options.outHeight;
}
run.addPicture(is, XWPFDocument.PICTURE_TYPE_PNG, igf.getName(), width, height);
createPicture(i, width, height, paragraph, d);
}
os = new FileOutputStream(out, true);
d.write(os);
Log.d("FileWriter", "file write finish");
} catch (IOException | InvalidFormatException e) {
e.printStackTrace();
} finally {
if(is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(os != null) {
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(!isInterrupted()) {
if(out.exists()) {
Log.d("FileWriter", "file write success");
result.success(saveFile);
} else {
Log.d("FileWriter", "file write fail");
result.success(null);
}
}
}//final
}//run
};
writeThread.start();
}
//感谢大神支援的方法,https://blog.csdn.net/kzhzhang/article/details/84924579 //没有这个方法图片写进去了,但是打开文档显示不出来 public void createPicture(int id, int width, int height, XWPFParagraph paragraph,XWPFDocument document) { final int EMU = 9525; width *= EMU; height *= EMU; String blipId = document.getRelationId(document.getAllPictures().get(id)); CTInline inline = paragraph.createRun().getCTR().addNewDrawing() .addNewInline(); String picXml = “” + “<a:graphic xmlns:a=“http://schemas.openxmlformats.org/drawingml/2006/main”>” + " <a:graphicData uri=“http://schemas.openxmlformats.org/drawingml/2006/picture”>" + " <pic:pic xmlns:pic=“http://schemas.openxmlformats.org/drawingml/2006/picture”>" + " pic:nvPicPr" + " <pic:cNvPr id="" + id + “” name=“Generated”/>" + " pic:cNvPicPr/" + " </pic:nvPicPr>" + " pic:blipFill" + " <a:blip r:embed="" + blipId + “” xmlns:r=“http://schemas.openxmlformats.org/officeDocument/2006/relationships”/>" + " <a:stretch>" + " <a:fillRect/>" + " </a:stretch>" + " </pic:blipFill>" + " pic:spPr" + " <a:xfrm>" + " <a:off x=“0” y=“0”/>" + " <a:ext cx="" + width + “” cy="" + height + “”/>" + " </a:xfrm>" + " <a:prstGeom prst=“rect”>" + " <a:avLst/>" + " </a:prstGeom>" + " </pic:spPr>" + " </pic:pic>" + " </a:graphicData>" + “</a:graphic>”;
inline.addNewGraphic().addNewGraphicData();
XmlToken xmlToken = null;
try {
xmlToken = XmlToken.Factory.parse(picXml);
} catch (XmlException xe) {
xe.printStackTrace();
}
inline.set(xmlToken);
inline.setDistT(0);
inline.setDistB(0);
inline.setDistL(0);
inline.setDistR(0);
CTPositiveSize2D extent = inline.addNewExtent();
extent.setCx(width);
extent.setCy(height);
}
|