mengjie лет назад: 8
Родитель
Сommit
fdfd607eb7
1 измененных файлов с 142 добавлено и 0 удалено
  1. 142 0
      codegit/CallCenterCommon/CallCenter.Utility/NPOI/NPOIHelper.cs

+ 142 - 0
codegit/CallCenterCommon/CallCenter.Utility/NPOI/NPOIHelper.cs

@@ -358,5 +358,147 @@ namespace CallCenter.Utility
358 358
 
359 359
             return dt;
360 360
         }
361
+
362
+        //2017-11-11数据第一行为标题
363
+        #region
364
+        /// <summary>
365
+        /// 弹出下载框导出excel(数据第一行为标题)
366
+        /// </summary>
367
+        /// <param name="Name"></param>
368
+        /// <param name="dt"></param>
369
+        /// <returns></returns>
370
+        public string ExportToExcel2(string Name, DataTable dt, string[] cols = null)
371
+        {
372
+            try
373
+            {
374
+                if (dt.Rows.Count > 0)
375
+                {
376
+                    HSSFWorkbook workbook = new HSSFWorkbook();
377
+                    ISheet sheet = workbook.CreateSheet("Sheet1");
378
+
379
+                    ICellStyle HeadercellStyle = workbook.CreateCellStyle();
380
+                    HeadercellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
381
+                    HeadercellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
382
+                    HeadercellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
383
+                    HeadercellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
384
+                    HeadercellStyle.Alignment = HorizontalAlignment.Center;
385
+                    HeadercellStyle.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.SkyBlue.Index;
386
+                    HeadercellStyle.FillPattern = FillPattern.SolidForeground;
387
+                    HeadercellStyle.FillBackgroundColor = NPOI.HSSF.Util.HSSFColor.SkyBlue.Index;
388
+
389
+                    //字体
390
+                    NPOI.SS.UserModel.IFont headerfont = workbook.CreateFont();
391
+                    headerfont.Boldweight = (short)FontBoldWeight.Bold;
392
+                    headerfont.FontHeightInPoints = 12;
393
+                    HeadercellStyle.SetFont(headerfont);
394
+
395
+
396
+                    //用column name 作为列名
397
+                    int icolIndex = 0;
398
+                    IRow headerRow = sheet.CreateRow(0);
399
+                    if (cols == null || (cols != null && cols.Length == 0))
400
+                    {
401
+                        //foreach (DataColumn dc in dt.Columns)
402
+                        //{
403
+                        //    ICell cell = headerRow.CreateCell(icolIndex);
404
+                        //    cell.SetCellValue(dc.ColumnName);
405
+                        //    cell.CellStyle = HeadercellStyle;
406
+                        //    icolIndex++;
407
+                        //}
408
+                        for (int i = 0; i < dt.Columns.Count; i++)
409
+                        {
410
+                            ICell cell = headerRow.CreateCell(icolIndex);
411
+                            cell.SetCellValue(dt.Rows [0][i].ToString());
412
+                            cell.CellStyle = HeadercellStyle;
413
+                            icolIndex++;
414
+                        }
415
+                    }
416
+                    else
417
+                    {
418
+                        foreach (string dc in cols)
419
+                        {
420
+                            ICell cell = headerRow.CreateCell(icolIndex);
421
+                            cell.SetCellValue(dc);
422
+                            cell.CellStyle = HeadercellStyle;
423
+                            icolIndex++;
424
+                        }
425
+                    }
426
+                    ICellStyle cellStyle = workbook.CreateCellStyle();
427
+
428
+                    //为避免日期格式被Excel自动替换,所以设定 format 为 『@』 表示一率当成text來看
429
+                    cellStyle.DataFormat = HSSFDataFormat.GetBuiltinFormat("@");
430
+                    cellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
431
+                    cellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
432
+                    cellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
433
+                    cellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
434
+
435
+
436
+                    NPOI.SS.UserModel.IFont cellfont = workbook.CreateFont();
437
+                    cellfont.Boldweight = (short)FontBoldWeight.Normal;
438
+                    cellStyle.SetFont(cellfont);
439
+
440
+                    //建立内容行
441
+                    int iRowIndex = 0;
442
+                    foreach (DataRow dr in dt.Rows)
443
+                    {
444
+                        if (iRowIndex > 0)
445
+                        {
446
+
447
+
448
+                            int iCellIndex = 0;
449
+                            IRow irow = sheet.CreateRow(iRowIndex );
450
+                            for (int i = 0; i < dt.Columns.Count; i++)
451
+                            {
452
+                                string strsj = string.Empty;
453
+                                if (dr[i] != null)
454
+                                {
455
+                                    strsj = dr[i].ToString();
456
+                                }
457
+                                ICell cell = irow.CreateCell(iCellIndex);
458
+                                cell.SetCellValue(strsj);
459
+                                cell.CellStyle = cellStyle;
460
+                                iCellIndex++;
461
+                            }
462
+                        }
463
+                        iRowIndex++;
464
+                    }
465
+
466
+                    //自适应列宽度
467
+                    for (int i = 0; i < icolIndex; i++)
468
+                    {
469
+                        sheet.AutoSizeColumn(i);
470
+                    }
471
+
472
+                    using (MemoryStream ms = new MemoryStream())
473
+                    {
474
+                        workbook.Write(ms);
475
+
476
+                        HttpContext curContext = HttpContext.Current;
477
+
478
+
479
+                        // 设置编码和附件格式
480
+                        curContext.Response.ContentType = "application/vnd.ms-excel";
481
+                        curContext.Response.ContentEncoding = Encoding.UTF8;
482
+                        curContext.Response.Charset = "";
483
+                        curContext.Response.AppendHeader("Content-Disposition",
484
+                            "attachment;filename=" + HttpUtility.UrlEncode(Name + ".xls", Encoding.UTF8));
485
+
486
+                        curContext.Response.BinaryWrite(ms.GetBuffer());
487
+
488
+                        workbook = null;
489
+                        ms.Close();
490
+                        ms.Dispose();
491
+
492
+                        curContext.Response.End();
493
+                    }
494
+                }
495
+                return "";
496
+            }
497
+            catch
498
+            {
499
+                return "导出失败!";
500
+            }
501
+        }
502
+        #endregion
361 503
     }
362 504
 }