java html转word!

最近写一个系统,需要把复文本的数据生成一个word文档,网上查了一些资料都觉的有点老了,就自己想了一个(暂时可以使用纯文本和表格),借助office本身可以存html的机制!还借助jsoup!直接上代码!

引入包的:

<dependency>
	<groupId>org.jsoup</groupId>
	<artifactId>jsoup</artifactId>
	<version>1.10.3</version>
</dependency>

代码:

/**
     *
     * @param content html body里面需要填充的内容
     * @param fileName 文件名
     * @param path 路径
     * @return
     */
    public static boolean htmlToWord(String content,String fileName,String path){
        try {
            //模板
            InputStream html=new FileInputStream("E:\\HtmlToWord\\Mod.html");
            String conte=getContent(html);
            Document document=Jsoup.parse(conte);
            Element body=document.body();
            body.html(content);
            File file=new File(path+fileName+".html");
            FileWriter fileWriter=new FileWriter(file);
            fileWriter.write(document.html());
            fileWriter.close();
            html.close();
            File file1=new File(path+fileName+".doc");
            if(file.renameTo(file1)){
                return true;
            }else {
                return false;
            }

        } catch (Exception e) {

            e.printStackTrace();
            return false;
        }
    }

    /**
     * 把输入流里面的内容以UTF-8编码当文本取出。
     * 不考虑异常,直接抛出
     * @param ises
     * @return
     * @throws IOException
     */
    private static String getContent(InputStream... ises) throws IOException {
        if (ises != null) {
            StringBuilder result = new StringBuilder();
            BufferedReader br;
            String line;
            for (InputStream is : ises) {
                br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
                while ((line=br.readLine()) != null) {
                    result.append(line);
                }
            }
            return result.toString();
        }
        return null;
    }
思路就是先存成html,再改名为word文档!
写的简单哈!有不足之处还望指点!!

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值