欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

html导出成word

程序员文章站 2024-03-20 15:33:40
...

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);
                
     
    }
相关标签: html导出word