Sfoglia il codice sorgente

点击外呼任务结果查询自定义列表导出报表
NPOI导出方法扩展- 数据源为List<List<string>>类型,表头为string[] 类型

yuqian 8 anni fa
parent
commit
1dbe0c57f0

+ 15 - 0
CallCenterApi/CallCenterApi.Interface/CallCenterApi.Interface/Controllers/callout/CallPlanController.cs

@@ -781,6 +781,10 @@ namespace CallCenterApi.Interface.Controllers.callout
781 781
                 headList2.Add(row["F_TITLE"]?.ToString() ?? "");
782 782
             }
783 783
 
784
+            var headList = new List<string>();
785
+            headList.AddRange(headList1);
786
+            headList.AddRange(headList2);
787
+
784 788
             //查询数据
785 789
             //var sql = " 1=1 ";
786 790
             var sql = "  ";
@@ -875,6 +879,14 @@ namespace CallCenterApi.Interface.Controllers.callout
875 879
                 dataList.Add(itemList);
876 880
             }
877 881
 
882
+            if (filter.IsExport)
883
+            {
884
+                NPOIHelper npoi = new NPOIHelper();
885
+                var msg1 = npoi.ExportToExcel(DateTime.Now.ToString("yyyyMMdd") + task.TaskName + "导出任务结果查询报表", dataList, headList.ToArray());
886
+                return !string.IsNullOrWhiteSpace(msg1) ? Error(msg1) : Success("导出成功");
887
+            }
888
+
889
+
878 890
             List<Dictionary<string, string>> result = new List<Dictionary<string, string>>();
879 891
             for (int i = 0; i < dataList.Count(); i++)
880 892
             {
@@ -1081,6 +1093,9 @@ namespace CallCenterApi.Interface.Controllers.callout
1081 1093
 
1082 1094
         }
1083 1095
 
1096
+
1097
+
1098
+
1084 1099
         #region 问卷回答
1085 1100
         /// <summary>
1086 1101
         /// 

+ 1 - 0
CallCenterApi/CallCenterApi.Interface/CallCenterApi.Interface/Models/Filter/FilterCallPhone.cs

@@ -18,5 +18,6 @@ namespace CallCenterApi.Interface.Models.Filter
18 18
         public int HJJGID { get; set; } = -1;
19 19
         public int YHFKID { get; set; } = -1;
20 20
         public int RequestType { get; set; } = 1;
21
+        public bool IsExport { get; set; } = false;
21 22
     }
22 23
 }

+ 152 - 3
CallCenterCommon/CallCenter.Utility/NPOI/NPOIHelper.cs

@@ -8,6 +8,7 @@ using NPOI.HSSF.UserModel;
8 8
 using NPOI.SS.UserModel;
9 9
 using NPOI.SS.Util;
10 10
 using NPOI.XSSF.UserModel;
11
+using System.Collections.Generic;
11 12
 
12 13
 namespace CallCenter.Utility
13 14
 {
@@ -247,14 +248,14 @@ namespace CallCenter.Utility
247 248
             {
248 249
                 HSSFWorkbook workbook = new HSSFWorkbook();
249 250
                 //ISheet sheet = workbook.CreateSheet("Sheet1");
250
-                
251
+
251 252
                 //数据总数
252 253
                 int count = dt.Rows.Count;
253 254
                 //注意:1、Excel2003及以前版本65536行,256列;  2、Excel2007、2010版本1048576行,16384列
254 255
                 int sheetpage = 50000;   //这里设置5w条
255 256
                 //向上取整
256 257
                 int sheetindexcount = int.Parse(Math.Ceiling(Convert.ToDecimal((double)count / sheetpage)).ToString());
257
-                if(sheetindexcount == 0)
258
+                if (sheetindexcount == 0)
258 259
                     sheetindexcount = 1;
259 260
 
260 261
                 for (int n = 1; n <= sheetindexcount; n++)
@@ -388,11 +389,159 @@ namespace CallCenter.Utility
388 389
                 }
389 390
                 return "";
390 391
             }
