PageOffice6最简集成代码(.NetCore)

本文描述了PageOffice产品在.NetCore项目中如何集成调用。

  1. 新建.NetCore项目:PageOffice6-Net-Core-Simple
  2. 在此项目的“依赖项-包-管理NuGet程序包”中搜索到“Zhuozhengsoft.PageOffice"程序后安装最新版本。
  3. 下载PageOffice客户端安装程序。
  • windows客户端安装程序:posetup_6.x.x.x.exe;
  • 国产操作系统客户端安装程序:对应芯片的PageOffice客户端deb安装包;

PageOffice客户端安装程序下载地址:https://gitee.com/pageoffice/pageoffice6-client/releases

  1. 拷贝PageOffice客户端安装程序到项目的bin目录下(项目的生成目录,比如:bin\Debug\net5.0)

  2. 修改Startup.cs文件,添加PageOffice服务器端程序的相关配置,代码如下:

  public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
  {
      ...
      app.UseMiddleware<PageOfficeNetCore.POServer.ServerHandlerMiddleware>();
  }

如果是.Net6项目,就在Program.cs文件中添加代码:app.UseMiddleware<PageOfficeNetCore.POServer.ServerHandlerMiddleware>();

  1. 在D盘根目录下准备一个有内容的test.docx文件。
  2. 新建WordController,并编写代码在线打开test.docx。WordController.cs代码如下:
public class WordController : Controller
{
    public IActionResult Index()
    {
        PageOfficeNetCore.PageOfficeCtrl poCtrl = new PageOfficeNetCore.PageOfficeCtrl(Request);
        poCtrl.SaveFilePage = "/Word/SaveFile"; //设置处理保存客户端上传文件流的Action方法
        poCtrl.WebOpen("D:\\test.docx", PageOfficeNetCore.OpenModeType.docNormalEdit, "tom");
        ViewBag.POCtrl = poCtrl.GetHtmlCode();
        return View();
    }

    //接收客户端上传的文件流,并保存到文件
    public async Task<ActionResult> SaveFile()
    {
        PageOfficeNetCore.FileSaver fs = new PageOfficeNetCore.FileSaver(Request, Response);
        await fs.LoadAsync();
        fs.SaveToFile("D:\\" + fs.FileName);
        return fs.Close();
    }
}
  1. 编写“Word/Index”的view视图文件Index.cshtml,代码如下:
@{
    Layout = null;
}
<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
    <script type="text/javascript">
        function Save() {
            pageofficectrl.WebSave();
        }
        function OnPageOfficeCtrlInit() {
            pageofficectrl.AddCustomToolButton("保存", "Save", 1);
        }
    </script>
</head>
<body>
    <div style=" width:auto; height:98vh;">
        @Html.Raw(ViewBag.POCtrl)
    </div>
</body>
</html>
  1. 在需要实现“点击超链接在线打开文件”的页面(比如:Views/Shared/_Layout.cshtml)中添加对pageoffice.js的引用。

注意
项目中并不存在pageoffice.js文件,此文件来自于Startup.cs中配置的PageOffice服务器端程序,此程序中封装了pageoffice.js。

  1. 然后在页面(比如:Views/Shared/_Layout.cshtml)中添加一个超链接,点击超链接调用POBrowser对象的openWindow方法,弹出新浏览器窗口访问“Word/Index”在线打开文件,代码如下:
<a href="javascript:POBrowser.openWindow('Word/Index' , 'width=1200px;height=800px;');">
    在线打开文档
</a>
  1. 启动项目,点击“在线打开文档”超链接,查看在线打开编辑保存Office文件的效果。

参考链接:PageOffice最简集成代码(.NetCore)

posted on 2024-04-11 11:26  qianxi  阅读(39)  评论(0编辑  收藏  举报