DocumentFormat.OpenXml.dll通過word做好的模板生成word

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
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();
                   }
               }
           }
       }

  

posted @   evemen  阅读(281)  评论(0编辑  收藏  举报
(评论功能已被禁用)
点击右上角即可分享
微信分享提示