391
-            catch(Exception ex)
392
+            catch (Exception ex)
393
+            {
394
+                return "导出失败!" + ex.Message;
395
+            }
396
+        }
397
+
398
+
399
+        /// <summary>
400
+        /// 弹出下载框导出excel => 创建多个Sheet:例如sheet1,sheet2...【数据源为 List<List<string>> 】
401
+        /// </summary>
402
+        /// <param name="Name"></param>
403
+        /// <param name="list"></param>
404
+        /// <returns></returns>
405
+        public string ExportToExcel(string Name, List<List<string>> list, string[] cols = null)
406
+        {
407
+            try
408
+            {
409
+                HSSFWorkbook workbook = new HSSFWorkbook();
410
+                //ISheet sheet = workbook.CreateSheet("Sheet1");
411
+
412
+                //数据总数
413
+                int count = list.Count;
414
+                //注意:1、Excel2003及以前版本65536行,256列;  2、Excel2007、2010版本1048576行,16384列
415
+                int sheetpage = 50000;   //这里设置5w条
416
+                //向上取整
417
+                int sheetindexcount = int.Parse(Math.Ceiling(Convert.ToDecimal((double)count / sheetpage)).ToString());
418
+                if (sheetindexcount == 0)
419
+                    sheetindexcount = 1;
420
+
421
+                for (int n = 1; n <= sheetindexcount; n++)
422
+                {
423
+                    ISheet sheet = workbook.CreateSheet("Sheet" + n);
424
+                    ICellStyle HeadercellStyle = workbook.CreateCellStyle();
425
+                    HeadercellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
426
+                    HeadercellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
427
+                    HeadercellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
428
+                    HeadercellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
429
+                    HeadercellStyle.Alignment = HorizontalAlignment.Center;
430
+                    HeadercellStyle.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.SkyBlue.Index;
431
+                    HeadercellStyle.FillPattern = FillPattern.SolidForeground;
432
+                    HeadercellStyle.FillBackgroundColor = NPOI.HSSF.Util.HSSFColor.SkyBlue.Index;
433
+
434
+                    //字体
435
+                    NPOI.SS.UserModel.IFont headerfont = workbook.CreateFont();
436
+                    headerfont.Boldweight = (short)FontBoldWeight.Bold;
437
+                    headerfont.FontHeightInPoints = 12;
438
+                    HeadercellStyle.SetFont(headerfont);
439
+
440
+
441
+                    //用column name 作为列名
442
+                    int icolIndex = 0;
443
+                    IRow headerRow = sheet.CreateRow(0);
444
+                    foreach (string dc in cols)
445
+                    {
446
+                        ICell cell = headerRow.CreateCell(icolIndex);
447
+                        cell.SetCellValue(dc);
448
+                        cell.CellStyle = HeadercellStyle;
449
+                        icolIndex++;
450
+                    }
451
+
452
+                    ICellStyle cellStyle = workbook.CreateCellStyle();
453
+
454
+                    //为避免日期格式被Excel自动替换,所以设定 format 为 『@』 表示一率当成text來看
455
+                    cellStyle.DataFormat = HSSFDataFormat.GetBuiltinFormat("@");
456
+                    cellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
457
+                    cellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
458
+                    cellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
459
+                    cellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
460
+
461
+
462
+                    NPOI.SS.UserModel.IFont cellfont = workbook.CreateFont();
463
+                    cellfont.Boldweight = (short)FontBoldWeight.Normal;
464
+                    cellStyle.SetFont(cellfont);
465
+                    if (list.Count > 0)
466
+                    {
467
+                        //建立内容行
468
+                        int iRowIndex = 0;
469
+                        #region 修改不用
470
+                        //foreach (DataRow dr in dt.Rows)
471
+                        //{
472
+                        //    int iCellIndex = 0;
473
+                        //    IRow irow = sheet.CreateRow(iRowIndex + 1);
474
+                        //    for (int i = 0; i < dt.Columns.Count; i++)
475
+                        //    {
476
+                        //        string strsj = string.Empty;
477
+                        //        if (dr[i] != null)
478
+                        //        {
479
+                        //            strsj = dr[i].ToString();
480
+                        //        }
481
+                        //        ICell cell = irow.CreateCell(iCellIndex);
482
+                        //        cell.SetCellValue(strsj);
483
+                        //        cell.CellStyle = cellStyle;
484
+                        //        iCellIndex++;
485
+                        //    }
486
+                        //    iRowIndex++;
487
+                        //}
488
+                        #endregion
489
+
490
+                        int startcount = (n - 1) * sheetpage;
491
+                        int endcount = n * sheetpage;
492
+                        if (sheetindexcount == n)
493
+                            endcount = count;
494
+
495
+                        //for (int x = 0; x < dt.Rows.Count; x++)
496
+                        for (int x = startcount; x < endcount; x++)
497
+                        {
498
+                            int iCellIndex = 0;
499
+                            IRow irow = sheet.CreateRow(iRowIndex + 1);
500
+                            for (int i = 0; i < list[iRowIndex].Count; i++)
501
+                            {
502
+                                string strsj = string.Empty;
503
+
504
+                                strsj = list[iRowIndex][i];
505
+
506
+                                ICell cell = irow.CreateCell(iCellIndex);
507
+                                cell.SetCellValue(strsj);
508
+                                cell.CellStyle = cellStyle;
509
+                                iCellIndex++;
510
+                            }
511
+                            iRowIndex++;
512
+                        }
513
+
514
+                        //自适应列宽度
515
+                        for (int i = 0; i < icolIndex; i++)
516
+                        {
517
+                            sheet.AutoSizeColumn(i);
518
+                        }
519
+                    }
520
+                }
521
+                using (MemoryStream ms = new MemoryStream())
522
+                {
523
+                    workbook.Write(ms);
524
+                    HttpContext curContext = HttpContext.Current;
525
+                    // 设置编码和附件格式
526
+                    curContext.Response.ContentType = "application/vnd.ms-excel";
527
+                    curContext.Response.ContentEncoding = Encoding.UTF8;
528
+                    curContext.Response.Charset = "";
529
+                    curContext.Response.AppendHeader("Content-Disposition",
530
+                        "attachment;filename=" + HttpUtility.UrlEncode(Name + ".xls", Encoding.UTF8));
531
+                    curContext.Response.BinaryWrite(ms.GetBuffer());
532
+                    workbook = null;
533
+                    ms.Close();
534
+                    ms.Dispose();
535
+                    curContext.Response.End();
536
+                }
537
+                return "";
538
+            }
539
+            catch (Exception ex)
392 540
             {
393 541
                 return "导出失败!" + ex.Message;
394 542
             }
395 543
         }
544
+
396 545
         /// <summary>
397 546
         /// 创建Sheet
398 547
         /// </summary>