HTML转换为word的思路

思路1:利用读取HTMl源码,然后生成wod文档,这样做对客户机器要求比较严格,实现工作中并不可取

思路2:将HTML源码传回到服务器,服务器负责生成word文档,客户机器再下载

这样做可行的

1.第一步:利用JS获取html源码

2.第二步:post源码到后台

3.第三步:利用poi生成word

4.配置文件下载

通过以上方式我们完成了html转换为word并下载的过程,相关技术可以网上搜搜即可


不足之处:html源码转换为word,我发现对于样式不解析,style=""这样的是可以的,总之,对样式的控制比较困难

如果技术达标,可以采用word编辑格式,转换为xml,再转换为freemakre模块大概这么样一个思路

想法变革

POI实际上是用来生成word的,实现上我们根本就不需要在后台生成word,而仅仅需要一个输入流即可

也就是把html源码转换为一个输入流即可下载了,而且下载后置于是word还是excel,关键在于对下载后

文件的命名,你写成XXX.word那下载后就是word文档,你写成XXX.xls,那下载后就是xls



import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;

import org.apache.poi.poifs.filesystem.DirectoryEntry;
import org.apache.poi.poifs.filesystem.DocumentEntry;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;

/**
 * 文件下载类
 *
 */
public class DownLoadAction extends ActionSupport{
    /**
     * 下载文档,下载问价的访问入口方法
     *
     * @return resultName
     */
    public String execute() {
        // 返回 @Result 注解中的 name 值
        return "success";
    }

    
    /**
     * 获取下载读入流 必须要有方法
     * 在 @Result 注解的参数 params 中, key 为 "inputName" 所对应的 value "inputStream";
     * value "inputStream" ,决定该方法名为getInputStream;
     * 如果 value 的值为 hello ,则该方法名为 getHello;
     * @return InputStream
     */
    public InputStream getInputStream() {
        byte b[] = htmlSource.getBytes();  
        ByteArrayInputStream bais = new ByteArrayInputStream(b);
        String exportfileName = "wqw"+(System.currentTimeMillis())+".doc";
        try {
            setFileName(new String(exportfileName.getBytes(), "iso8859-1") );
        } catch (UnsupportedEncodingException e) {
           LOG.error("文件下载后名称编码转换异常");
           LOG.error("", e);
        }
        return bais;
    }

    /**
     * 下载文件的名称,用于动态获取下载文件的名称(可选,即下载的文件名可以写死在@Result的params参数中,但是没有人会这么傻。)
     * 在 @Result 注解的参数 params 中通过 ${fileName} 来获取该属性 fileName 的值;
     * 和EL表达式的取值方式类似,呵呵;
     */
    private String fileName;
    public String getHtmlSource() {
        return htmlSource;
    }


    public void setHtmlSource(String htmlSource) {
        this.htmlSource = htmlSource;
    }

    private String htmlSource;

    public String getFileName() {
        return fileName;
    }

    public void setFileName(String fileName) {
        this.fileName = fileName;
    }
    
    public static boolean writeWordFile() {

        boolean w = false;  

        String path = "d:/";  

        try {  

            if (!"".equals(path)) {  

                // 检查目录是否存在  

                File fileDir = new File(path);  

                if (fileDir.exists()) {  

                    // 生成临时文件名称  

                    String fileName = "a.doc";  

                    String content = "<html><div style=\"text-align: center\"><span style=\"font-size: 28px\"><span style=\"font-family: 黑体\">" +  

                        "制度发布通知<br /> <br /> </span></span></div></html>";  

                    byte b[] = content.getBytes();  

                    ByteArrayInputStream bais = new ByteArrayInputStream(b);  

                    POIFSFileSystem poifs = new POIFSFileSystem();  

                    DirectoryEntry directory = poifs.getRoot();  

                    DocumentEntry documentEntry = directory.createDocument("WordDocument", bais);  

                    FileOutputStream ostream = new FileOutputStream(path+ fileName);  

                    poifs.writeFilesystem(ostream);  

                    bais.close();  

                    ostream.close();  

                }  

            }  

        } catch (IOException e) {

            e.printStackTrace();  

      }  

      return w;  

    }
    
    public static boolean writeWordFile2(String htmlSource) {
        boolean w = false;  
        String path = "d:/";  
        try {  
            if (!"".equals(path)) {  
                // 检查目录是否存在  

                File fileDir = new File(path);  

                if (fileDir.exists()) {  

                    // 生成临时文件名称  

                    String fileName = "a.doc";  

                    byte b[] = htmlSource.getBytes();  

                    ByteArrayInputStream bais = new ByteArrayInputStream(b);  
                    
                    POIFSFileSystem poifs = new POIFSFileSystem();  

                    FileOutputStream ostream = new FileOutputStream(path+ fileName);  

                    poifs.writeFilesystem(ostream);  

                    bais.close();  

                    ostream.close();  

                }  

            }  

        } catch (IOException e) {

            e.printStackTrace();  

      }  

      return w;  

    }
    public static void main(String[] args) {
        if(writeWordFile()){
         System.out.println("执行成功");
        }else{
             System.out.println("执行失败");
        }
    }
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值