• Html转Word文档,解决无法保存网络图片的问题


      最近项目中需要这个功能,网上有很多word转html的方法,但是html转word的方法很少,因为html中的图片转换到本地比较麻烦;

      开始的时候只能转换不带图片的html内容,但是不符合要求,将html页面中的图片改成绝对路径后在断网之后无法查看,将图片下载下来改成绝对路径后,换台机器无法观看,问题干扰了一天;

      当然有一种实现方式是将外链样式和外链图片全部一个个请求下来再放到word中排版,这个貌似非常麻烦,跟做一个浏览器一样。

      后来发现,在网站中直接复制网页然后到word文档中粘贴,可以把图片和样式全部拿过来,于是想到一种方法是否可以利用剪切板来取数据,模拟复制粘贴,最终发现可行,唯一的不足是由于宽度原因,拿来的东西在word中呈现会把格局变掉。

      代码还是非常简单,比较好理解的,下面上代码:

      

     1      public void HtmlToWordByUrl(string url)
     2         {
     3             WebBrowser WB = new WebBrowser();//新建内置浏览
     4             WB.Navigate(url);//加载页面
     5             //加载完成
     6             while (WB.ReadyState != WebBrowserReadyState.Complete)
     7             {
     8                 System.Windows.Forms.Application.DoEvents();
     9             }
    10             //对加载完成的页面进行全选和复制操作
    11             HtmlDocument doc = WB.Document;
    12             doc.ExecCommand("SelectAll", false, "");//全选
    13             doc.ExecCommand("Copy", false, "");//复制
    14             //放入剪切板
    15             IDataObject iData = Clipboard.GetDataObject();
    16             SaveWord();//保存为word文档
    17             //读取文档,下载文档
    18             FileStream fs = new FileStream(Server.MapPath("~/UploadFile/test.doc"), FileMode.Open);
    19             byte[] bytes = new byte[(int)fs.Length];
    20             fs.Read(bytes, 0, bytes.Length);
    21             fs.Close();
    22             Response.ContentType = "application/octet-stream";
    23             //通知浏览器下载文件而不是打开 
    24             Response.AddHeader("Content-Disposition", "attachment; filename=htmlfile.doc");
    25             Response.BinaryWrite(bytes);
    26             WB.Dispose();
    27             Response.Flush();
    28             Response.End();
    29 
    30         }
    31 
    32         public void SaveWord()
    33         {
    34             object path;                      //声明文件路径变量
    35             //string wordstr = wdstr;                   //声明word文档内容
    36             MSWord.Application wordApp;       //声明word应用程序变量
    37             MSWord.Document worddoc;          //声明word文档变量    
    38 
    39             //初始化变量
    40             object Nothing = Missing.Value;                       //COM调用时用于占位
    41             object format = MSWord.WdSaveFormat.wdFormatDocument; //Word文档的保存格式
    42             wordApp = new MSWord.ApplicationClass();              //声明一个wordAPP对象
    43             worddoc = wordApp.Documents.Add(ref Nothing, ref Nothing,
    44                 ref Nothing, ref Nothing);
    45 
    46             //页面设置
    47             worddoc.PageSetup.PaperSize = Microsoft.Office.Interop.Word.WdPaperSize.wdPaperA4;//设置纸张样式
    48             worddoc.PageSetup.Orientation = Microsoft.Office.Interop.Word.WdOrientation.wdOrientPortrait;//排列方式为垂直方向
    49 
    50 
    51             //向文档中写入内容(直接粘贴)
    52             worddoc.Paragraphs.Last.Range.Paste();
    53 
    54             //保存文档
    55             path = Server.MapPath("~/UploadFile/test.doc");          //设置文件保存路劲
    56             worddoc.SaveAs(ref path, ref format, ref Nothing, ref Nothing,
    57                 ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing,
    58                 ref Nothing, ref Nothing, ref Nothing, ref Nothing);
    59 
    60             //关闭文档
    61             worddoc.Close(ref Nothing, ref Nothing, ref Nothing);  //关闭worddoc文档对象
    62             wordApp.Quit(ref Nothing, ref Nothing, ref Nothing);   //关闭wordApp组对象
    63 
    64         }

    其中要注意的一点是,因为在webform页面调用webbrowser,需要引入以下引用

     1 using System.Windows.Forms; 

    前端页面引用,需要加入AspCompat="true"

    1 <%@ Page Language="C#" AutoEventWireup="true" AspCompat="true" CodeBehind="HtmlToWord.aspx.cs" Inherits="NurseManage.Export.HtmlToWord" %>

    最后引用了微软的操作类库

     1 using MSWord = Microsoft.Office.Interop.Word; 

    方法引用:

    1      protected void Page_Load(object sender, EventArgs e)
    2         {
    3             HtmlToWordByUrl("http://www.cnblogs.com/Kuleft/p/5010636.html");
    4 
    5         }

    效果图:

    关于word内容排版的问题希望大家能不吝赐教,对于word的操作确实不太清楚。

    还有一个就是不知道是不是百度首页(http://www.baidu.com)防盗爬,暂时转化不了。

  • 相关阅读:
    加密解密
    论事件驱动与异步IO
    linux 基础命令
    libgcc_s_dw2-1.dll 缺失问题解决
    TightVNC 远程桌面
    配置机器学习开发环境(eclipse + anaconda2)
    Caffe 执行python实例并可视化
    Caffe windows编译找不到python27_d.lib问题解决
    PHP 上传文件名中带中文的文件失败问题
    Windows 搭建PHP开发环境
  • 原文地址:https://www.cnblogs.com/Kuleft/p/5010636.html
Copyright © 2020-2023  润新知