.net layui 批量導出

.net開發,前臺使用layui框架,後臺使用WCFcss

廢話很少,直接上代碼html

1>文件引用:jquery

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

2>前臺:json

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

----------先來個批量導出的按鈕    (ExportExcel)自定義api

 

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;)  看需求增減post

----------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: 1,
                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(2, { 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();
        }

----------導出功能分爲所有導出和按照篩選條件導出ui

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

3>接口:

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

            int ExportType = GetRequestInt("ExportType");
            if (ExportType == 2)
            {
                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
            });
            if (response.Code == 1)
            {
                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(0);//建立row

                ICellStyle style = workbook.CreateCellStyle();//建立單元格樣式
                IFont font = workbook.CreateFont();//建立字體
                font.Boldweight = short.MaxValue;//字體寬度
                style.SetFont(font);//添加到樣式

                //表頭
                string[] colName = { "ID", "簡稱", "全稱", "供應商類型", "供應商級別", "開戶銀行", "收款帳號", "網址", "地址", "座機", "是否啓用", "商城顯示", "納稅人識別號", "類型", "法定表明人", "註冊資本", "成立日期", "經營期限", "經營範圍" };
                for (int i = 0; i < colName.Length; i++)
                {
                    row.CreateCell(i).SetCellValue(colName[i]);//建立列
                    row.Cells[i].CellStyle = style;//字體加粗
                }

                List<LabMallSupplierEntity> list = response.LabMallSupplierDtos;
                if (list.Count==0)
                {
                    return new LabMallSupplierResponse
                    {
                        Code = 2,
                        ResposeData = "沒有數據"
                    };
                }
                for (int i = 0; i < list.Count; i++)
                {
                    LabMallSupplierEntity supplier = list[i];
                    IRow rows = sheet.CreateRow(i + 1);
                    rows.CreateCell(0).SetCellValue(supplier.LabMallSupplierID);
                    rows.CreateCell(1).SetCellValue(supplier.Name == null ? "" : supplier.Name.Trim());
                    rows.CreateCell(2).SetCellValue(supplier.FullName == null ? "" : supplier.FullName.Trim());
                    rows.CreateCell(3).SetCellValue(supplier.SupplierType == 1 ? "直銷" : "經銷");
                    rows.CreateCell(4).SetCellValue(supplier.SupplierLevel == 1 ? "合約供應商" : "普通供應商");
                    rows.CreateCell(5).SetCellValue(supplier.BankName == null ? "" : supplier.BankName.Trim());
                    rows.CreateCell(6).SetCellValue(supplier.BankCardNumber == null ? "" : supplier.BankCardNumber.Trim());
                    rows.CreateCell(7).SetCellValue(supplier.WebSite == null ? "" : supplier.WebSite.Trim());
                    rows.CreateCell(8).SetCellValue(supplier.Address == null ? "" : supplier.Address.Trim());
                    rows.CreateCell(9).SetCellValue(supplier.Phone == null ? "" : supplier.Phone.Trim());
                    rows.CreateCell(10).SetCellValue(supplier.LabMallSupplierIsEnable == 0 ? "禁用" : "啓用");
                    rows.CreateCell(11).SetCellValue(supplier.GoodsHomeShow == 1 ? "顯示" : "隱藏");
                    rows.CreateCell(12).SetCellValue(supplier.IdentityNum == null ? "" : supplier.IdentityNum.Trim());
                    rows.CreateCell(13).SetCellValue(supplier.Type == null ? "" : supplier.Type.Trim());
                    rows.CreateCell(14).SetCellValue(supplier.Representative == null ? "" : supplier.Representative.Trim());
                    rows.CreateCell(15).SetCellValue(supplier.RegisterCapital == null ? "" : supplier.RegisterCapital.Trim());
                    rows.CreateCell(16).SetCellValue(supplier.BirthDate.GetString() == null ? "" : supplier.BirthDate.GetString());
                    rows.CreateCell(17).SetCellValue(supplier.EndDate.GetString() == null ? "" : supplier.EndDate.GetString());
                    rows.CreateCell(18).SetCellValue(supplier.RunScope == null ? "" : supplier.RunScope.Trim());
                }
                workbook.Write(ms);
                ms.Flush();
                ms.Position = 0;

                using (FileStream fs=new FileStream(FileName,FileMode.Create,FileAccess.Write))
                {
                    byte[] data = ms.ToArray();
                    fs.Write(data, 0, data.Length);
                    fs.Flush();
                    data = null;
                }
                response.Code = 1;
                response.ResposeData = ReturnUrl;
                return response;
            }
            catch (Exception ex)
            {
                LogHelp.Error(ex, request.LabMallSupplierDto);
                return new LabMallSupplierResponse
                {
                    Code = 3,
                    ResposeData = ex.Message
                };
            }
            finally
            {
                obj.Close();
            }
        }
        #endregion
相關文章
相關標籤/搜索