.net layui 批量导出

2019-11-05 来源: 费良 发布在  https://www.cnblogs.com/fl-1998/p/11725637.html

.net开发,前台使用layui框架,后台使用WCF

废话不多,直接上代码

1>文件引用:

admin.css
layui.css
layui.js
jquery.min.js
layerTool.js

2>前台:

 <script type="text/html" id="toptoolbar">
   <button class="layui-btn layui-btn-sm" lay-event="ExportExcel">批量导出</button>
 </script>

----------先来个批量导出的按钮    (ExportExcel)自定义

layui.use(['table', 'form', 'util'], function () {
    var form = layui.form, layer = layui.layer;
      var table = layui.table;
      var field = null;
      var util = layui.util;    
    table.on('toolbar(ListSupplierTable)', function (obj) {
            switch (obj.event) {
                case 'ExportExcel'://批量导出
                    ShowExportExcelWin();
                    break;
            };
        });
 });

----------('table', 'form', 'util')看需求增减

----------(var form = layui.form, layer = layui.layer;    var table = layui.table;    var field = null;    var util = layui.util;)  看需求增减

----------ShowExportExcelWin  //方法名

//打开批量导出弹窗
        var exportOpen;
        function ShowExportExcelWin() {
            var $ = layui.jquery;
            var html = '<div class="layui-form">';
            html += '   <div class="layui-form-item">';
            html += '        <div class="layui-input-inline" style="padding-left:55px;padding-top:20px;">';
            html += '           <input type="radio" name="rdowhere" value="1" title="全部导出" checked><br />';
            html += '           <input type="radio" name="rdowhere" value="2" title="按筛选条件导出" >';
            html += '       </div>';
            html += '   </div>';
            html += '</div>';
            exportOpen = layer.open({
                type: ,
                title: '批量导出',
                area: ['300px', '210px'],//宽高
                btn: ['导出'],
                yes: function () {
                    var FullName = $('#FullName').val();//供应商全称(筛选条件)
                    var SupplierType = $('#SupplierType').val();//供应商类型(筛选条件)
                    var SupplierLevel = $('#SupplierLevel').val();//供应商级别(筛选条件)
                    var LabMallSupplierIsEnable = $('#LabMallSupplierIsEnable').val();//是否启用(筛选条件)
                    var GoodsHomeShow = $('#GoodsHomeShow').val();//是否显示Log(筛选条件)

                    var ExportType = $("input[name='rdowhere']:checked").val();//导出类型(1:全部;2:筛选)

                    layer.load(, { shade: [0.1, '#000'] });//上传loading

                    $.post("ExportExcelToSupplier", { FullName: FullName, SupplierType: SupplierType, SupplierLevel: SupplierLevel, LabMallSupplierIsEnable: LabMallSupplierIsEnable, GoodsHomeShow: GoodsHomeShow, ExportType: ExportType }, function (res) {
                        layer.closeAll('loading');//关闭loading
                        if (res.success) {
                            layer.close(exportOpen);
                            layer.msg("导出成功");
                            location.href = res.payload;
                        } else {
                            layer.alert(res.error.message, { title: '导出失败' });
                        }
                    }, "json");
                },
                content: html
            });
            layui.form.render();
        }

----------导出功能分为全部导出和按照筛选条件导出

----------ExportExcelToSupplier  //接口名

3>接口:

#region 批量导出供应商
        /// <summary>
        /// 批量导出供应商
        /// </summary>
        /// <returns></returns>
        public JsonResult ExportExcelToSupplier()
        {
            LabMallSupplierEntity supplier = new LabMallSupplierEntity();
            supplier.LabMallSupplierIsEnable = -;
            supplier.LabMallSupplierIsDelete = -;

            int ExportType = GetRequestInt("ExportType");
            )
            {
                supplier.FullName = GetRequestString("FullName");
                supplier.SupplierType = GetRequestInt("SupplierType");
                supplier.SupplierLevel = GetRequestInt("SupplierLevel");
                supplier.LabMallSupplierIsEnable = GetRequestInt("LabMallSupplierIsEnable");
                supplier.GoodsHomeShow = GetRequestInt("GoodsHomeShow");
            }

            LabMallSupplierResponse response = Supplier.GetSupplierObj().ExportExcelToSupplier(new LabMallSupplierRequest
            {
                LabMallSupplierDto = supplier
            });
            )
            {
                return Success(response.ResposeData.ToString());
            }
            else
            {
                return Fail(response.ResposeData.ToString());
            }
        }
        #endregion
