DocumentFormat.OpenXml.dll通過word做好的模板生成word
阿新 • • 發佈:2021-07-10
private void ExportToWord(string saveFolder, string saveName, List<ExportToWord01Dto> allSaveNoItems) { var savePath = Path.Combine(StaticFileManager.GetStaticFileTemplateRootFolder(), saveFolder); var inputFileName = "MB01";//"頁編號";//"MB01"; var ext = ".docx"; var templateDocName = inputFileName + ext; var templateDoc = Path.Combine(StaticFileManager.GetStaticFileTemplateRootFolder() + $"/{templateDocName}"); var tempDi = new DirectoryInfo(savePath); tempDi.Create(); File.Copy(templateDoc, Path.Combine(tempDi.FullName, saveName + ".docx")); if (allSaveNoItems == null || !allSaveNoItems.Any()) return; using (WordprocessingDocument doc = WordprocessingDocument.Open(Path.Combine(tempDi.FullName, saveName + ".docx"), true)) { Body body = doc.MainDocumentPart.Document.Body; OpenXmlElement tableClone = null; var tb = body.ChildElements.Where(e => e.XName.LocalName == "tbl").FirstOrDefault(); if (tb != null) { tableClone = tb.CloneNode(true); } tb.Remove(); var docT1 = allSaveNoItems.First().DocT1.ToStr(); var docT2 = allSaveNoItems.First().DocT2.ToStr(); //TextReplacer.SearchAndReplace(doc, "{$DocT1}", docT1, false); //TextReplacer.SearchAndReplace(doc, "{$DocT2}", docT2, false); foreach (var text in body.Descendants<Text>()) { if (text.Text.Contains("{$DocT1}")) { text.Text = text.Text.Replace("{$DocT1}", docT1); } else if (text.Text.Contains("{$DocT2}")) { text.Text = text.Text.Replace("{$DocT2}", docT2); } } foreach (var input in allSaveNoItems) { OpenXmlElement tn = null; var tbl = body.AppendChild(tableClone.CloneNode(true)); // var tbl = body.ChildElements.Where(e => e.XName.LocalName == "tbl").Last(); if (tbl != null) { foreach (var text in body.Descendants<Text>()) { if (text.Text.Contains("{$DocT3}")) { text.Text = text.Text.Replace("{$DocT3}", input.DocT3.ToStr()); } else if (text.Text.Contains(@"{$DocT4}")) { text.Text = text.Text.Replace(@"{$DocT4}", input.DocT4.ToStr()); } else if (text.Text.Contains("{$DocT5}")) { text.Text = text.Text.Replace("{$DocT5}", input.DocT5.ToStr()); } else if (text.Text.Contains("{$DocT6}")) { text.Text = text.Text.Replace("{$DocT6}", input.DocT6.ToStr()); } else if (text.Text.Contains("{$DocT7}")) { text.Text = text.Text.Replace("{$DocT7}", input.DocT7.ToStr()); } else if (text.Text.Contains("{$DocT8}")) { text.Text = text.Text.Replace("{$DocT8}", input.DocT8.ToStr()); } else if (text.Text.Contains("{$DocT49}")) { text.Text = text.Text.Replace("{$DocT9}", input.DocT9.ToStr()); } else if (text.Text.Contains("{$DocT10}")) { text.Text = text.Text.Replace("{$DocT10}", input.DocT10.ToStr()); } } //動態新增的元素,使用下方的無法替換標籤 //TextReplacer.SearchAndReplace(doc, "{$DocT3}", input.DocT3.IsNullOrEmpty() ? " " : input.DocT3, false); //TextReplacer.SearchAndReplace(doc, "{$DocT4}", input.DocT4.IsNullOrEmpty() ? " " : input.DocT4, false); //TextReplacer.SearchAndReplace(doc, "{$DocT5}", input.DocT5.IsNullOrEmpty() ? " " : input.DocT5, false); //TextReplacer.SearchAndReplace(doc, "{$DocT6}", input.DocT6.IsNullOrEmpty() ? " " : input.DocT6, false); //TextReplacer.SearchAndReplace(doc, "{$DocT7}", input.DocT7.IsNullOrEmpty() ? " " : input.DocT7, false); //TextReplacer.SearchAndReplace(doc, "{$DocT8}", input.DocT8.IsNullOrEmpty() ? " " : input.DocT8, false); //TextReplacer.SearchAndReplace(doc, "{$DocT9}", input.DocT9.IsNullOrEmpty() ? " " : input.DocT9, false); //TextReplacer.SearchAndReplace(doc, "{$DocT10}", input.DocT10.IsNullOrEmpty() ? " " : input.DocT10, false); // var tbl = body.ChildElements.Where(e => e.XName.LocalName == "tbl").FirstOrDefault(); // Create row to the table. var remarkTR = (TableRow)tbl.ChildElements.Last().CloneNode(true); tbl.ChildElements.Last().Remove(); var tr1 = (TableRow)tbl.ChildElements.Last().CloneNode(true); tbl.ChildElements.Last().Remove(); for (int i = 0; i < input.DocTR.Count; i++) { var newtr = tr1.CloneNode(true); var texts = newtr.Descendants<Text>(); texts.ElementAtOrDefault(0).Text = input.DocTR[i][0].IsNullOrEmpty() ? " " : input.DocTR[i][0]; texts.ElementAtOrDefault(1).Text = input.DocTR[i][1].IsNullOrEmpty() ? " " : input.DocTR[i][1]; texts.ElementAtOrDefault(2).Text = input.DocTR[i][2].IsNullOrEmpty() ? " " : input.DocTR[i][2]; texts.ElementAtOrDefault(3).Text = input.DocTR[i][3].IsNullOrEmpty() ? " " : input.DocTR[i][3]; texts.ElementAtOrDefault(4).Text = input.DocTR[i][4].IsNullOrEmpty() ? " " : input.DocTR[i][4]; tbl.AppendChild(newtr); } tbl.AppendChild(remarkTR); //加入一個段落p body.AppendChild(new Paragraph()); doc.MainDocumentPart.PutXDocument(); } } } }