public TableRow Replace(TableRow tableRow, Dictionary <string, string> ReplaceItems)
        {
            TableRow targetTableRow = (TableRow)tableRow.Clone();

            foreach (KeyValuePair <string, string> keyValuePair in ReplaceItems)
            {
                string SearchString  = keyValuePair.Key;
                string ReplaceString = keyValuePair.Value.Replace("\r\n", "\n");  // 解決換行問題

                #region 字串替代
                foreach (Paragraph para in targetTableRow.Descendants <Paragraph>())
                {
                    if (para.InnerText.Contains(SearchString))
                    {
                        Run newRun = (Run)para.Descendants <Run>().First(r => r.InnerText.Contains("#")).Clone();

                        newRun.Descendants <Text>().First().Text = para.InnerText.Replace(SearchString, ReplaceString);

                        // 處理換行
                        newRun.InnerXml = newRun.InnerXml.Replace("\r\n", "</w:t><w:br/><w:t>");

                        // 處理\t轉成Tab
                        newRun.InnerXml = newRun.InnerXml.Replace("\t", "   ");

                        para.RemoveAllChildren <Run>();
                        para.AppendChild <Run>(newRun);
                    }
                }
                #endregion
            }

            return(targetTableRow);
        }
        public void InsertTableRow(Table table, TableRow tableRow, int rowIndex, int count)
        {
            for (int i = 0; i <= count; i++)
            {
                var rowToInsert = (TableRow)tableRow.Clone();

                //add new row to table, after last row in table
                InsertTableRow(table, rowToInsert, rowIndex);
            }
        }
        public byte[] Replace <T>(byte[] bytes, List <T> ReplaceModelList) where T : class
        {
            // 處理Model List 資訊
            PropertyInfo[] infos = ReplaceModelList.First().GetType().GetProperties();

            List <string> ReplaceTags = new List <string>();

            foreach (PropertyInfo info in infos)
            {
                ReplaceTags.Add("#" + info.Name + "#");
            }

            MemoryStream destination = ByteArrayToMemoryStream(bytes);

            using (var wordDoc = WordprocessingDocument.Open(destination, true))
            {
                TableRow TargetRow = wordDoc.MainDocumentPart.Document.Body.Descendants <TableRow>().FirstOrDefault((target) => ReplaceTags.All(target.InnerText.Contains));

                // 如果沒找到則回傳原先資料
                if (TargetRow == null)
                {
                    return(bytes);
                }

                TableRow CopyRow = (TableRow)TargetRow.Clone();

                var Inserter = new InsertWordTemplate();
                foreach (T ReplaceItem in ReplaceModelList)
                {
                    Inserter.InsertTableRow(TargetRow, Replace(CopyRow, ReplaceItem));
                }
                TargetRow.Remove();

                wordDoc.Save();
            }

            destination.Position = 0;

            return(destination.ToArray());
        }