WebService中使用Aspose.Cells.dll

时间:2023-12-04 16:57:38

首先,目前我是在Json里面使用的,然后关于HTML+WebService+Json怎么使用,可以看看Jsonp跨域的相关例子。

本次的实现原理是:通过HTML传送参数到WebService,然后在WebService生成Excel文件,然后保存在服务器上,然后服务器返回文件的相对路径,然后HTML接收到路径,直接下载;
下面我直接贴出WebService的代码;
        /// <summary>
        /// 获取房间每小时报表
        /// </summary>
        [WebMethod(EnableSession = true)]
        public void GetRoomReportOfHour()
        {
            HttpContext.Current.Response.ContentType = "application/json;charset=utf-8";
            string jsonCallBackFunName = HttpContext.Current.Request.Params["jsoncallback"].ToString();
            //上面代码必须,不能注释
            //中间代码执行自己的业务操作,可返回自己的任意信息(多数据类型)
            string strJson = "";
            string Date = HttpContext.Current.Request["Date"];
            string DeptID = HttpContext.Current.Request["DeptID"];
            string BuildId = HttpContext.Current.Request["BuildId"];
            if (!string.IsNullOrWhiteSpace(Date) && !string.IsNullOrWhiteSpace(DeptID) && !string.IsNullOrWhiteSpace(BuildId))
            {
                int iDepartID = DataFormat.ConvertDBNullToInt32(DeptID);
                DateTime dtTime = DataFormat.ConvertDBNullToDateTime(Date);
                List<ModelForDepartmentOrRoomOfHour> mList = DataLoader.GetRoomOfHour(dtTime, DeptID, BuildId);
                if (mList.Count > 0)
                {
                    string strReportDate = DateTime.Now.ToString("yyyy-MM-dd");
                    string strDepartName = DataLoader.GetDepartInfoByDepartID(iDepartID).DeptName;
                    string strBuildName = DataLoader.GetBuildInfoByBuildID(BuildId).F_BuildName;
                    DataTable dtSource = DTListFormat.ListToDataTable(mList);
                    //Excel的路径 是放excel模板的路径
                    WorkbookDesigner designer = new WorkbookDesigner();
                    string strSystemPath = HttpContext.Current.Server.MapPath("~");
                    designer.Open(strSystemPath + "/excel/RoomReportOfHour.xls");
                    Worksheet sheet = designer.Workbook.Worksheets[0];
                    var c124 = sheet.Cells[1, 24];//第二行 第25列,导出日期
                    c124.PutValue(strReportDate);
                    var c33 = sheet.Cells[3, 3];
                    c33.PutValue(strBuildName);
                    var c36 = sheet.Cells[3, 6];
                    c36.PutValue(strDepartName);
                    var c312 = sheet.Cells[3, 12];
                    c312.PutValue(Date);
                    sheet.Cells.ImportDataTable(dtSource, false, 6, 0, true);
                    SaveOptions s = new XlsSaveOptions(SaveFormat.Excel97To2003);
                    string str = "";
                    str = HttpUtility.UrlEncode(string.Format("{0}.xls", DateTime.Now.ToString("yyyyMMddHHmmss")), Encoding.UTF8).ToString();
                    designer.Workbook.Save(strSystemPath + "/Attachment/" + str);//通过这个方法,就可以通过WebService来使用Excel文件保存和文件下载了。
                    ModelForReport aReport = new ModelForReport()
                    {
                        strUrl = "Attachment/" + str
                    };
                    strJson = JsonHelper.Serialize(aReport);
                }
            }
            //下面代码必须,不能注释
            HttpContext.Current.Response.Write(string.Format("{0}({1})", jsonCallBackFunName, strJson));
            HttpContext.Current.Response.End();
        }

HTML端:
            function fExportReportOfDay() {
                var date = $("#IdForDayTime").val();
                var DepartId = $('#DeptID').combobox('getValue') //获取当前选中的值;
                var BuildID = $('#BuildID').combobox('getValue') //获取当前选中的值;
                $.ajax({
                    type: 'GET',
                    url: vUrlPre + 'WebService.asmx/GetRoomReportOfHour?jsoncallback=?',
                    dataType: 'jsonp',
                    data: {
                        Date: date,
                        DeptID: DepartId,
                        BuildId: BuildID
                    },
                    success: function(data) {
                        var vUrl = vUrlPre + data.strUrl;//解释一下,vUrlPre是一个全局的参数,是远程WebService的服务地址,例如:http://www.webservice.com/
                        fDownLoad(vUrl);
                    },
                    error: function(data) {
                        var vData = JSON.stringify(data);
                    }
                })

}