当前位置:编程学习 > C#/ASP.NET >>

MVC3 导出Excel 问题,急急急!!!!

1.后台从数据库取出数生成FileResult,正常情况下浏览器可直接导出excel了
但是现在想控制View层的按钮,点击导出按钮后禁用按钮,等事件完成再启用就出问题了。

后台代码:

  [HttpPost]
        public FileResult Index(ReportModel model)
        {
            if (!model.ReporType.HasValue)
            {
                return null;
            }

            byte reportTypeValue = 0;
            string reportName = string.Empty;
            ReportType accountMaster = _reportService.GetReportType(model.ReporType.Value);
            if (accountMaster == null)
            {
                return null;
            }
            else
            {
                reportTypeValue = accountMaster.Id;
                reportName = accountMaster.Name;
            }

            byte businessTypeValue = 0;
            string businessName = string.Empty;
            if (model.BusinessType.HasValue)
            {
                businessTypeValue = model.BusinessType.Value;
                businessName = _reportService.GetReportBusinessName(reportTypeValue, businessTypeValue);
            }

            var getUser = GetUser();
            ReportSearchCondition rsc = new ReportSearchCondition
            {
                ReportName = reportName,
                BusinessID = businessTypeValue,
                BusinessName = businessName,
                BeginTime = model.BeginTime.Value,
                EndTime = model.EndTime.Value.AddMilliseconds(997),
                OperaterID = getUser.UserCode,
                OperaterName = getUser.UserName
            };

            Func<ReportSearchCondition, DataSet> func = GetReportMethod(reportTypeValue);

            string fileName = string.Format("{0}从{1}到{2}",
                                                  rsc.BusinessID == 0 ? rsc.ReportName : rsc.BusinessName,
                                                  Convert.ToDateTime(rsc.BeginTime).ToString("yyMMdd"),
                                                  Convert.ToDateTime(rsc.EndTime).ToString("yyMMdd"));
            ExportDataToExcel(func, rsc);
            return File(fileName, "application/vnd.ms-excel");
        }


2.前台通过Ajax.BeginForm来异步控制,onbegin就执行startFunc脚本禁用导出按钮,onsuccess就
执行completeFunc启用导出按钮,但是这个时候,浏览器就不再出现下载EXCEL文件的提示了!!
整了一天,希望有高手指点。顺便说下Ajax.BeginForm 的 UpdateTargetId="Result" 能把报表结果导出到
id=result这个div下面展示到页面,就是TM不出现下载文件。
 前台代码:

@model Myj.ELife.Financial.WebUI.Models.ReportModel
@{
    ViewBag.Title = "Index";
}

<div id="content">
    <fieldset>
        <legend>报表导出</legend>
        <div class="edit">
        @using (Ajax.BeginForm("Index", "Report", new AjaxOptions {
            LoadingElementId = "message",
       OnBegin = "startFunc",
       OnSuccess = "completeFunc" ,
       //UpdateTargetId="Result"
        }))
        {
            <table cellpadding="0" cellspacing="0" class="table_edit">
             <tr>
                <th>@Html.Encode("报表类型:")</th>
                <td>
                    @Html.DropDownList("ReporType", Model.AvailableReportTypeStatuses)
                </td>
                <th>@Html.Encode("业务类型:")</th>
               <td>
                @Html.DropDownList("BusinessType", Model.AvailableBusinessTypeStatuses)
               </td>
               </tr>
                <tr>
                    <th>
                        @Html.Encode("开始时间:")
                    </th>
                    <td> 
                        @Html.TextBox("BeginTime", null, new { @class = "mediumfield", @id = "txtBeginTime", onfocus = "WdatePicker({dateFmt:'yyyy-MM-dd HH:mm:ss',readOnly:true})" })
                    </td>
                
                    <th>
                        @Html.Encode("截止时间:")
                    </th>
                    <td> 
                        @Html.TextBox("EndTime", null, new { @class = "mediumfield", @id = "txtEndTime", onfocus = "WdatePicker({dateFmt:'yyyy-MM-dd HH:mm:ss',readOnly:true})" })
                    </td>
                </tr>
                 <tr>
                        <td colspan="4" style="text-align: center;">
                            <input class="button_big" type="submit" id="i_Export" value="报表导出" />
                        </td>
                    </tr>
                    <tr>
                        <td colspan="4" style="text-align: center;">
                            <div id="message" style="color: Red;display:none;">
                                 <img id="loadingImg" src="../../Content/themes/images/loadingData.gif" />正在导出...
                            </div>
                        </td>
                    </tr>
                     <tr>
                        <td colspan="4" style="text-align: center;">
                            <div id="Result" style="color: Red;">
                            </div>
                        </td>
                    </tr>
            </table>     
  
        }
        </div>
    </fieldset>
