<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <script type="text/javascript"> function GetValue(){ var xlDialogOpen = 1; //创建连接对象 var objFactory = new ActiveXObject("CacheActiveX.Factory"); //连接信息 var strConn = "cn_iptcp:127.0.0.1[1972]:YOURNAMESPACE:USERNAME:PASSWORD" //连接成功返回true,否则返回false var boolConn = objFactory.Connect(strConn); if(boolConn){ //Test.CacheFactory的类名 var objClass = objFactory.Static("Test.CacheFactory"); var objExcel = new ActiveXObject("Excel.Application"); objExcel.Visible = false; var objWorkBook = objExcel.Workbooks.Open("D:\\template.xls"); var objSheet = objWorkBook.Worksheets.Item(1); //代码 描述 值 备注 objSheet.Cells(1, 1).Value = "姓名"; objSheet.Cells(1, 2).Value = "性别"; objSheet.Cells(1, 3).Value = "年龄"; objSheet.Cells(1, 4).Value = "婚姻状况"; //调用类中方法,返回数据 var strData = objClass.TestData(); alert(strData) //解析数据 var arrData = strData.split("^"); for(var row = 0; row < arrData.length; row++) { var strRec = arrData[row]; var arrRec = strRec.split("_"); objSheet.Cells(row+2, 1).Value = arrRec[0]; objSheet.Cells(row+2, 2).Value = arrRec[1]; objSheet.Cells(row+2, 3).Value = arrRec[2]; objSheet.Cells(row+2, 4).Value = arrRec[3]; } var fname = objExcel.Application.GetSaveAsFilename( "人员信息.xls","Excel Spreadsheets (*.xls), *.xls"); objWorkBook.SaveAs(fname); //释放资源 objSheet = null; objWorkBook.Close (savechanges=true); objExcel.Quit(); objSheet = null; objWorkBook = null; objExcel = null; }else{ alert("连接失败"); } } </script> <title>下载</title> </head> <body>Cache中导出excel以及js中建立连接到class<br /> <li class="tabin" id="aaa" value="123" onclick="GetValue();">点击导出人员信息</li> </body> </html>
使用ActiveXObject此方法支持IE浏览器,火狐浏览器需要下载activexobject相关插件
导出效果:
服务器端代码:
Class Test.CacheFactory Extends (%Persistent, %XML.Adaptor) { //测试,返回数据 ClassMethod TestData() As %String { q "张三_男_22_未婚^李四_女_30_未婚" } }