Java的XWPFTemplate工具类导出word.docx的使用
•
Jave
依赖
com.deepoove
poi-tl
1.7.3
org.apache.poi
poi-ooxml
4.1.2
org.apache.poi
poi-ooxml-schemas
4.1.2
org.apache.poi
poi
4.1.2
代码
基础语法
public void aaa() {
String filePath = "D:\\test\\巡查日志.docx";
XWPFTemplate template = XWPFTemplate.compile(filePath);
// 填充数据
Map data = new HashMap();
data.put("inspectionTime", "(1)第一行\n(2)第二行");
data.put("deptName", 123);
// 读取本地磁盘图片
data.put("weChatPicture", new PictureRenderData(100, 100, "C:\\Users\\Administrator\\Pictures\\16194037861239194.jpg"));
// 通过url读取网络图片
data.put("picture", new PictureRenderData(200, 400, ".png", BytePictureUtils.getUrlByteArray("https://res.wx.qq.com/a/wx_fed/weixin_portal/res/static/img/1EtCRvm.png")));
template.render(data);
File file = new File("D:\\test\\test1.docx");
// 保存Word文档
FileOutputStream out = new FileOutputStream(file);
template.write(out);
out.close();
}
模板使用{{占位符}}
图片{{@占位符}}
换行\n

图片和文字遍历使用
读取文字:{{?photoCollection}}{{pho}}{{/photoCollection}}
读取照片:{{?photoCollection}} {@{pho}} {{/photoCollection}}
特别注意:读取照片的时候需要有一个空格才会显示,找了好久的问题,最后加一个空格解决了
文字
public static void main(String[] args) throws IOException {
String filePath = "D:\\test\\巡查日志.docx";
XWPFTemplate template = XWPFTemplate.compile(filePath);
Map map = new HashMap();
List<Map> maps1 = new ArrayList();
for (int i = 1; i <= 5; i++) {
Map m = new HashMap();
m.put("pho",", 哈哈哈"+i);
maps1.add(m);
}
map.put("photoCollection", maps1);
template.render(map);
File file = new File("D:\\test\\test1.docx");
FileOutputStream out = new FileOutputStream(file);
template.write(out);
out.close();
}
照片
public static void main(String[] args) throws IOException {
String filePath = "D:\\test\\巡查日志.docx";
XWPFTemplate template = XWPFTemplate.compile(filePath);
Map map = new HashMap();
List<Map> maps1 = new ArrayList();
for (int i = 1; i <= 5; i++) {
Map m = new HashMap();
// 读取本地磁盘图片
m.put("pho", new PictureRenderData(30, 30, "C:\\Users\\28430\\Pictures\\Camera Roll\\1.jpg"));
maps1.add(m);
}
map.put("photoCollection", maps1);
map.put("pho1", new PictureRenderData(100, 100, "C:\\Users\\28430\\Pictures\\Camera Roll\\1.jpg"));
template.render(map);
File file = new File("D:\\test\\test1.docx");
FileOutputStream out = new FileOutputStream(file);
template.write(out);
out.close();
}

列表的使用
public static void main(String[] args) throws IOException {
DecimalFormat df = new DecimalFormat("######0.00");
Calendar now = Calendar.getInstance();
double money = 0;//总金额
//组装表格列表数据
List<Map> detailList=new ArrayList<Map>();
for (int i = 0; i < 6; i++) {
Map detailMap = new HashMap();
detailMap.put("index", i+1);//序号
detailMap.put("title", "商品"+i);//商品名称
detailMap.put("product_description", "套");//商品规格
detailMap.put("buy_num", 3+i);//销售数量
detailMap.put("saleprice", 100+i);//销售价格
double saleprice=Double.parseDouble(String.valueOf(100+i));
int buy_num= Integer.parseInt(String.valueOf(3 + i));
String buy_price=df.format(saleprice*buy_num);
detailMap.put("buy_price", buy_price);//单个商品总价格
money=money+Double.parseDouble(buy_price);
detailList.add(detailMap);
}
//总金额
String order_money=String.valueOf(money);
String filePath = "D:\\test\\order12.docx";
//渲染表格
HackLoopTableRenderPolicy policy = new HackLoopTableRenderPolicy();
Configure config = Configure.newBuilder().bind("detailList", policy).build();
Map map = new HashMap();
map.put("detailList", detailList);
map.put("order_number", "2356346346645");
map.put("y", now.get(Calendar.YEAR));//当前年
map.put("m", (now.get(Calendar.MONTH) + 1));//当前月
map.put("d", now.get(Calendar.DAY_OF_MONTH));//当前日
map.put("order_money",order_money);//总金额
XWPFTemplate template = XWPFTemplate.compile(filePath, config).render(map);
File file = new File("D:\\test\\test1.docx");
FileOutputStream out = new FileOutputStream(file);
template.write(out);
out.close();
}


本文来自网络,不代表协通编程立场,如若转载,请注明出处:https://net2asp.com/f51ed2a312.html
