pageoffice使用dome(代码片段)

zhengwei-cq zhengwei-cq     2023-02-23     615

关键词:

一、前言

PageOffice是一款帮助Web应用系统或Web网站实现用户在线编辑WordExcel、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

1. 双击运行Setup.exe安装服务器组件。(可以到官网下载安装程序)

2. 如果运行示例,用VS.Net打开MvcApplication4.sln即可运行。   

3.  如果新建网站或集成PageOffice到您现有的网站里:  

1). 双击运行Setup.exe安装服务器组件;  

2). 拷贝“集成文件”目录下的“pageoffice”文件夹到您自己网站的根目录下;  

2). VS.NET工具箱拖放PageOffice控件,双击控件,在事件代码中编写代码。

------我的项目是.net MVC4  直接把 “pageoffice”文件夹放到自己网站的根目录下;  

三、使用

使用就分为编辑一个指定路径下的文件,并保存

1、在网中打开一个指定路径下的word

 

我的文件的路径是:D:projectA27A27_SourceWebOfficeTemp总结报告.docx

如何打开该路径下的文件,直接上代码吧

 public ActionResult EditReport()
        
                       ViewBag.Message = "Your contact page.";
            System.Web.UI.Page page = new System.Web.UI.Page();
            string controlOutput = string.Empty;
            PageOffice.PageOfficeCtrl pc = new PageOffice.PageOfficeCtrl();
            try
            
                string fileName = "总结报告.docx";
                string filePath = Server.MapPath("~/OfficeTemp/")+fileName;
                string currfilepath =  "/" + fileName;
                pc.SaveFilePage = Url.Content("SaveDoc") + "?path=" + Server.UrlEncode(currfilepath);

                if (filePath == null) throw new ApplicationException("配置文件中未找到对应系统的项");
                pc.ServerPage = Url.Content("~/pageoffice/server.aspx");
                pc.WebOpen(filePath, PageOffice.OpenModeType.docAdmin, "s");

                result.IsSuccess = true;

                page.Controls.Add(pc);
                StringBuilder sb = new StringBuilder();
                using (StringWriter sw = new StringWriter(sb))
                
                    using (HtmlTextWriter htw = new HtmlTextWriter(sw))
                    
                        Server.Execute(page, htw, false); controlOutput = sb.ToString();
                    
                
                ViewBag.EditorHtml = controlOutput;

            
            catch (Exception err)
            
                throw err;
            

            return PartialView();
        

2、保存编辑后的word, 保存时调用的saveDoc方法和参数正是编辑时这段代码提供的:

pc.SaveFilePage = Url.Content("SaveDoc") + "?path=" + Server.UrlEncode(currfilepath);

public ActionResult SaveDoc(string path)
        
            string filePath = Server.MapPath("~/OfficeTemp/" + path);
            PageOffice.FileSaver fs = new PageOffice.FileSaver();
            fs.SaveToFile(filePath);
            fs.Close();
            return View();
        

四、根据数据库动态生成word文件

生成word并不是pageoffice的功能,但是会一起与该组件一起使用

1、生成word

 /// <summary>
        /// 报告word
        /// </summary>
        /// <param name="wellboreId"></param>
        /// <returns></returns>
        public string OutPutDoc(Guid wellboreId, string appendixTypes, string wellboreNo)
        
            try
            
              