</div>
@*<script src="@Url.Content("../../Scripts/My97DatePicker/WdatePicker.js")" type="text/javascript"></script>*@
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.min.js")" type="text/javascript"></script>

<script type="text/javascript">
    var startFunc = function () {
        $('input[id=i_Export]').attr('disabled', true);
        $('input[id=i_Export]').removeAttr('class');
    }
    var completeFunc = function () {
        $('input[id=i_Export]').removeAttr('disabled');
        $('input[id=i_Export]').attr('class', 'button_big');
        $.ajax({
                 type: this.method,
                  url:  this.action,
                  data:  $(this).serialize(),
                  });
    }
    $(document).ready(function () {

        $('select[id=ReporType]').change(function () {

            var id = $('select[id=ReporType]').val();
            //门店余额查询及深圳通明细查询(结算)不需要供应商
            if (id == 2 || id == 5) {
                $("#BusinessType").empty();
                $("<option></option>").val("").html("--请选择--").appendTo("#BusinessType");
                $('select[id=BusinessType]').attr("disabled", true);
                return false;
            }
            $('select[id=BusinessType]').attr("disabled", false);
            var url = '@Url.Content("~/Report/GetAccountDetails?id=")' + id + "&" + Math.random();
            $.getJSON(url, function (result) {
                $("#BusinessType").empty();
                $("<option></option>").val("").html("--请选择--").appendTo("#BusinessType");
                $.each(result, function (i) {
                    $("#BusinessType").append($("<option></option>").val(result[i].SupplierCode).html(result[i].SupplierName))
                });
            });
        });
      
        $('#i_Export').click(function () {
            var reporTypeId = $('select[id=ReporType]').val();
            if (typeof (reporTypeId) == undefined || reporTypeId == '') {
                alert("请选择报表类型");
                return false;
            }

            // 检查选择的业务类型,排除门店余额查询及深圳通明细查询(结算)
            if (reporTypeId != 2 && reporTypeId != 5) {
                var bussinessId = $('select[id=BusinessType]').val();
                if (typeof (bussinessId) == undefined || bussinessId == '') {
                    alert("请选择业务类型");
                    return false;
                }
            }

            //检查输入的日期是否合法
            var beginTime = $('#txtBeginTime').val();
            var endTime = $('#txtEndTime').val();

            if (typeof (beginTime) == undefined || beginTime == '') {
                alert("请输入开始时间!");
                return false;
            }

            if (typeof (endTime) == undefined || endTime == '') {
                alert("请输入结束时间!");
                return false;
            }

            var beginDate = new Date(beginTime.replace(/^(\d{4})(\d{2})(\d{2})$/, "$1/$2/$3"));
            var endDate = new Date(endTime.replace(/^(\d{4})(\d{2})(\d{2})$/, "$1/$2/$3"));

            if (beginDate > endDate) {
                alert("开始时间不能大于结束时间!");
                return false;
            }
        });
    })
</script>


--------------------编程问答-------------------- 人烟稀少,顶一下 --------------------编程问答-------------------- 没有高手吗?
补充:.NET技术 ,  .NET Framework
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,