2017-09-28 158 views
1

我使用Novacode Docx创建Word文档与C#但我有合并两个文件到一个问题。根据我的要求,我需要下载两个Word文档,而不是一次压缩它们,然而,我没有找到解决方案,所以我已经计划合并W​​ord文档并将它们作为单个文件下载。可以帮助我如何使用Novacode Docx合并Word文档。Novacode Docx合并多个单词文档

预先感谢您。

回答

1

我不喜欢这样写道:

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 librairy添加如果你不想修改DOCX库,你也可以做一个公开的MemoryStream内部的MemoryStream

DocumentMemoryStream财产这样的:

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

GENERATED_DOCX_LOCATION显然与要保存文件的physcal路径的字符串。

相关问题