我想转换一个Html页面与所有的内容,即图像,如.png,.gif到ms word文档具有相同的外观和感觉

本文关键字:gif png 图像 ms word 外观 感觉 文档 转换 一个 Html | 更新日期: 2023-09-27 18:09:09

谁能帮我把一个html页面和它的内容转换成Microsoft Word文件?基本上我想要的是一个ms word页面,看起来像我的html页面在浏览器。

我正在使用这个代码,谁能给我一些建议吗?

object filename1 = @"html file path";
    object oMissing = System.Reflection.Missing.Value;
    object readOnly = false;
    object oFalse = false;
    Microsoft.Office.Interop.Word.Application oWord = new Microsoft.Office.Interop.Word.Application();
    Microsoft.Office.Interop.Word.Document oDoc = new Microsoft.Office.Interop.Word.Document();
    oDoc = oWord.Documents.Add(ref oMissing, ref oMissing, ref oMissing, ref oMissing);
    oWord.Visible = false;
    oDoc = oWord.Documents.Open(ref filename1, ref oMissing, ref readOnly, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
    if (!Directory.Exists(@"D:'FileConverter'Temp'new.doc"))//path of destination file.
    {
        Directory.CreateDirectory(@"D:'FileConverter'Temp");
    }
    if (!File.Exists(@"D:'FileConverter'Temp'new.doc"))
    {
        File.Create(@"D:'FileConverter'Temp'new.doc");
    }
    filename1 = @"D:'FileConverter'Temp'new.doc";
    object fileFormat = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatDocument;
    oDoc.SaveAs(ref filename1, ref fileFormat, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
    oDoc.Close(ref oFalse, ref oMissing, ref oMissing);
    oWord.Quit(ref oMissing, ref oMissing, ref oMissing);

我想转换一个Html页面与所有的内容,即图像,如.png,.gif到ms word文档具有相同的外观和感觉

这里是代码,任何人都可以使用转换html页面,并获得图像也..

 const string filename = "C:/Temp/test.docx";
        Response.ContentEncoding = System.Text.Encoding.UTF7;
        System.Text.StringBuilder SB = new System.Text.StringBuilder();
        System.IO.StringWriter SW = new System.IO.StringWriter();
        System.Web.UI.HtmlTextWriter htmlTW = new System.Web.UI.HtmlTextWriter(SW);
        tbl.RenderControl(htmlTW);
       string strBody = "<html>" +
        "<body>" + "<div><b>" + htmlTW.InnerWriter.ToString() + "</b></div>" +
          "</body>" +
         "</html>";
        string html = strBody;
        if (File.Exists(filename)) File.Delete(filename);
        using (MemoryStream generatedDocument = new MemoryStream())
        {
            using (WordprocessingDocument package = WordprocessingDocument.Create(generatedDocument, WordprocessingDocumentType.Document))
            {
                MainDocumentPart mainPart = package.MainDocumentPart;
                if (mainPart == null)
                {
                    mainPart = package.AddMainDocumentPart();
                    new Document(new Body()).Save(mainPart);
                }
                HtmlConverter converter = new HtmlConverter(mainPart);
                converter.BaseImageUrl = new Uri("http://localhost:portnumber/");
                Body body = mainPart.Document.Body;
                var paragraphs = converter.Parse(html);
                for (int i = 0; i < paragraphs.Count; i++)
                {
                    body.Append(paragraphs[i]);
                }
                mainPart.Document.Save();
            }
            File.WriteAllBytes(filename, generatedDocument.ToArray());
        }
        System.Diagnostics.Process.Start(filename);

获取.dll文件。你可以从这里下载notesfor.dll。

可以是名称HTMLtoOpenXML

不确定这是否适合您的问题域,但是…

你可以像往常一样呈现你的html,但将响应内容类型更改为application/msword,并确保文件名以.doc结尾。这将提示浏览器下载该文件,并提示操作系统将其作为word文档打开。根据我的经验,MS Word在将其他格式转换为Word文档方面做得不错。

你可能会在Word中得到一些看起来很烦人的弹出窗口,但如果这不是问题,这将是一个很好的物有所值的解决方案。