OpenXML 方法

        private static void FillSimpleText(SdtRun simpleTextCC, string replacementText)

        {

            // remove the showing place holder element      

            SdtProperties ccProperties = simpleTextCC.SdtProperties;

            ccProperties.RemoveAllChildren<ShowingPlaceholder>();

 

            // fetch content block Run element            

            SdtContentRun contentRun = simpleTextCC.SdtContentRun;

            var ccRun = contentRun.GetFirstChild<Run>();

 

            // if there was no placeholder text in the content control, then the SdtContentRun

            // block will be empty -> ccRun will be null, so create a new instance

            if (ccRun == null)

            {

                ccRun = new Run(

                    new RunProperties() { RunStyle = null },

                    new Text());

                contentRun.Append(ccRun);

            }

 

            // remove revision identifier & replace text

            ccRun.RsidRunProperties = null;

            ccRun.GetFirstChild<Text>().Text = replacementText;

 

            // set the run style to that stored in the SdtProperties block, if there was

            // one. Otherwise the existing style will be used.            

            var props = ccProperties.GetFirstChild<RunProperties>();

            if (props != null)

                if (props != null)

                {

                    RunStyle runStyle = props.RunStyle;

                    if (runStyle != null)

                    {

                        // set the run style to the same as content block property style.

                        var runProps = ccRun.RunProperties;

                        runProps.RunStyle = new RunStyle() { Val = runStyle.Val };

                        runProps.RunFonts = null;

                    }

                }

        }

        /// <summary>

        /// 

        /// </summary>

        /// <param name="dotxPath"></param>

        /// <param name="contentControlTag"></param>

        /// <param name="replaceText"></param>

        public static void SetSimpleTextInFooter(string dotxPath, string contentControlTag, string replaceText)

        {

            using (WordprocessingDocument wordprocessingDocument = WordprocessingDocument.Open(dotxPath, true))

            {

                foreach (FooterPart footerPart in wordprocessingDocument.MainDocumentPart.FooterParts)

                {

                    var footerPartSdtRuns = footerPart.Footer.Descendants<SdtRun>()

                        .Where(run => run.SdtProperties.GetFirstChild<Tag>().Val.Value == contentControlTag);

 

                    foreach (SdtRun sdtRun in footerPartSdtRuns)

                    {

                        sdtRun.Descendants<Text>().First().Text = replaceText;

                    }

                }

 

                wordprocessingDocument.MainDocumentPart.Document.Save();

            }

        }

        /// <summary>

        /// 

        /// </summary>

        /// <param name="dotxPath"></param>

        /// <param name="contentControlTag"></param>

        /// <param name="replaceText"></param>

        public static void SetSimpleTextInHeader(string dotxPath, string contentControlTag, string replaceText)

        {

            using (WordprocessingDocument wordprocessingDocument = WordprocessingDocument.Open(dotxPath, true))

            {

                foreach (HeaderPart headerPart in wordprocessingDocument.MainDocumentPart.HeaderParts)

                {

                    var headerPartSdtRuns = headerPart.Header.Descendants<SdtRun>()

                        .Where(run => run.SdtProperties.GetFirstChild<Tag>().Val.Value == contentControlTag);

 

                    foreach (SdtRun sdtRun in headerPartSdtRuns)

                    {

                        sdtRun.Descendants<Text>().First().Text = replaceText;

                    }

                }

 

                wordprocessingDocument.MainDocumentPart.Document.Save();

            }

        }

        public static void SetSimpleTextInDocument(string dotxPath, string contentControlTag, string replaceText)

        {

            using (WordprocessingDocument wordProcessingDocument = WordprocessingDocument.Open(dotxPath, true))

            {

                MainDocumentPart mainDocumentPart = wordProcessingDocument.MainDocumentPart;

                IEnumerable<SdtRun> sdtRuns = mainDocumentPart.Document.Descendants<SdtRun>()

                  .Where(run => run.SdtProperties.GetFirstChild<Tag>().Val == contentControlTag);

                foreach (SdtRun item in sdtRuns)

                {

                    FillSimpleText(item, replaceText);

                }

            }

 

        }

        public static Table GetTableByControlTag(MainDocumentPart mainPart, string contentControlTag)

        {

            // This should return only one content control element: the one with 

            // the specified tag value.

            // If not, "Single()" throws an exception.

            SdtBlock ccWithTable = mainPart.Document.Body.Descendants<SdtBlock>().Where

                (r => r.SdtProperties.GetFirstChild<Tag>().Val == contentControlTag).Single();

 

            // This should return only one table.

            return ccWithTable.Descendants<Table>().Single();

 

        }

        /// <summary>

        /// 根据数据源DataTable动态生成DOC文档中的表行

        /// </summary>

        /// <param name="theTable">DOC文档中的表</param>

        /// <param name="dtSource">数据源DataTable</param>

        public static void GenerateRowFromDataTable(Table theTable, DataTable dtSource)

        {

            //Copy the last row format

            TableRow theRow = theTable.Elements<TableRow>().Last();

 

            foreach (DataRow dr in dtSource.Rows)

            {

                TableRow rowCopy = (TableRow)theRow.CloneNode(true);

                object[] objs = dr.ItemArray;

 

                IEnumerable<TableCell> tcs = rowCopy.Descendants<TableCell>();

                int i = 0;

                foreach (TableCell item in tcs)

                {

                    //IEnumerable<Paragraph> ps = item.Descendants<Paragraph>();

                    item.RemoveAllChildren<Paragraph>();

                    item.Append(new Paragraph(new Run(new Text(objs[i].ToString()))));

                }

 

                theTable.AppendChild(rowCopy);

            }

 

            // Remove the empty placeholder row from the table.

            theTable.RemoveChild(theRow);

        }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值