string filePathNew = Server.MapPath("~/OfficeTemp/") + wellbore_No + "/" + HttpContext.User.Identity.Name + "/";//Server.MapPath("~/Upload/LogReportTemp/") +"\" + HttpContext.User.Identity.Name;
                //生成文件夹

               Directory.CreateDirectory(filePathNew);

                string fullfileName = filePathNew  + fileName;
                string mainDocPath = Server.MapPath("~/Upload/LogReportTemp/GeoSummaryReport_template.docx");//Server.MapPath("~/Upload/LogReportTemp/LS25-1-5测井作业总结报告01.docx                 using (var mainDoc =WordprocessingDocument.Open(mainDocPath, false))
                using (var resultDoc = WordprocessingDocument.Create(fullfileName, WordprocessingDocumentType.Document))
                
  
                                      MainDocumentPart mainPart = resultDoc.MainDocumentPart;
                    foreach (var part in mainDoc.Parts)
                    
                        if (part.OpenXmlPart.ContentType != "application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml")
                            resultDoc.AddPart(part.OpenXmlPart, part.RelationshipId);
                        else if (mainPart == null)
                        
                            mainPart = (MainDocumentPart)resultDoc.AddPart(part.OpenXmlPart, part.RelationshipId);
                            //通过替换OuterXml中的占位符替换文本
                            mainPart.Document.Body = new Body(GeoSummaryReportHelper.XmlStringReplace(wellboreId, wellbore_No, mainDoc.MainDocumentPart.Document.Body.OuterXml, appendixTypes));
                            var bookMarks = GeoSummaryReportHelper.FindBookmarks(resultDoc.MainDocumentPart.Document);
                            //替换书签中的内容
                            foreach (var end in bookMarks)
                            
                                //为了满足甲方格式要求,使用模板生成方式
                                if (end.Key == "SuizuanTypeMark") GeoSummaryReportHelper.CreateSuiZuanTable(end,resultDoc ,wellbore_No, "LWD",wellboreId);
                                if (end.Key == "DianLanTypeMark") GeoSummaryReportHelper.CreateDianLanTable(end, resultDoc, wellbore_No, "RUN", wellboreId);
                               
                                if (end.Key.Contains("DrillAndCasing")) GeoSummaryReportHelper.DrawingDrillAndCasingInfoTb(end, wellboreId);//绘制基本信息井身结构模块表格

                            
                        
                    
                    string headerText = string.Format("0总结报告", wellbore_No);
                    GeoSummaryReportHelper.AddHeader(resultDoc, headerText);//添加页眉
                

                string url = Request.Url.ToString().Replace(Request.Url.AbsolutePath,"");
                //返回生成的文档的信息
                string ahref = url + "/OfficeTemp/" + wellbore_No + "/" + HttpContext.User.Identity.Name + "/" + fileName;//Server.MapPath("~/OfficeTemp/") + wellboreId + "/" + HttpContext.User.Identity.Name + "/" + fileName; //Url.Content(Server.MapPath("~/OfficeTemp/")) + wellboreId + "/" + HttpContext.User.Identity.Name + "/" + fileName; 

                string createTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm");
                string 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>", ahref, fileName, createTime);
                return 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);


            
            catch (Exception ex)
            
                throw new Exception(ex.Message);
            
        

2、替换占位符方法

/// <summary>
        /// 替换文本
        /// </summary>
        /// <param name="WellboreId"></param>
        /// <param name="OuterXml"></param>
        /// <returns></returns>
        public static string XmlStringReplace(Guid WellboreId, string wellbore_No, string OuterXml, string appendixTypes)
        
            try
            string retVal = OuterXml;
                #region 标题第一页数据占位符替换
                retVal = retVal.Replace("AREA", 888);
                #endregion

                return retVal;
            
            catch (Exception ex)
            
                throw new ApplicationException(string.Format("替换附表数据失败:错误原因是:0", ex.Message));
            

        

3、上面还用到一个设置页眉的方法

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

                    sectProperties.Append(newHeaderReference);
                

            
        

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;
        

 更多该组件的欢迎大家一起讨论交流。。。。

pageoffice5.2试用版注册使用(代码片段)

1下载在pageoffice官网下载需要的版本http://www.zhuozhengsoft.com/dowm/我这里使用的是Java下载的是5.2下载后压缩包有集成文件,示例代码,序列号,安装说明2运行示例将Samples5拷贝到本地tomcat的webapps下面启动tomcat,访问to... 查看详情

一个ajax的dome(代码片段)

文章目录前言实现页面布局原生JS实现Ajax使用Jquery实现前言所谓的Ajax其实就是动态的去请求另一个资源站点,然后将资源加载到我们的当前页面中,例如当你点击网页下一页时页面会发生改变,但是却没有发生跳转... 查看详情

国产麒麟操作系统调用pageoffice在线编辑保存wps文档(代码片段)

...满足政府、企业等日常办公需求。在Windows系统环境下,PageOffice产品积累了十多年的Office在线编辑研发经验和应用经验,现在针对麒麟系统在线办公发布了支持国产操作系统的中间件产品PageOffice国产版,能很好的支持龙芯,兆芯... 查看详情

国产uos操作系统调用pageoffice在线编辑保存office文档(代码片段)

...满足政府、企业等日常办公需求。在Windows系统环境下,PageOffice产品积累了十多年的Office在线编辑研发经验和应用经验,现在针对UOS在线办公发布了支持国产操作系统的中间件产品PageOffice国产版,能很好的支持龙芯,兆芯,飞腾... 查看详情

javadome(aop模式回顾小dome)(代码片段)

...写Dome的流程图代理流程图完整的流程Dome的项目结构完整代码总结补充Java代理模式代理静态代理动态代理何为AOP用不是人话来说就是面向切面编程,什么是切面,就是和我们程序的主体功能关系不大的那一方面,例如... 查看详情

