import com.deepoove.poi.XWPFTemplate;
import com.deepoove.poi.data.PictureRenderData;
import org.springframework.core.io.ClassPathResource;
import java.io.*;
import java.util.HashMap;
import java.util.Map;
public class url123 {
public static void asd() throws IOException {
try {
Map<String, Object> data = new HashMap<String, Object>();
File file = new File("");
String filePath1 = file.getCanonicalPath();//获取项目路径
// 文本
data.put("name", "张三");
//图片
data.put("photo", new PictureRenderData(127, 185, "D:/.jpg"));
// 写入word输出
System.out.println(filePath1+"\\wordTest.docx");
ClassPathResource template = new ClassPathResource("D:/word.docx"); //获取模板
String filePath = template.getPath(); //读取图片路径
XWPFTemplate xwpfTemplate = XWPFTemplate.compile(filePath).render(data); //填充word
String docName = filePath1+System.currentTimeMillis() + ".docx"; //生成word路径
File targetFile = new File(docName); //新建File文件对象
FileOutputStream out = new FileOutputStream(targetFile); //FileOutputStream文件字节输出流
xwpfTemplate.write(out);
out.flush(); //刷新
out.close(); //关流
xwpfTemplate.close();
} catch (
IOException e) {
e.printStackTrace();
}
}
}
06-08