1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > java开发html如何转换为word Java怎么将html转换成word

java开发html如何转换为word Java怎么将html转换成word

时间:2018-12-26 12:18:41

相关推荐

java开发html如何转换为word Java怎么将html转换成word

Java如何将html转换成word

1、有一个比较复杂的页面,

能不能直接转成word?

因为页面涉及到一些样式、图片啥的

2、如果不能直接转的话,

能不能提取html中table里面的数据然后转成word里面的表格?

我分段来生成这个word文档。

------解决思路----------------------

本人最近一个项目也是这个需求,谈下我们目前的解决方法:首先html要有该有的格式,要注意编码什么的。

html=html.replaceAll("","

");

StringfileName=yaxx.getYamc()+".doc";

finalStringuserAgent=request.getHeader("USER-AGENT");

try{

InputStreaminput=newByteArrayInputStream(html.getBytes());

BufferedInputStreambr=newBufferedInputStream(input);

byte[]buf=newbyte[1024];

intlen=0;

response.reset();//非常重要

//纯下载方式

response.setContentType("application/x-msdownload");

if(-1!=userAgent.indexOf("Firefox")){//Firefox

fileName="=?UTF-8?B?"+(newString(mons.codec.binary.Base64.encodeBase64(fileName.getBytes("UTF-8"))))+"?=";

}elseif(-1!=userAgent.indexOf("Chrome")){//Chrome

fileName=newString(fileName.getBytes(),"ISO8859-1");

}else{//IE7+

fileName=.URLEncoder.encode(fileName,"UTF-8");

fileName=StringUtils.replace(fileName,"+","%20");//替换空格

}

response.setHeader("Content-Disposition","attachment;filename="+fileName);

OutputStreamout=response.getOutputStream();

while((len=br.read(buf))>0)

out.write(buf,0,len);

br.close();

out.close();

后面直接下载即可,至于图片的话,虽然能正常显示,但是并不是图片,而是路径。

------解决思路----------------------

页面上的图片一般都是img标签链的,可以解析其连接,通过URL下载图片到本地,再通过word的api,将图片写入word

------解决思路----------------------

楼主请参考/blog/376084

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