【问题标题】:How to convert Multiple HTML Pages to Single Doc in c#如何在 C# 中将多个 HTML 页面转换为单个文档
【发布时间】:2018-02-12 06:50:40
【问题描述】:

我正在使用 spire doc 将单个 HTML 页面转换为 Doc。我需要将多个 html 页面从单个文件夹转换为单个 Doc。如何做到这一点。谁能提供一些想法或任何可用的库来实现这一目标?

请找到我将单个 HTML 转换为 Doc 的代码。

 Spire.Doc.Document document = new Spire.Doc.Document();
 document.LoadFromFile(@"D:\DocFilesConvert\htmlfile.html", Spire.Doc.FileFormat.Html, XHTMLValidationType.None);
 document.SaveToFile(@"D:\DocFilesConvert\docfiless.docx", Spire.Doc.FileFormat.Docx);

【问题讨论】:

    标签: c# html html-to-pdf spire.doc


    【解决方案1】:

    似乎没有直接的方法来实现这一点。我发现的一种解决方法是将每个 HTML 文档转换为单个 Word 文件,然后将这些 Word 文件合并到一个文件中。

    //get HTML file paths
    string[] htmlfilePaths = new string[]{
    
        @"F:\Documents\Html\1.html",
        @"F:\Documents\Html\2.html",
        @"F:\Documents\Html\3.html"
    };
    
    //create Document array
    Document[] docs = new Document[htmlfilePaths.Length];
    
    for (int i = 0; i < htmlfilePaths.Length; i++)
    {
        //load each HTML to a sperate Word file
        docs[i] = new Document(htmlfilePaths[i], FileFormat.Html);
    
        //combine these Word files in one file
        if (i>=1)
        {
            foreach (Section sec in docs[i].Sections)
            {
                docs[0].Sections.Add(sec.Clone());
            }                 
        }
    }
    
    //save to a Word document
    docs[0].SaveToFile("output.docx", FileFormat.Docx2013);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-02-15
      • 2021-02-21
      • 2015-05-18
      • 2013-06-08
      • 1970-01-01
      • 1970-01-01
      • 2021-03-13
      相关资源
      最近更新 更多