springboot整合pageoffice实现word在线编辑保存。(代码片段)

一、查看官网http://www.zhuozhengsoft.com/点击首页下载,进入页面:最新得5.2,我们就下载5.2版本进行测试。二、查看下载包Samples5为示例文件。放入tomcat中得webapps可以直接访问。localhost:8080/Samples5/index.html集成文件里面有... 查看详情

java网络编程(socket(tcp小dome))(代码片段)

目录Java流socket定义Javasocket的使用客户端多客户端请求处理Java流在进行网络编程之前,我们一定要搞清楚,流这玩意对于Java来说是多么重要。我们可以通过流来完成文件的存储,读取,同时也通过流来对网络数据... 查看详情

胡扯推荐算法(协同)及其dome实现(代码片段)

...过滤案例数据定义相似度计算推荐svd奇异值分解优化完整代码总结前言打瞌睡遇到送枕头的感觉真爽嘿嘿@BoyC啊废话不多说,开始吧。推荐系统定义推荐系统(RecommendationSystem,RS),就是根据用户的日常行为,自动预... 查看详情

懂得i++和++i(代码片段)

懂得i++和++i案例代码1packageorg.huangao.other.dome1;importorg.junit.Test;publicclassDome1@Testpublicvoidtest1()intj=0;j=j++;System.out.println(j);@Testpublicvoidtest2()intj=0;j=++j;System.out.println(j);结果test1:0test2:1代码2packageorg.huangao.other.dome1;importorg.junit.Test;publicc... 查看详情

python绘图小dome(代码片段)

文章目录前言实现读取数据绘图完整实现总结前言冒个泡泡,好久没有冒泡泡了,那么今天的话就浅浅水一下博文吧。任务是这样的,将Excel当中的数据,把它放到咱们的matlpolitlib里面去画个图。数据是这样子的&#... 查看详情

pageoffice的使用

Java调用PageOffice实现在线编辑保存Word文件(以jsp调用为例,支持SSM、SSH、SpringMVC等流行框架)1.下载PageOffice开发包:http://www.zhuozhengsoft.com/dowm/下载PageOfficeforJAVA2. 以Myeclipse为例,新建一个WebProject,比如:wordonline3. 解压Pag... 查看详情

pysyft学习笔记一:dome思路(代码片段)

Pysyft是一个比较适合学习FL学习小白的开源框架,相比与FATE的高封装性,Pysyft提供了更高的自由度。虽然FATE确实加密算法等等的效果做的非常的好,但是我在Ubuntu下面捣鼓了两天,下载了Mysql,Redis等等的一堆... 查看详情

如何在js函数中调用另外一个函数

...资料函数是由事件驱动的或者当它被调用时执行的可重复使用的代码块。JavaScript函数语法,函数就是包裹在花括号中的代码块,前面使用了关键词function:当调用该函数时,会执行函数内的代码。可以在某事件发生时直接调用函... 查看详情

dome-5单元测试

...库power-assert,没有API就是最好的API,详细的报错信息egg中使用了egg-bin来运行测试脚本,其融合了多个模块,让精力更专注于测试上测试的顺序。测试代码中有before,beforeEach,it,after 查看详情

多值参数(代码片段)

*args和**kwargs,一看就能知道args是神马,就是所有参数的数组,kwargs就不知道了,google一下,一个人的blog写的比较简单明了,秒懂了~~kwargs就是当你传入key=value是存储的字典。defdome(num,*tuple,**dict):print(num)print(tuple)print(dict)dome(1)dom... 查看详情

使用io流实现一个简单的小dome

(一)在电脑D盘下创建一个文件为HelloWorld.txt文件,判断他是文件还是目录,在创建一个目录IOTest,之后将HelloWorld.txt移动到IOTest目录下去;之后遍历IOTest这个目录下的文件 为了提高代码的安全性,这里所有的异常,我都处理... 查看详情

自己动手做个分页插件(代码片段)

PC分页,完美支持ie8+,2KB下面就是见证奇迹的时刻dome地址https://github.com/cleartime/pageSize/blob/master/dome/index.html配置详情varconfig=el:document.getElementById("page"),//绑定到你的dompageCount:10,//总页数ps:如果后两项填写了则本字段失效,两者只需其 查看详情

如何在js函数中调用另外一个函数

...怎么写的。①如果是最简单的一个方法:functiuondome1()//.代码块//调用方法就是:dome1();②如果是闭包形式:functiondome1()functiondome2()returnfunctiondome3()//这里可以访问dome1方法内的dome3方法,dome2方法对与dome1方法外部是不可见的。③如... 查看详情