基于模版生成word
- 1、引入maven
- 2、word模版编写
- 3、java代码
- 4、效果
- 5、word转pdf
AutoPoi的主要特点
参考文献
https://help.jeecg.com/autopoi/autopoi/prequel/test.html
1.设计精巧,使用简单
2.接口丰富,扩展简单
3.默认值多,write less do more
4.spring mvc支持,web导出可以简单明了
1、引入maven
<dependency><groupId>org.jeecgframework</groupId><artifactId>autopoi-web</artifactId><version>1.4.8</version>
</dependency>
2、word模版编写
目前仅支持表格循环、变量替换
{{department}}
{{person}}
{{image}}
{{time}}
序列号 | xx | 名称 | 小学生 |
---|---|---|---|
{{$fe:testList}}{{.id.}} | {{zijin}} | {{quancheng}} | {{bianma}} |
图片以供模版参考
3、java代码
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.jeecg.common.util.DateUtils;
import org.jeecgframework.poi.word.WordExportUtil;
import org.jeecgframework.poi.word.entity.WordImageEntity;import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;public class WordExportUtils {public static void main(String[] args) {Map<String, Object> map = new HashMap<String, Object>();map.put("department", "Easypoi");map.put("person", "JueYue");map.put("time", DateUtils.formatDate(new Date()));String templateFileUrl = "C:\\Users\\123\\Desktop\\1\\667\\1111.docx";List<Map<String, Object>> listMap = new ArrayList<Map<String, Object>>();for (int i = 0; i < 4; i++) {Map<String, Object> lm = new HashMap<String, Object>();lm.put("id", i + 1 + "");lm.put("zijin", i * 10000 + "");lm.put("bianma", "A001");lm.put("mingcheng", "设计");lm.put("xiangmumingcheng", "EasyPoi " + i + "期");lm.put("quancheng", "开源项目");lm.put("sqje", i * 10000 + "");lm.put("hdje", i * 10000 + "");lm.put("$fe:testList", lm);listMap.add(lm);}map.put("testList", listMap);WordImageEntity image = new WordImageEntity();image.setHeight(200);image.setWidth(500);image.setUrl("/static/337.png");image.setType(WordImageEntity.URL);map.put("image", image);XWPFDocument doc = imageWordExport(map, templateFileUrl);try {FileOutputStream fos = new FileOutputStream("C:\\Users\\123\\Desktop\\1\\667\\1111After.docx");doc.write(fos);fos.close();ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();doc.write(byteArrayOutputStream);System.out.println("总字节大小:" + byteArrayOutputStream.toByteArray().length);} catch (IOException e) {e.printStackTrace();}}public static XWPFDocument imageWordExport(Map<String, Object> map, String templateFileUrl) {try {return WordExportUtil.exportWord07(templateFileUrl, map);} catch (Exception e) {e.printStackTrace();}return null;}}
4、效果
5、word转pdf
请参考文章
https://blog.csdn.net/Tm598035415/article/details/140663654?utm_source%20=%20uc_fansmsg