导出ex

这段代码展示了如何通过Ajax调用后台处理方法ExcelPaege,该方法利用NPOI库生成Excel文件。Ajax请求携带当前页数,后台根据页数和排序参数查询数据并转化为Excel格式,最后返回文件下载链接。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 function getExcelToPage() {
        var grid = $("#tb1");
        var options = grid.datagrid('getPager').data("pagination").options;
        //当前页
        var curr = options.pageNumber;
        $.ajax({
            url: "/Handlers/DataMan/Handler1.ashx?action=getExPage",
            data: {
                index: curr
            },
            beforeSend: function () {//ajax执行之前运行
                //打开进度条
                var win = $.messager.progress({
                    title: '文件下载中,请耐心等待.....',//标题
                    msg: 'Loading data...'//消息框显示值
                });
            },
            complete: function () {//ajax执行之后,且不论结果是成功还是失败
                $.messager.progress("close");
            },
            success: function (res) {
                var link = document.createElement('a');//创建a标签
                link.setAttribute("download", "");//实现下载
                link.href = res;//下载地址
                link.click();
            }
        });
    }
        public void ExcelPaege(HttpContext context)
        {
            try
            {
                int pageSize = 10;
                if (!String.IsNullOrEmpty(context.Request["rows"]))
                {
                    pageSize = int.Parse(context.Request["rows"].ToString());
                }
                int pageIndex = Convert.ToInt32(context.Request["index"]);
                if (!string.IsNullOrEmpty(context.Request["page"]))
                {
                    pageIndex = int.Parse(context.Request["page"]);
                }
                int countPage = 0;
                string where = "";
                string order = "asc";
                if (!string.IsNullOrEmpty(context.Request["order"]))
                {
                    order = context.Request["order"];
                }
                string sort = "编号";
                if (!string.IsNullOrEmpty(context.Request["sort"]))
                {
                    sort = context.Request["sort"];
                }

                DataTable dt = bll.SelectList(where, pageSize, pageIndex, sort, order, out countPage);
                string excel = Common.ExcelHelper.ExportEasy(dt, context);
                context.Response.Write(excel);
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }
    public class ExcelHelper
    {
        /// <summary>
        /// NPOI简单Demo,快速入门代码
        /// </summary>
        /// <param name="dtSource"></param>
        /// <remarks>NPOI认为Excel的第一个单元格是:(0,0)</remarks>
        public static string ExportEasy(DataTable dtSource, HttpContext context)
        {
            HSSFWorkbook workbook = new HSSFWorkbook();
            HSSFSheet sheet = (HSSFSheet)workbook.CreateSheet();

            //填充表头
            HSSFRow dataRow = (HSSFRow)sheet.CreateRow(0);
            foreach (DataColumn column in dtSource.Columns)
            {
                dataRow.CreateCell(column.Ordinal).SetCellValue(column.ColumnName);
            }
            //填充内容
            for (int i = 0; i < dtSource.Rows.Count; i++)
            {
                dataRow = (HSSFRow)sheet.CreateRow(i + 1);
                for (int j = 0; j < dtSource.Columns.Count; j++)
                {
                    dataRow.CreateCell(j).SetCellValue(dtSource.Rows[i][j].ToString());
                }
            }

            string strFileName = context.Server.MapPath("~/") + @"\MyExc\excel.xls";//@可以转换\,根目录下的:创建的名字
                                           //http     ://               localhost:2149 
            string url = context.Request.Url.Scheme + "://" + context.Request.Url.Authority + "/MyExc/excel.xls";//服务器下载的虚拟路径
            //保存
            using (MemoryStream ms = new MemoryStream())
            {
                using (FileStream fs = new FileStream(strFileName, FileMode.Create, FileAccess.Write))
                {
                    workbook.Write(fs);
                }
            }
            return url;
        }
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值