zoukankan      html  css  js  c++  java
  • POI把html写入word doc文件

    直接把Html文本写入到Word文件

    1. 获取查看页面的body内容和引用的css文件路径传入到后台。
    2. 把对应css文件的内容读取出来。
    3. 利用body内容和css文件的内容组成一个标准格式的Html文本。
    4. 根据组合后的Html文本生成对应的ByteArrayInputStream。
    5. 构建一个默认的POIFSFileSystem,并利用它和生成的ByteArrayInputStream创建一个WordDocument。
    6. 把构建的POIFSFileSystem写入到对应的输出流。

           经过上面这几步之后我们就可以把Html格式的文本写入到Word文件中,同时使生成的Word文件呈现出对应的Web样式。需要注意的是原本Html文件中引用到的css文件的内容需要放到生成的Word文件中,生成后的Word文件才会呈现出对应的Web样式。下面是一个针对于该方式的一个简单例子:

     public void htmlToWord2() throws Exception {
              InputStream bodyIs = new FileInputStream("f:\1.html");
              InputStream cssIs = new FileInputStream("f:\1.css");
              String body = this.getContent(bodyIs);
              String css = this.getContent(cssIs);
              //拼一个标准的HTML格式文档
              String content = "<html><head><style>" + css + "</style></head><body>" + body + "</body></html>";
              InputStream is = new ByteArrayInputStream(content.getBytes("GBK"));
              OutputStream os = new FileOutputStream("f:\1.doc");
              this.inputStreamToWord(is, os);
           }
          
           /**
            * 把is写入到对应的word输出流os中
            * 不考虑异常的捕获,直接抛出
            * @param is
            * @param os
            * @throws IOException
            */
           private void inputStreamToWord(InputStream is, OutputStream os) throws IOException {
              POIFSFileSystem fs = new POIFSFileSystem();
              //对应于org.apache.poi.hdf.extractor.WordDocument
              fs.createDocument(is, "WordDocument");
              fs.writeFilesystem(os);
              os.close();
              is.close();
           }
          
           /**
            * 把输入流里面的内容以UTF-8编码当文本取出。
            * 不考虑异常,直接抛出
            * @param ises
            * @return
            * @throws IOException
            */
           private 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;
           }

    1.css代码如下

    table {
           border: 1px solid blue;
            800px;
           height: 500px;
           text-align: center;
    }
    td {
            200px;
           border: 1px solid blue;
    }

    1.html对应的内容如下:

    <table cellpadding="5" style="border-collapse: collapse;">
           <tr>
                  <td>中文</td>
                  <td>中文</td>
                  <td>中文</td>
                  <td>中文</td>
           </tr>
           <tr>
                  <td>中文</td>
                  <td>中文</td>
                  <td>中文</td>
                  <td>中文</td>
           </tr>
    </table>

    效果图

    (注:本文是基于poi3.9所写)

  • 相关阅读:
    6章-项目进度管理-day5
    常规正则表达式
    axios删除接口
    elk
    英语笔记
    升级打怪
    用computed实现watch的保持子组件与父组件值同步
    vertical-align不生效的问题
    css居右
    使用maven创建spring工程出现配置文件打不开/不存在的错误
  • 原文地址:https://www.cnblogs.com/estellez/p/4091156.html
Copyright © 2011-2022 走看看