簡體   English   中英

如何在C#中將多個HTML頁面轉換為單個文檔

[英]How to convert Multiple HTML Pages to Single Doc in c#

我正在使用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);

似乎沒有直接的方法可以實現這一目標。 我發現一種解決方法是將每個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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM