java生成word文档 图片_java生成带有图片的word的文档-Go语言中文社区

生成带有图片的word的文档

@RequestMapping(params ="getWordByDate",produces = "text/html;charset=UTF-8")

@ResponseBody

public String getWordByDate(String ImgInfo,String startDate,String endDate) throws IOException, ParseException {

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");

SimpleDateFormat sdfs = new SimpleDateFormat("yyyy年MM月");

String result = null;

try {

//日期

Date date = sdf.parse(startDate);

String start = sdfs.format(date);

Date dateEnd = sdf.parse(endDate);

String end = sdfs.format(dateEnd);

//模板路径

String templ = "D://word.docx";

//图片路径

String wordPath  = "D://";

Map datas = new HashMap(){{

put("startDate", start);

put("endDate", end);

if(StringUtil.isNotEmpty(startDate) && StringUtil.isNotEmpty(endDate) ){

put("title",new TextRenderData(start+"至"+end+"文档报告"));

}else{

put("title",new TextRenderData("文档报告"));

}

//插入图片  PictureRenderData对象  参数:  宽 高 图片文件地址

put("ImgInfo", new PictureRenderData(520, 360, wordPath+"/"+ImgInfo));

//插入各种文字段落  TextRenderData

put("info",new TextRenderData("000000","文档内容1"));

...............

..............

put("info......",new TextRenderData("000000","文档内容n........"));

}};

if(datas.size() > 1){

//将生成的文件保存到指定目录下

String filePath ="D://file";

XWPFTemplate template = XWPFTemplate.create(templ);

RenderAPI.render(template, datas);

Date day=new Date();

SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");

//输出文件

result =df.format(day)+".docx";

File outFile = new File(filePath+"/"+result);

//如果输出目标文件夹不存在,则创建

if (!outFile.getParentFile().exists()){

outFile.getParentFile().mkdirs();

}

FileOutputStream out = new FileOutputStream(outFile);

template.write(out);

out.flush();

out.close();

template.close();

}

}finally {

}

return result;

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值