Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/290.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 用c语言将Html转换为Docx#_C#_Docx - Fatal编程技术网

C# 用c语言将Html转换为Docx#

C# 用c语言将Html转换为Docx#,c#,docx,C#,Docx,我想将html页面转换为c#中的docx,我该如何做?是一个商业组件,允许您实现这一点。MigraDoc可以提供帮助。 或者在办公室使用VS工具。 或者通过COM连接到Office。OpenXML SDK允许您以编程方式构建docx文档: 使用该代码转换 Microsoft.Office.Interop.Word.Application word = new Microsoft.Office.Interop.Word.Application(); Microsoft.Office.In

我想将html页面转换为c#中的docx,我该如何做?

是一个商业组件,允许您实现这一点。

MigraDoc可以提供帮助。 或者在办公室使用VS工具。
或者通过COM连接到Office。

OpenXML SDK允许您以编程方式构建docx文档:


使用该代码转换

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);

<>你可以考虑使用AltCink。看,除其他外

如果您不想依赖Word来转换HTML,可以尝试docx4j ImportXHTML for.NET;请参阅

,您可以考虑使用该提供程序将HTML文件和/或HTML字符串转换成WordML的非常方便的工具。 其中有很多选择:

  • 您可以使用CSS样式选择器筛选HTML的哪些块应该 被插入Word文档中
  • 您可以选择是下载图像还是将其作为外部链接
  • 它解析HTML表单
  • 您可以对覆盖原始CSS的表格和段落使用本机单词样式
  • 转换Word书签中的HTML锚
  • 等等

  • 我希望您会发现它很有用:-)

    下面的代码与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);
    
    您还可以以其他格式保存,如docx,如下所示:

    var savePathDocx = Server.MapPath("~/MyFiles/Html2PdfTest.docx");
    var wordDoc = word.Documents.Open(FileName: filePath, ReadOnly: false);
    wordDoc.SaveAs2(FileName: savePathDocx, FileFormat: WdSaveFormat.wdFormatXMLDocument);
    
    我的解决方案与()一起使用,为ASP.NET MVC提供了一个优雅的解决方案

    WordHelper.cs
    我正在使用生成此示例的HTML。

    Microsoft不建议在web服务器上使用office应用程序。 但是,使用OpenXML2.5可以相当容易地完成这项工作

    您真正需要做的就是将HTML拆分为(“”) 然后,对于每个部分,将其插入一个开关,并确定它是否是HTML标记

    然后,对于每个部分,您可以开始将HTML转换为“Run”和“RunProperties”,而非HTML文本只需放入“text”中

    听起来比现在更难。。。是的,我不知道为什么没有代码可以完全做到这一点

    要记住的事情。
    这两种格式不能很好地相互转换,因此如果您关注最干净的代码,您将遇到格式本身变得混乱的问题。

    还可以查看使用Aspose的示例。还可以查看.NET的Word。我在Aspose从html转换为docx时遇到了一些问题,比如对我来说非常基本的样式和图像格式问题,他们认为这是产品的局限性…同意。缺乏对css的支持,即使是嵌入的css,也意味着您必须自己格式化所有表格、段落甚至列表。记住调用
    wordDoc.Close()
    wordDoc.Quit()
    ,然后处理对象,否则,word的实例将在后台运行。请注意,在ASP.NET应用程序中使用
    Interop.word.Application
    是官方未移植的,Microsoft不建议这样做:这与C#无关,而与PHP无关。请回答OP的问题。
    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();
            }
        }
    }
    
        [HttpPost]
        [ValidateInput(false)]
        public FileResult Demo(CkEditorViewModel viewModel)
        {
            return File(WordHelper.HtmlToWord(viewModel.CkEditorContent),
              "application/vnd.openxmlformats-officedocument.wordprocessingml.document");
        }