700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > java导出word文档(easypoi)

java导出word文档(easypoi)

时间:2019-11-22 14:04:20

相关推荐

java导出word文档(easypoi)

导出word,引用的是easypoi的jar

第一步:导入jar

<dependency><groupId>cn.afterturn</groupId><artifactId>easypoi-base</artifactId><version>3.0.3</version></dependency><dependency><groupId>cn.afterturn</groupId><artifactId>easypoi-web</artifactId><version>3.0.3</version></dependency><dependency><groupId>cn.afterturn</groupId><artifactId>easypoi-annotation</artifactId><version>3.0.3</version></dependency>

第二步:

编辑word文档 ,easypoi支持word版本为的

xxx.docx

记得使用英文输入法 :{}

将建好的模板放到项目里名为word文件夹下

第三步:开始编辑代码

传入的是defExpert对象,对象中的属性赋值给word文档上

首先模板中用到了图片,从服务器下载图片到本地,设置在word中显示的高度和宽度使用WordImageEntity对象。

图中的第一步:把图片下载到本地调用outImage方法

public void outImage(String path,String localPath,String name) { //HttpURLConnection conn = null;InputStream inputStream = null;BufferedInputStream bis = null;FileOutputStream out = null;try{File file0=new File(localPath);if(!file0.isDirectory()&&!file0.exists()){file0.mkdirs();}out = new FileOutputStream(file0+"\\"+name+".jpg");// 建立链接URL httpUrl=new URL(path);conn=(HttpURLConnection) httpUrl.openConnection();//以Post方式提交表单,默认get方式conn.setRequestMethod("GET");conn.setDoInput(true);conn.setDoOutput(true);// post方式不能使用缓存conn.setUseCaches(false);//连接指定的资源conn.connect();//获取网络输入流inputStream=conn.getInputStream();bis = new BufferedInputStream(inputStream);byte b [] = new byte[1024];int len = 0;while((len=bis.read(b))!=-1){out.write(b, 0, len);}System.out.println("下载完成...");} catch (Exception e) {e.printStackTrace();}finally{try {if(out!=null){out.close();}if(bis!=null){bis.close();}if(inputStream!=null){inputStream.close();}} catch (Exception e2) {e2.printStackTrace();}}}

第二步:

map中填写属性值和word模板中的名称一致

第三步:

调用easypoi的

防止word文件名乱码

String name = .URLDecoder.decode(defExpert.getName(),"utf-8"); //防止文件名乱码

以上就是使用easypoi导出word,如有什么错误或者疑问,请指教

另一种方式使用freeMarker导出word:/wjnjava/article/details/100288246

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。