簡體   English   中英

Novacode Docx 合並多個單詞文檔

[英]Novacode Docx Merge multiple word Documents

我正在使用 Novacode Docx 用 c# 創建 word 文檔,但我在將兩個文檔合並為一個時遇到問題。根據我的要求,我需要下載兩個 word 文檔而不立即壓縮它們,但是我沒有找到解決方案因此,我計划合並 Word 文檔並將它們下載為單個文件。有人可以幫助我如何使用 Novacode Docx 合並 Word 文檔。

先感謝您。

我這樣做:

private DocX fullReportDocument;
private DocX titleDocument;
private DocX firstDocument;
private DocX secondDocument;

public byte[] GetReport(bool WantFirstReport, bool WantSecondReport)
{

    fullDocument = DocX.Create(new MemoryStream());
    // Create title for the report
    this.GenerateTitleDocument();
    // Insert the old document into the new document.
    fullDocument.InsertDocument(titleDocument);

    // Save the new document.
    fullDocument.Save();

    if (WantFirstReport)
    {
        // Create expertise report
        this.GenrateFirstDocument();

        // Insert a page break at the beginning to separate title and expertise report properly
        firstDocument.Paragraphs[0].InsertPageBreakBeforeSelf();
        firstDocument.Save();

        // Insert the old document into the new document.
        fullDocument.InsertDocument(firstDocument);

        // Save the new document.
        fullDocument.Save();
    }

    if (WantSecondReport)
    {
        // Create expertise report
        this.GenrateSecondDocument();

        // Insert a page break at the beginning to separate title and expertise report properly
        secondDocument.Paragraphs[0].InsertPageBreakBeforeSelf();
        secondDocument.Save();

        // Insert the old document into the new document.
        fullDocument.InsertDocument(secondDocument);

        // Save the new document.
        fullDocument.Save();
    }
    return fullReportDocument.DocumentMemoryStream.ToArray();
}

但是我修改了 DocX 庫以添加 DocumentMemoryStream 屬性,該屬性公開 memoryStream 內部 MemoryStream

如果您不想修改 DocX 庫,您也可以這樣做:

fullDocument.SaveAs(GENERATED_DOCX_LOCATION);
return System.IO.File.ReadAllBytes(GENERATED_DOCX_LOCATION);

GENERATED_DOCX_LOCATION 顯然是一個字符串,其中包含要保存的文件的物理路徑。

暫無
暫無

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

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