html导出成word

maven坐标

<dependency>
            <groupId>fr.opensagres.xdocreport</groupId>
            <artifactId>org.apache.poi.xwpf.converter.core</artifactId>
            <version>1.0.6</version>
        </dependency>
        <dependency>
            <groupId>fr.opensagres.xdocreport</groupId>
            <artifactId>org.apache.poi.xwpf.converter.xhtml</artifactId>
            <version>1.0.6</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-scratchpad</artifactId>
            <version>3.14</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.14</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>3.14</version>
        </dependency>
        <dependency>
            <groupId>org.apache.xmlbeans</groupId>
            <artifactId>xmlbeans</artifactId>
            <version>2.6.0</version>
        </dependency>

工具类代码

public Map writeWordFile(String txt,String fileName,HttpServletRequest request) {
        String uri = "";
        String realPath = null;
        fileName=fileName+System.currentTimeMillis()+"";
        HashMap<String, String> map = new HashMap<>();

        String outFileName=fileName+".doc";
        try {
            if (!"".equals(uploadPath)) {
                // 检查目录是否存在
                File fileDir = new File(uploadPath);

                if (!fileDir.exists()){
                    fileDir.mkdir();
                }

                byte b[] = txt.getBytes();
                ByteArrayInputStream bais = new ByteArrayInputStream(b);
                POIFSFileSystem poifs = new POIFSFileSystem();
                DirectoryEntry directory = poifs.getRoot();
                DocumentEntry documentEntry = directory.createDocument("WordDocument", bais);
                FileOutputStream ostream = new FileOutputStream(uploadPath+ outFileName);
                poifs.writeFilesystem(ostream);

                /*关闭流*/
                ostream.close();
                bais.close();

                //创建访问路径
                realPath = request.getSession().getServletContext().getRealPath(fileName);
                System.out.println(realPath);
                uri = request.getScheme() + "://" + request.getServerName() + ":" +
                        request.getServerPort() + accessPath.substring(0,accessPath.lastIndexOf("/")+1)+outFileName;

                map.put("uri",uri);
                map.put("fileName",outFileName);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

        return map;
    }

controller方法调用

/**
*fileName 文件名字
*txt  html内容
*/
    @RequestMapping("/translate")
    @ResponseBody
    public Object translate(String fileName,String txt,HttpServletRequest request) {
      
                Map map = writeWordFile(txt, fileName, request);
                
     
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值