#region 批量导出供应商(运营)
        public LabMallSupplierResponse ExportExcelToSupplier(LabMallSupplierRequest request)
        {
            SupplierServiceClient obj = new SupplierServiceClient();
            try
            {
                LabMallSupplierResponse response = obj.GetLabMallSupplierList(request);//调用服务端 数据集合

                string FileFolder = AppDomain.CurrentDomain.BaseDirectory + "files\\Export\\Excel\\";
                string FileName = FileFolder + "供应商管理.xls";//文件存放的路径
                string ReturnUrl = "/files/Export/Excel/" + "供应商管理.xls";//需要返回的文件路径
                if (!Directory.Exists(FileFolder))//指定路径没有该文件时创建
                {
                    Directory.CreateDirectory(FileFolder);
                }
                if (File.Exists(FileName))//指定文件存在时删除
                {
                    File.Delete(FileName);
                }
                MemoryStream ms = new MemoryStream();//创建一个流
                IWorkbook workbook = new HSSFWorkbook();//创建workbook
                ISheet sheet = workbook.CreateSheet("供应商");//创建sheet
                IRow row = sheet.CreateRow();//创建row

                ICellStyle style = workbook.CreateCellStyle();//创建单元格样式
                IFont font = workbook.CreateFont();//创建字体
                font.Boldweight = short.MaxValue;//字体宽度
                style.SetFont(font);//添加到样式

                //表头
                string[] colName = { "ID", "简称", "全称", "供应商类型", "供应商级别", "开户银行", "收款账号", "网址", "地址", "座机", "是否启用", "商城显示", "纳税人识别号", "类型", "法定代表人", "注册资本", "成立日期", "经营期限", "经营范围" };
                ; i < colName.Length; i++)
                {
                    row.CreateCell(i).SetCellValue(colName[i]);//创建列
                    row.Cells[i].CellStyle = style;//字体加粗
                }

                List<LabMallSupplierEntity> list = response.LabMallSupplierDtos;
                )
                {
                    return new LabMallSupplierResponse
                    {
                        Code = ,
                        ResposeData = "没有数据"
                    };
                }
                ; i < list.Count; i++)
                {
                    LabMallSupplierEntity supplier = list[i];
                    IRow rows = sheet.CreateRow(i + );
                    rows.CreateCell().SetCellValue(supplier.LabMallSupplierID);
                    rows.CreateCell().SetCellValue(supplier.Name == null ? "" : supplier.Name.Trim());
                    rows.CreateCell().SetCellValue(supplier.FullName == null ? "" : supplier.FullName.Trim());
                    rows.CreateCell().SetCellValue(supplier.SupplierType ==  ? "直销" : "经销");
                    rows.CreateCell().SetCellValue(supplier.SupplierLevel ==  ? "合约供应商" : "普通供应商");
                    rows.CreateCell().SetCellValue(supplier.BankName == null ? "" : supplier.BankName.Trim());
                    rows.CreateCell().SetCellValue(supplier.BankCardNumber == null ? "" : supplier.BankCardNumber.Trim());
                    rows.CreateCell().SetCellValue(supplier.WebSite == null ? "" : supplier.WebSite.Trim());
                    rows.CreateCell().SetCellValue(supplier.Address == null ? "" : supplier.Address.Trim());
                    rows.CreateCell().SetCellValue(supplier.Phone == null ? "" : supplier.Phone.Trim());
                    rows.CreateCell().SetCellValue(supplier.LabMallSupplierIsEnable ==  ? "禁用" : "启用");
                    rows.CreateCell().SetCellValue(supplier.GoodsHomeShow ==  ? "显示" : "隐藏");
                    rows.CreateCell().SetCellValue(supplier.IdentityNum == null ? "" : supplier.IdentityNum.Trim());
                    rows.CreateCell().SetCellValue(supplier.Type == null ? "" : supplier.Type.Trim());
                    rows.CreateCell().SetCellValue(supplier.Representative == null ? "" : supplier.Representative.Trim());
                    rows.CreateCell().SetCellValue(supplier.RegisterCapital == null ? "" : supplier.RegisterCapital.Trim());
                    rows.CreateCell().SetCellValue(supplier.BirthDate.GetString() == null ? "" : supplier.BirthDate.GetString());
                    rows.CreateCell().SetCellValue(supplier.EndDate.GetString() == null ? "" : supplier.EndDate.GetString());
                    rows.CreateCell().SetCellValue(supplier.RunScope == null ? "" : supplier.RunScope.Trim());
                }
                workbook.Write(ms);
                ms.Flush();
                ms.Position = ;

                using (FileStream fs=new FileStream(FileName,FileMode.Create,FileAccess.Write))
                {
                    byte[] data = ms.ToArray();
                    fs.Write(data, , data.Length);
                    fs.Flush();
                    data = null;
                }
                response.Code = ;
                response.ResposeData = ReturnUrl;
                return response;
            }
            catch (Exception ex)
            {
                LogHelp.Error(ex, request.LabMallSupplierDto);
                return new LabMallSupplierResponse
                {
                    Code = ,
                    ResposeData = ex.Message
                };
            }
            finally
            {
                obj.Close();
            }
        }
        #endregion

相关文章