g
/// <summary>
/// 合并多個word文檔(合并到第一文件)
/// </summary>
/// <param name="as_word_paths">word文檔完整路徑</param>
/// <param name="breakNewPage">true(默認(rèn)值),合并下一個文檔前,自動換頁</param>
/// <returns>無</returns>
public void MergeWordFiles(string[] as_word_paths, bool breakNewPage = true)
{
var ls_first_word = as_word_paths.Length > 0 ? as_word_paths[0] : "";
if (ls_first_word.fn_isempty())
{
return;
}
using (WordprocessingDocument doc = WordprocessingDocument.Open(ls_first_word, true))
{
var mainPart = doc.MainDocumentPart;
for (var i = 1; i < as_word_paths.Length; i++)
{
var altChunkId = "cid_" + Guid.NewGuid().ToString().Replace("-", "");
var chunk = mainPart.AddAlternativeFormatImportPart(AlternativeFormatImportPartType.WordprocessingML, altChunkId);
//mainPart.Document.Save();
using (FileStream fileStream = File.Open(as_word_paths[i], FileMode.Open))
{
chunk.FeedData(fileStream);
}
var altChunk = new DocumentFormat.OpenXml.Wordprocessing.AltChunk();
altChunk.Id = altChunkId;
//添加下一頁(下一個文檔合并此頁)
if (breakNewPage)
{
Paragraph newPage = new Paragraph(new Run
(new Break() { Type = BreakValues.Page }
));
mainPart.Document.Append(newPage, altChunk);
}
else
{
mainPart.Document.Append(altChunk);
}
//mainPart.Document.Body.Append(altChunk);
}
//mainPart.Document.Body.InsertAfter(altChunk, mainPart.Document.Body.Elements<DocumentFormat.OpenXml.Wordprocessing.Paragraph>().Last());
mainPart.Document.Save();
}
}
文章來源:http://www.zghlxwxcb.cn/news/detail-761673.html
詳情了解...?文章來源地址http://www.zghlxwxcb.cn/news/detail-761673.html
到了這里,關(guān)于C#合并多個Word文檔(微軟官方免費(fèi)openxml接口)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!