唐伯虎 发表于 2021-6-25 09:12:07

PageOffice 使用Dome

一、前言PageOffice是一款帮助Web应用系统或Web网站实现用户在线编辑Word、Excel、PowerPoint文档,Word/Excel模板动态填充,Word/Excel在线输入提交,系统数据导入导出word、excel文档的Office快速开发组件库,是目前把Office应用到Web平台上的最全面、最先进的解决方案。PageOffice为软件开发者构建了一套简洁高效、统一的Office对象接口,同时无缝支持doc、docx、xls、xlsx、ppt、pptx等流行Office文件格式。简而言之就是可以在线编写office文件的产品。二、安装PageOffice专业版试用序列号: 2C697-4FC8-F274-3DD88
[*]双击运行Setup.exe安装服务器组件。(可以到官网下载安装程序)
[*]如果运行示例,用VS.Net打开MvcApplication4.sln即可运行。   
  3.  如果新建网站或集成PageOffice到您现有的网站里:  
  1). 双击运行Setup.exe安装服务器组件;  
  2). 拷贝“集成文件”目录下的“pageoffice”文件夹到您自己网站的根目录下;  
  2). VS.NET工具箱拖放PageOffice控件,双击控件,在事件代码中编写代码。
------我的项目是.net MVC4  直接把 “pageoffice”文件夹放到自己网站的根目录下;  三、使用使用就分为编辑一个指定路径下的文件,并保存1、在网中打开一个指定路径下的word我的文件的路径是:D:\project\A27\A27_Source\Web\OfficeTemp\总结报告.docx如何打开该路径下的文件,直接上代码吧 public ActionResult EditReport()

      {
                     ViewBag.Message </span>= <span style="color:rgba(128,0,0,1);">"</span><span style="color:rgba(128,0,0,1);">Your contact page.</span><span style="color:rgba(128,0,0,1);">"</span><span style="color:rgba(0,0,0,1);">;
            System.Web.UI.Page page </span>= <span style="color:rgba(0,0,255,1);">new</span><span style="color:rgba(0,0,0,1);"> System.Web.UI.Page();
            </span><span style="color:rgba(0,0,255,1);">string</span> controlOutput = <span style="color:rgba(0,0,255,1);">string</span><span style="color:rgba(0,0,0,1);">.Empty;
            PageOffice.PageOfficeCtrl pc </span>= <span style="color:rgba(0,0,255,1);">new</span><span style="color:rgba(0,0,0,1);"> PageOffice.PageOfficeCtrl();
            </span><span style="color:rgba(0,0,255,1);">try</span><span style="color:rgba(0,0,0,1);">
            {
                </span><span style="color:rgba(0,0,255,1);">string</span> fileName = <span style="color:rgba(128,0,0,1);">"</span><span style="color:rgba(128,0,0,1);">总结报告.docx</span><span style="color:rgba(128,0,0,1);">"</span><span style="color:rgba(0,0,0,1);">;
                </span><span style="color:rgba(0,0,255,1);">string</span> filePath = Server.MapPath(<span style="color:rgba(128,0,0,1);">"</span><span style="color:rgba(128,0,0,1);">~/OfficeTemp/</span><span style="color:rgba(128,0,0,1);">"</span>)+<span style="color:rgba(0,0,0,1);">fileName;
                </span><span style="color:rgba(0,0,255,1);">string</span> currfilepath =<span style="color:rgba(128,0,0,1);">"</span><span style="color:rgba(128,0,0,1);">/</span><span style="color:rgba(128,0,0,1);">"</span> +<span style="color:rgba(0,0,0,1);"> fileName;
                pc.SaveFilePage </span>= Url.Content(<span style="color:rgba(128,0,0,1);">"</span><span style="color:rgba(128,0,0,1);">SaveDoc</span><span style="color:rgba(128,0,0,1);">"</span>) + <span style="color:rgba(128,0,0,1);">"</span><span style="color:rgba(128,0,0,1);">?path=</span><span style="color:rgba(128,0,0,1);">"</span> +<span style="color:rgba(0,0,0,1);"> Server.UrlEncode(currfilepath);
                </span><span style="color:rgba(0,0,255,1);">if</span> (filePath == <span style="color:rgba(0,0,255,1);">null</span>) <span style="color:rgba(0,0,255,1);">throw</span> <span style="color:rgba(0,0,255,1);">new</span> ApplicationException(<span style="color:rgba(128,0,0,1);">"</span><span style="color:rgba(128,0,0,1);">配置文件中未找到对应系统的项</span><span style="color:rgba(128,0,0,1);">"</span><span style="color:rgba(0,0,0,1);">);
                pc.ServerPage </span>= Url.Content(<span style="color:rgba(128,0,0,1);">"</span><span style="color:rgba(128,0,0,1);">~/pageoffice/server.aspx</span><span style="color:rgba(128,0,0,1);">"</span><span style="color:rgba(0,0,0,1);">);
                pc.WebOpen(filePath, PageOffice.OpenModeType.docAdmin, </span><span style="color:rgba(128,0,0,1);">"</span><span style="color:rgba(128,0,0,1);">s</span><span style="color:rgba(128,0,0,1);">"</span><span style="color:rgba(0,0,0,1);">);
                result.IsSuccess </span>= <span style="color:rgba(0,0,255,1);">true</span><span style="color:rgba(0,0,0,1);">;
                page.Controls.Add(pc);
                StringBuilder sb </span>= <span style="color:rgba(0,0,255,1);">new</span><span style="color:rgba(0,0,0,1);"> StringBuilder();
                </span><span style="color:rgba(0,0,255,1);">using</span> (StringWriter sw = <span style="color:rgba(0,0,255,1);">new</span><span style="color:rgba(0,0,0,1);"> StringWriter(sb))
                {
                  </span><span style="color:rgba(0,0,255,1);">using</span> (HtmlTextWriter htw = <span style="color:rgba(0,0,255,1);">new</span><span style="color:rgba(0,0,0,1);"> HtmlTextWriter(sw))
                  {
                        Server.Execute(page, htw, </span><span style="color:rgba(0,0,255,1);">false</span>); controlOutput =<span style="color:rgba(0,0,0,1);"> sb.ToString();
                  }
                }
                ViewBag.EditorHtml </span>=<span style="color:rgba(0,0,0,1);"> controlOutput;
            }
            </span><span style="color:rgba(0,0,255,1);">catch</span><span style="color:rgba(0,0,0,1);"> (Exception err)
            {
                </span><span style="color:rgba(0,0,255,1);">throw</span><span style="color:rgba(0,0,0,1);"> err;
            }
            </span><span style="color:rgba(0,0,255,1);">return</span><span style="color:rgba(0,0,0,1);"> PartialView();
      }</span></pre>2、保存编辑后的word, 保存时调用的saveDoc方法和参数正是编辑时这段代码提供的:pc.SaveFilePage = Url.Content("SaveDoc") + "?path=" + Server.UrlEncode(currfilepath);public ActionResult SaveDoc(string path)

      {
            </span><span style="color:rgba(0,0,255,1);">string</span> filePath = Server.MapPath(<span style="color:rgba(128,0,0,1);">"</span><span style="color:rgba(128,0,0,1);">~/OfficeTemp/</span><span style="color:rgba(128,0,0,1);">"</span> +<span style="color:rgba(0,0,0,1);"> path);
            PageOffice.FileSaver fs </span>= <span style="color:rgba(0,0,255,1);">new</span><span style="color:rgba(0,0,0,1);"> PageOffice.FileSaver();
            fs.SaveToFile(filePath);
            fs.Close();
            </span><span style="color:rgba(0,0,255,1);">return</span><span style="color:rgba(0,0,0,1);"> View();
      }</span></pre>四、根据数据库动态生成word文件生成word并不是pageoffice的功能,但是会一起与该组件一起使用1、生成word /// <summary>

      <span style="color:rgba(128,128,128,1);">///</span><span style="color:rgba(0,128,0,1);"> 报告word
      </span><span style="color:rgba(128,128,128,1);">///</span> <span style="color:rgba(128,128,128,1);">&lt;/summary&gt;</span>
      <span style="color:rgba(128,128,128,1);">///</span> <span style="color:rgba(128,128,128,1);">&lt;param name="wellboreId"&gt;&lt;/param&gt;</span>
      <span style="color:rgba(128,128,128,1);">///</span> <span style="color:rgba(128,128,128,1);">&lt;returns&gt;&lt;/returns&gt;</span>
      <span style="color:rgba(0,0,255,1);">public</span> <span style="color:rgba(0,0,255,1);">string</span> OutPutDoc(Guid wellboreId, <span style="color:rgba(0,0,255,1);">string</span> appendixTypes, <span style="color:rgba(0,0,255,1);">string</span><span style="color:rgba(0,0,0,1);"> wellboreNo)
      {
            </span><span style="color:rgba(0,0,255,1);">try</span><span style="color:rgba(0,0,0,1);">
            {
            string filePathNew = Server.MapPath("~/OfficeTemp/") + wellbore_No + "/" + HttpContext.User.Identity.Name + "/";//Server.MapPath("~/Upload/LogReportTemp/") +"\\" + HttpContext.User.Identity.Name;
                </span><span style="color:rgba(0,128,0,1);">//</span><span style="color:rgba(0,128,0,1);">生成文件夹</span>
               Directory.CreateDirectory(filePathNew);
                </span><span style="color:rgba(0,0,255,1);">string</span> fullfileName = filePathNew+<span style="color:rgba(0,0,0,1);"> fileName;
                </span><span style="color:rgba(0,0,255,1);">string</span> mainDocPath = Server.MapPath(<span style="color:rgba(128,0,0,1);">"</span><span style="color:rgba(128,0,0,1);">~/Upload/LogReportTemp/GeoSummaryReport_template.docx</span><span style="color:rgba(128,0,0,1);">"</span>);<span style="color:rgba(0,128,0,1);">//</span><span style="color:rgba(0,128,0,1);">Server.MapPath("~/Upload/LogReportTemp/LS25-1-5测井作业总结报告01.docx               using (var mainDoc =WordprocessingDocument.Open(mainDocPath, false))</span>
                <span style="color:rgba(0,0,255,1);">using</span> (<span style="color:rgba(0,0,255,1);">var</span> resultDoc =<span style="color:rgba(0,0,0,1);"> WordprocessingDocument.Create(fullfileName, WordprocessingDocumentType.Document))
                {
                                    MainDocumentPart mainPart =<span style="color:rgba(0,0,0,1);"> resultDoc.MainDocumentPart;
                  </span><span style="color:rgba(0,0,255,1);">foreach</span> (<span style="color:rgba(0,0,255,1);">var</span> part <span style="color:rgba(0,0,255,1);">in</span><span style="color:rgba(0,0,0,1);"> mainDoc.Parts)
                  {
                        </span><span style="color:rgba(0,0,255,1);">if</span> (part.OpenXmlPart.ContentType != <span style="color:rgba(128,0,0,1);">"</span><span style="color:rgba(128,0,0,1);">application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml</span><span style="color:rgba(128,0,0,1);">"</span><span style="color:rgba(0,0,0,1);">)
                            resultDoc.AddPart(part.OpenXmlPart, part.RelationshipId);
                        </span><span style="color:rgba(0,0,255,1);">else</span> <span style="color:rgba(0,0,255,1);">if</span> (mainPart == <span style="color:rgba(0,0,255,1);">null</span><span style="color:rgba(0,0,0,1);">)
                        {
                            mainPart </span>=<span style="color:rgba(0,0,0,1);"> (MainDocumentPart)resultDoc.AddPart(part.OpenXmlPart, part.RelationshipId);
                            </span><span style="color:rgba(0,128,0,1);">//</span><span style="color:rgba(0,128,0,1);">通过替换OuterXml中的占位符替换文本</span>
                            mainPart.Document.Body = <span style="color:rgba(0,0,255,1);">new</span><span style="color:rgba(0,0,0,1);"> Body(GeoSummaryReportHelper.XmlStringReplace(wellboreId, wellbore_No, mainDoc.MainDocumentPart.Document.Body.OuterXml, appendixTypes));
                            </span><span style="color:rgba(0,0,255,1);">var</span> bookMarks =<span style="color:rgba(0,0,0,1);"> GeoSummaryReportHelper.FindBookmarks(resultDoc.MainDocumentPart.Document);
                            </span><span style="color:rgba(0,128,0,1);">//</span><span style="color:rgba(0,128,0,1);">替换书签中的内容</span>
                            <span style="color:rgba(0,0,255,1);">foreach</span> (<span style="color:rgba(0,0,255,1);">var</span> end <span style="color:rgba(0,0,255,1);">in</span><span style="color:rgba(0,0,0,1);"> bookMarks)
                            {
                              </span><span style="color:rgba(0,128,0,1);">//</span><span style="color:rgba(0,128,0,1);">为了满足甲方格式要求,使用模板生成方式</span>
                              <span style="color:rgba(0,0,255,1);">if</span> (end.Key == <span style="color:rgba(128,0,0,1);">"</span><span style="color:rgba(128,0,0,1);">SuizuanTypeMark</span><span style="color:rgba(128,0,0,1);">"</span>) GeoSummaryReportHelper.CreateSuiZuanTable(end,resultDoc ,wellbore_No, <span style="color:rgba(128,0,0,1);">"</span><span style="color:rgba(128,0,0,1);">LWD</span><span style="color:rgba(128,0,0,1);">"</span><span style="color:rgba(0,0,0,1);">,wellboreId);
                              </span><span style="color:rgba(0,0,255,1);">if</span> (end.Key == <span style="color:rgba(128,0,0,1);">"</span><span style="color:rgba(128,0,0,1);">DianLanTypeMark</span><span style="color:rgba(128,0,0,1);">"</span>) GeoSummaryReportHelper.CreateDianLanTable(end, resultDoc, wellbore_No, <span style="color:rgba(128,0,0,1);">"</span><span style="color:rgba(128,0,0,1);">RUN</span><span style="color:rgba(128,0,0,1);">"</span><span style="color:rgba(0,0,0,1);">, wellboreId);
                              
                              </span><span style="color:rgba(0,0,255,1);">if</span> (end.Key.Contains(<span style="color:rgba(128,0,0,1);">"</span><span style="color:rgba(128,0,0,1);">DrillAndCasing</span><span style="color:rgba(128,0,0,1);">"</span>)) GeoSummaryReportHelper.DrawingDrillAndCasingInfoTb(end, wellboreId);<span style="color:rgba(0,128,0,1);">//</span><span style="color:rgba(0,128,0,1);">绘制基本信息井身结构模块表格</span>
                            }
                        }
                  }
                  </span><span style="color:rgba(0,0,255,1);">string</span> headerText = <span style="color:rgba(0,0,255,1);">string</span>.Format(<span style="color:rgba(128,0,0,1);">"</span><span style="color:rgba(128,0,0,1);">{0}总结报告</span><span style="color:rgba(128,0,0,1);">"</span><span style="color:rgba(0,0,0,1);">, wellbore_No);
                  GeoSummaryReportHelper.AddHeader(resultDoc, headerText);</span><span style="color:rgba(0,128,0,1);">//</span><span style="color:rgba(0,128,0,1);">添加页眉</span>                }
                </span><span style="color:rgba(0,0,255,1);">string</span> url = Request.Url.ToString().Replace(Request.Url.AbsolutePath,<span style="color:rgba(128,0,0,1);">""</span><span style="color:rgba(0,0,0,1);">);
                </span><span style="color:rgba(0,128,0,1);">//</span><span style="color:rgba(0,128,0,1);">返回生成的文档的信息</span>
                <span style="color:rgba(0,0,255,1);">string</span> ahref = url + <span style="color:rgba(128,0,0,1);">"</span><span style="color:rgba(128,0,0,1);">/OfficeTemp/</span><span style="color:rgba(128,0,0,1);">"</span> + wellbore_No + <span style="color:rgba(128,0,0,1);">"</span><span style="color:rgba(128,0,0,1);">/</span><span style="color:rgba(128,0,0,1);">"</span> + HttpContext.User.Identity.Name + <span style="color:rgba(128,0,0,1);">"</span><span style="color:rgba(128,0,0,1);">/</span><span style="color:rgba(128,0,0,1);">"</span> + fileName;<span style="color:rgba(0,128,0,1);">//</span><span style="color:rgba(0,128,0,1);">Server.MapPath("~/OfficeTemp/") + wellboreId + "/" + HttpContext.User.Identity.Name + "/" + fileName; </span><span style="color:rgba(0,128,0,1);">//</span><span style="color:rgba(0,128,0,1);">Url.Content(Server.MapPath("~/OfficeTemp/")) + wellboreId + "/" + HttpContext.User.Identity.Name + "/" + fileName; </span>
                <span style="color:rgba(0,0,255,1);">string</span> createTime = DateTime.Now.ToString(<span style="color:rgba(128,0,0,1);">"</span><span style="color:rgba(128,0,0,1);">yyyy-MM-dd HH:mm</span><span style="color:rgba(128,0,0,1);">"</span><span style="color:rgba(0,0,0,1);">);
                </span><span style="color:rgba(0,0,255,1);">string</span> returnValues = <span style="color:rgba(0,0,255,1);">string</span><span style="color:rgba(0,0,0,1);">.Format(
                   </span><span style="color:rgba(128,0,0,1);">@"</span><span style="color:rgba(128,0,0,1);">&lt;a href='{0}'&gt;{1}&lt;/a&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;
                  生成时间:{2} &amp;nbsp;&amp;nbsp&amp;nbsp;&amp;nbsp;&lt;a href=javascript:EditReport('{1}')&gt;【编辑】&lt;/a&gt;
                  &amp;nbsp;&amp;nbsp&amp;nbsp;&amp;nbsp;&lt;a href='javascript:BuildReport()'&gt;【重新生成】&lt;/a&gt;
                  &amp;nbsp;&amp;nbsp;&lt;a href='javascript:ExpToZip()'&gt;【打包下载】&lt;/a&gt;</span><span style="color:rgba(128,0,0,1);">"</span><span style="color:rgba(0,0,0,1);">, ahref, fileName, createTime);
                </span><span style="color:rgba(0,0,255,1);">return</span><span style="color:rgba(0,0,0,1);"> returnValues;//                  string.Format(//                   @"<a href='{0}'>{1}</a>&nbsp;&nbsp;&nbsp;//                  生成时间:{2} &nbsp;&nbsp&nbsp;&nbsp;<a href=javascript:EditReport('{1}')>【编辑】</a>//                  &nbsp;&nbsp&nbsp;&nbsp;<a href='javascript:BuildReport()'>【重新生成】</a>//                  &nbsp;&nbsp;<a href='javascript:ExpToZip()'>【打包下载】</a> //                  &nbsp;&nbsp;<a href=javascript:DeleteWellboresById('{3}')>【删除】</a>", ahref, fileName, createTime, wellboreId);
            }
            </span><span style="color:rgba(0,0,255,1);">catch</span><span style="color:rgba(0,0,0,1);"> (Exception ex)
            {
                </span><span style="color:rgba(0,0,255,1);">throw</span> <span style="color:rgba(0,0,255,1);">new</span><span style="color:rgba(0,0,0,1);"> Exception(ex.Message);
            }
      }</span></pre>2、替换占位符方法/// <summary>

      <span style="color:rgba(128,128,128,1);">///</span><span style="color:rgba(0,128,0,1);"> 替换文本
      </span><span style="color:rgba(128,128,128,1);">///</span> <span style="color:rgba(128,128,128,1);">&lt;/summary&gt;</span>
      <span style="color:rgba(128,128,128,1);">///</span> <span style="color:rgba(128,128,128,1);">&lt;param name="WellboreId"&gt;&lt;/param&gt;</span>
      <span style="color:rgba(128,128,128,1);">///</span> <span style="color:rgba(128,128,128,1);">&lt;param name="OuterXml"&gt;&lt;/param&gt;</span>
      <span style="color:rgba(128,128,128,1);">///</span> <span style="color:rgba(128,128,128,1);">&lt;returns&gt;&lt;/returns&gt;</span>
      <span style="color:rgba(0,0,255,1);">public</span> <span style="color:rgba(0,0,255,1);">static</span> <span style="color:rgba(0,0,255,1);">string</span> XmlStringReplace(Guid WellboreId, <span style="color:rgba(0,0,255,1);">string</span> wellbore_No, <span style="color:rgba(0,0,255,1);">string</span> OuterXml, <span style="color:rgba(0,0,255,1);">string</span><span style="color:rgba(0,0,0,1);"> appendixTypes)
      {
            </span><span style="color:rgba(0,0,255,1);">try</span><span style="color:rgba(0,0,0,1);">
            {</span><span style="color:rgba(0,0,255,1);">string</span> retVal =<span style="color:rgba(0,0,0,1);"> OuterXml;
                </span><span style="color:rgba(0,0,255,1);">#region</span> 标题第一页数据占位符替换<span style="color:rgba(0,0,0,1);">
                retVal </span>= retVal.Replace(<span style="color:rgba(128,0,0,1);">"</span><span style="color:rgba(128,0,0,1);">{AREA}</span><span style="color:rgba(128,0,0,1);">"</span><span style="color:rgba(0,0,0,1);">, 888);</span>
                <span style="color:rgba(0,0,255,1);">#endregion</span>
                <span style="color:rgba(0,0,255,1);">return</span><span style="color:rgba(0,0,0,1);"> retVal;
            }
            </span><span style="color:rgba(0,0,255,1);">catch</span><span style="color:rgba(0,0,0,1);"> (Exception ex)
            {
                </span><span style="color:rgba(0,0,255,1);">throw</span> <span style="color:rgba(0,0,255,1);">new</span> ApplicationException(<span style="color:rgba(0,0,255,1);">string</span>.Format(<span style="color:rgba(128,0,0,1);">"</span><span style="color:rgba(128,0,0,1);">替换附表数据失败:错误原因是:{0}</span><span style="color:rgba(128,0,0,1);">"</span><span style="color:rgba(0,0,0,1);">, ex.Message));
            }
      }</span></pre>  3、上面还用到一个设置页眉的方法
/// <summary>

      <span style="color:rgba(128,128,128,1);">///</span><span style="color:rgba(0,128,0,1);"> 添加页眉
      </span><span style="color:rgba(128,128,128,1);">///</span> <span style="color:rgba(128,128,128,1);">&lt;/summary&gt;</span>
      <span style="color:rgba(128,128,128,1);">///</span> <span style="color:rgba(128,128,128,1);">&lt;param name="doc"&gt;</span><span style="color:rgba(0,128,0,1);">文档对象</span><span style="color:rgba(128,128,128,1);">&lt;/param&gt;</span>
      <span style="color:rgba(128,128,128,1);">///</span> <span style="color:rgba(128,128,128,1);">&lt;param name="HeaderTxt"&gt;</span><span style="color:rgba(0,128,0,1);">页眉标题</span><span style="color:rgba(128,128,128,1);">&lt;/param&gt;</span>
      <span style="color:rgba(0,0,255,1);">public</span> <span style="color:rgba(0,0,255,1);">static</span> <span style="color:rgba(0,0,255,1);">void</span> AddHeader(WordprocessingDocument doc, <span style="color:rgba(0,0,255,1);">string</span><span style="color:rgba(0,0,0,1);"> HeaderTxt)
      {
            countheader </span>= <span style="color:rgba(128,0,128,1);">0</span><span style="color:rgba(0,0,0,1);">;
            </span><span style="color:rgba(0,0,255,1);">string</span> newHeaderText =<span style="color:rgba(0,0,0,1);"> HeaderTxt;
            MainDocumentPart mainDocPart </span>=<span style="color:rgba(0,0,0,1);"> doc.MainDocumentPart;
            mainDocPart.DeleteParts(mainDocPart.HeaderParts);
            HeaderPart newHeaderPart </span>= mainDocPart.AddNewPart&lt;HeaderPart&gt;<span style="color:rgba(0,0,0,1);">();
            </span><span style="color:rgba(0,0,255,1);">string</span> rId =<span style="color:rgba(0,0,0,1);"> mainDocPart.GetIdOfPart(newHeaderPart);
            GeneratePageHeaderPart(newHeaderText).Save(newHeaderPart);
            </span><span style="color:rgba(0,0,255,1);">foreach</span> (SectionProperties sectProperties <span style="color:rgba(0,0,255,1);">in</span><span style="color:rgba(0,0,0,1);">
            mainDocPart.Document.Descendants</span>&lt;SectionProperties&gt;<span style="color:rgba(0,0,0,1);">())
            {
                countheader</span>++<span style="color:rgba(0,0,0,1);">;
                </span><span style="color:rgba(0,0,255,1);">int</span> count=<span style="color:rgba(0,0,0,1);">sectProperties.Count();
                </span><span style="color:rgba(0,0,255,1);">foreach</span> (HeaderReference headerReference <span style="color:rgba(0,0,255,1);">in</span><span style="color:rgba(0,0,0,1);">
                  sectProperties.Descendants</span>&lt;HeaderReference&gt;<span style="color:rgba(0,0,0,1);">())
                {
                  sectProperties.RemoveChild(headerReference);
                  HeaderReference newHeaderReference </span>=
                        <span style="color:rgba(0,0,255,1);">new</span> HeaderReference() {Id = rId, Type =<span style="color:rgba(0,0,0,1);"> HeaderFooterValues.Default};
                  sectProperties.Append(newHeaderReference);
                }  }

      }</span></pre>  4、生成word中还有一个获取所有书签的方法
/// <summary>  /// 获取所有书签
/// </summary>      /// <param name="documentPart"></param>      /// <param name="results"></param>      /// <param name="unmatched"></param>      /// <returns></returns>  public static Dictionary<string, BookmarkEnd> FindBookmarks(OpenXmlElement documentPart,
  Dictionary<string, BookmarkEnd> results = null, Dictionary<string, string> unmatched = null)
  {
  results = results ?? new Dictionary<string, BookmarkEnd>();
  unmatched = unmatched ?? new Dictionary<string, string>();
  foreach (var child in documentPart.Elements())
  {
  if (child is BookmarkStart)
  {
  var bStart = child as BookmarkStart;
  if (!unmatched.ContainsKey(bStart.Id))
  {
  unmatched.Add(bStart.Id, bStart.Name);
  }
  }
  if (child is BookmarkEnd)
  {
  var bEnd = child as BookmarkEnd;
  foreach (var orphanName in unmatched)
  {
  if (bEnd.Id == orphanName.Key && !results.ContainsKey(orphanName.Value))
  results.Add(orphanName.Value, bEnd);
  }
  }
  FindBookmarks(child, results, unmatched);
  }
  return results;

      }</span></pre> 更多该组件的欢迎大家一起讨论交流。。。。
页: [1]
查看完整版本: PageOffice 使用Dome