在c#中将Html转换为Docx

Lui*_*uis 14 c# docx

我想在c#中将html页面转换为docx,我该怎么做?

Pos*_*ing 11

下面的内容与Luis代码相同,但只是更具可读性并应用于ASP.NET MVC应用程序:

var word = new Microsoft.Office.Interop.Word.Application();
word.Visible = false;

var filePath = Server.MapPath("~/MyFiles/Html2PdfTest.html");
var savePathPdf = Server.MapPath("~/MyFiles/Html2PdfTest.pdf");
var wordDoc = word.Documents.Open(FileName: filePath, ReadOnly: false);
wordDoc.SaveAs2(FileName: savePathPdf, FileFormat: WdSaveFormat.wdFormatPDF);
Run Code Online (Sandbox Code Playgroud)

你也可以保存其他格式,如docx,如下所示:

var savePathDocx = Server.MapPath("~/MyFiles/Html2PdfTest.docx");
var wordDoc = word.Documents.Open(FileName: filePath, ReadOnly: false);
wordDoc.SaveAs2(FileName: savePathDocx, FileFormat: WdSaveFormat.wdFormatXMLDocument);
Run Code Online (Sandbox Code Playgroud)

  • 请记住随后调用 `wordDoc.Close()` 和 `wordDoc.Quit()` 来处理该对象,否则您将留下在后台运行的 word 实例。 (2认同)
  • 请注意,在ASP.NET应用程序中使用`Interop.Word.Application`正式**不受支持**和**不推荐**由Microsoft:/sf/answers/609647881/ (2认同)

Leo*_*lva 7

我的解决方案使用Html2OpenXmlDocumentFormat.OpenXml(Html2OpenXml的NuGet包在这里)为ASP.NET MVC提供了一个优雅的解决方案.

WordHelper.cs

public static class WordHelper
{
    public static byte[] HtmlToWord(String html)
    {
        const string filename = "test.docx";
        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);
                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();
            }

            return generatedDocument.ToArray();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

调节器

    [HttpPost]
    [ValidateInput(false)]
    public FileResult Demo(CkEditorViewModel viewModel)
    {
        return File(WordHelper.HtmlToWord(viewModel.CkEditorContent),
          "application/vnd.openxmlformats-officedocument.wordprocessingml.document");
    }
Run Code Online (Sandbox Code Playgroud)

我正在使用CKEditor为此示例生成HTML.


Lui*_*uis 5

使用该代码进行转换

Microsoft.Office.Interop.Word.Application word = 
    new Microsoft.Office.Interop.Word.Application();
Microsoft.Office.Interop.Word.Document wordDoc = 
    new Microsoft.Office.Interop.Word.Document();
Object oMissing = System.Reflection.Missing.Value;
wordDoc = word.Documents.Add(ref oMissing, ref oMissing, ref oMissing, ref oMissing);
word.Visible = false;
Object filepath = "c:\\page.html";
Object confirmconversion = System.Reflection.Missing.Value;
Object readOnly = false;
Object saveto = "c:\\doc.pdf";
Object oallowsubstitution = System.Reflection.Missing.Value;

wordDoc = word.Documents.Open(ref filepath, ref confirmconversion, 
    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);
 object fileFormat = WdSaveFormat.wdFormatPDF;
 wordDoc.SaveAs(ref saveto, 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 oallowsubstitution, ref oMissing,
     ref oMissing);
Run Code Online (Sandbox Code Playgroud)