文档控件NTKO OFFICE 详细使用说明之预览Excel文件(查看、编辑、保存回服务器)
1、在线预览Excel文件
(1) 运行环境
① 浏览器:支持IE7-IE11(平台版本还支持Chrome和Firefox)
② IE工具栏-Internet 选项:将www.ntko.com加入到浏览器的可信站点中,不启用保护模式,并将HTTPS验证取消
③ 如果是开发人员首次使用文档控件,需要将控件手动注册控件,获取控件的版本clisid以及版本信息Version(如果程序已经开发完则可以忽略此步骤)
(2) 创建一个加载控件的JS文件,用户只需修改控件版本号Version,classid,cab包路径,以及添加授权即可。具体可访问http://dev.ntko.com:8000/查看
① JS文件代码如下:(-1表示TRUE,0表示FALSE)
////----在线编辑Excel并保存 document.write('<object id="TANGER_OCX" classid="clsid:A64E3073-2016-4baf-A89D-FFE1FAA10EC0"'); document.write('codebase="NTKO/OfficeControl.cab#version=5,0,3,0" width="100%" height="100%"> '); document.write('<param name="Toolbars" value="-1"> '); // 工具栏 document.write('<param name="Menubar" value="0"> '); //菜单栏 document.write('<param name="CustomToolBar" value="-1"> '); document.write('<param name="Titlebar" value="-1"> '); //标题栏 document.write('<param name="Caption" value="华测计量技术有限公司专用">');//标题内容 document.write('<param name="IsUseControlAgent" value="-1"> '); //是否是控制请求数据,其他请他不允许 document.write('<param name="IsNoCopy" value="0"> '); //0不允许复制 document.write('<param name="IsDirectConnect" value="-1"> '); document.write('<param name="DefaultOpenDocType" value="1"> ');//1表示只能打开word文档 document.write('<param name="FileOpen" value="0"> '); document.write('<param name="FileNew" value="0"> '); document.write('<param name="FileSave" value="0"> '); document.write('<param name="FileSaveAs" value="0"> '); document.write('<param name="FilePrint" value="0"> '); document.write('<param name="FilePrintPreview" value="0"> '); document.write('<param name="ProductCaption" value="Office Anywhere 3.0"> '); document.write('<param name="ProductKey" value="65F867689D7FF5CD02FBE4AA23764306F715A9E2">'); document.write('<SPAN STYLE="color:red">不能装载NTKO OFFICE文档控件。请在检查浏览器的选项中检查浏览器的安全设置。</SPAN> '); document.write('</object> ');
① View代码如下:
@{ ViewBag.Title = "预览excel"; } <h2>Index</h2> <html> <head> <title>NTKO Office文档控件在线预览Excel文件(修改、保存回服务器)演示示例</title> <meta http-equiv="content-type" content="text/html; charset=gb2312"> </head> <body bgcolor="#ffffff" ;> <p> <span>针对excel单元格的操作</span> <button onclick="AddPicToWord()">添加图片</button> <button onclick="ReplacePicToWord()">替换图片</button> <button onclick="AddTextToWord()">插入文字</button> <button onclick="ReplaceTextToWord()">替换指定的文字</button> <input type="submit" value="保存文档" id="_upload"> </p> <script src="~/Scripts/ntkoGenObj.js"></script> </body> </html> <script src="~/Scripts/jquery-1.10.2.min.js"></script> <script language="javascript"> //控件初始化完成后再以流的形式加载文件 $(function () { inIt(); }); //初始化 function inIt() { if (TANGER_OCX == null) { alert("NTKO OFFICE文档控件未能正确装载。请在本页面装载时窗口上方出现的提示条上点击装载ActiveX控件,并在随后出现的安装ActiveX的对话框中选择‘是’。"); return; } OpenFile(); } //以流的形式打开文件 function OpenFile() { TANGER_OCX.BeginOpenFromURL('@Url.Action("ReturnFileStream", "ExcelView")', true); } //在线编辑,保存修改后的文件回服务器 $("#_upload").click(function () { TANGER_OCX.SaveToURL('@Url.Action("uploadFile", "ExcelView")', "TANGER_OCX", "", "", "", true); }); //添加图片 function AddPicToWord() { var localName = "W33";//单元格的名称 TANGER_OCX.Activate(true);//激活excel var range=TANGER_OCX.ActiveDocument.Application.Worksheets("Cover").Range(localName); TANGER_OCX.ActiveDocument.Application.Goto(range);//跳转指定单元格 TANGER_OCX.AddPicFromURL("@Url.Content("~/NTKO/william.jpg")", true, 0, 0, 1, 100, 1);//添加图片 //TANGER_OCX.ActiveDocument.Application.ActiveCell.FormulaR1C1 = "恭喜你找到了我!";//设置内容 localName = "W36";//单元格的名称 range = TANGER_OCX.ActiveDocument.Application.Worksheets("Cover").Range(localName); TANGER_OCX.ActiveDocument.Application.Goto(range); TANGER_OCX.AddPicFromURL("@Url.Content("~/NTKO/jimly.jpg")", true, 0, 0, 1, 100, 1); } //找到对应的单元格删除图片,替换图片 function ReplacePicToWord() { var localName = "W33"; TANGER_OCX.Activate(true); var range = TANGER_OCX.ActiveDocument.Application.Worksheets("Cover").Range(localName); TANGER_OCX.ActiveDocument.Application.Goto(range); var shapes = TANGER_OCX.ActiveDocument.Application.Worksheets("Cover").shapes; for (var i = 1; i <= shapes.Count; i++) { //alert(shapes(i).Type + shapes(i).AlternativeText); if (shapes(i).AlternativeText == "william") { shapes(i).Delete(); TANGER_OCX.AddPicFromURL("@Url.Content("~/NTKO/jimly.jpg")", true, 0, 0, 1, 100, 1); } } } //为单元格添加文字信息 function AddTextToWord() { var localName = "J55"; TANGER_OCX.Activate(true); var range=TANGER_OCX.ActiveDocument.Application.Worksheets("Cover").Range(localName); TANGER_OCX.ActiveDocument.Application.Goto(range); TANGER_OCX.ActiveDocument.Application.ActiveCell.FormulaR1C1 = "恭喜你找到了我!";//设置内容 } //修改单元格的内容 function ReplaceTextToWord() { var localName = "J55"; TANGER_OCX.Activate(true); var range = TANGER_OCX.ActiveDocument.Application.Worksheets("Cover").Range(localName); TANGER_OCX.ActiveDocument.Application.Goto(range); TANGER_OCX.ActiveDocument.Application.ActiveCell.FormulaR1C1 = "哈哈,恭喜你you找到了我!";//设置内容 } </script>
Controller代码如下:
/// <summary> /// 打开文件 /// </summary> /// <returns></returns> public ActionResult ReturnFileStream() { var bytes = Properties.Resources.excel; var fileStream = new MemoryStream(bytes); return new FileStreamResult(fileStream, "application/xhtml+xml"); } /// <summary> /// 保存文件 /// </summary> /// <returns></returns> [HttpPost] public ActionResult uploadFile() { var files = Request.Files[0]; var path = Server.MapPath($"~/{DateTime.Now.ToLongDateString()}.xls"); FileStream fs = new FileStream(path, FileMode.OpenOrCreate); byte[] bytes = new byte[files.InputStream.Length]; files.InputStream.Read(bytes, 0, bytes.Length); fs.Write(bytes, 0, bytes.Length); fs.Close(); return View("Index"); }