• NPOI Excel 导出学习 一 (基础导出)


    代码 

      1   public static class ExcelReportHelper
      2     {
      3         /// <summary>
      4         /// 将列表导出到Excel中
      5         /// </summary>
      6         /// <typeparam name="T"></typeparam>
      7         /// <param name="listData"></param>
      8         /// <param name="sheetName"></param>
      9         /// <param name="title"></param>
     10         /// <param name="description"></param>
     11         /// <param name="saveTo"></param>
     12         /// <returns></returns>
     13         public static XSSFWorkbook ExportToExcelSheet<T>(List<T> listData, string sheetName, XSSFWorkbook hssfWorkbook)
     14         {
     15             try
     16             {
     17                 int propertyCount = getPropertyCount(typeof(T));
     18                 var sheet1 = hssfWorkbook.CreateSheet(sheetName);
     19                 var row1 = (XSSFRow)sheet1.CreateRow(0);
     20                 WriteHeader(typeof(T), row1);
     21 
     22                 int i = 0;
     23                 foreach (var item in listData)
     24                 {
     25                     int rowIndex = i;
     26                     var rowData = (XSSFRow)sheet1.CreateRow(rowIndex + 1);
     27                     WriteData(item, typeof(T), rowData);
     28                     i++;
     29                 }
     30                 //setAutoColumn(sheet1, i);
     31                 return hssfWorkbook;
     32             }
     33             catch (Exception e)
     34             {
     35                 return null;
     36             }
     37         }
     38         private static int getPropertyCount(Type type)
     39         {
     40             if (type != null)
     41             {
     42                 Type t = type;
     43                 PropertyInfo[] propertyInfo = t.GetProperties();
     44 
     45                 int i = 0;
     46                 foreach (PropertyInfo propInfo in propertyInfo)
     47                 {
     48                     object[] objAttrs = propInfo.GetCustomAttributes(typeof(DisplayNameAttribute), true);
     49 
     50                     if (objAttrs.Length > 0)
     51                     {
     52                         i++;
     53                     }
     54 
     55                 }
     56                 return i;
     57             }
     58             return 0;
     59         }
     60 
     61         /// <summary>
     62         /// 写表头
     63         /// </summary>
     64         /// <param name="type"></param>
     65         /// <param name="row"> </param>
     66         /// <param name="style"> </param>
     67         public static void WriteHeader(Type type, XSSFRow row)
     68         {
     69             if (type != null)
     70             {
     71                 Type t = type;
     72                 PropertyInfo[] propertyInfo = t.GetProperties();
     73                 int i = 0;
     74                 foreach (PropertyInfo propInfo in propertyInfo)
     75                 {
     76                     var cell = row.CreateCell(i);
     77                     object[] objAttrs = propInfo.GetCustomAttributes(typeof(DisplayNameAttribute), true);
     78 
     79                     if (objAttrs.Length > 0)
     80                     {
     81                         var attr = objAttrs[0] as DisplayNameAttribute;
     82                         cell.SetCellValue(attr != null ? attr.DisplayName : "");
     83                         i++;
     84                     }
     85                     //cell.CellStyle = style;
     86 
     87                 }
     88 
     89             }
     90         }
     91 
     92         public static void WriteData<T>(T obj, Type type, XSSFRow row)
     93         {
     94             if (obj != null)
     95             {
     96                 Type t = type;
     97                 PropertyInfo[] propertyInfo = t.GetProperties();
     98                 int i = 0;
     99                 foreach (PropertyInfo propInfo in propertyInfo)
    100                 {
    101                     object[] objAttrs = propInfo.GetCustomAttributes(typeof(DisplayNameAttribute), true);
    102 
    103                     if (objAttrs.Length > 0)
    104                     {
    105                         var cell = row.CreateCell(i);
    106                         object value = propInfo.GetValue(obj, null);
    107                         if (value != null)
    108                         {
    109                             if (propInfo.PropertyType == typeof(int))
    110                             {
    111 
    112                                 cell.SetCellValue((int)value);
    113                             }
    114                             else if(propInfo.PropertyType == typeof(decimal?)|| propInfo.PropertyType == typeof(decimal))
    115                             {
    116                                 var values = (decimal)value;
    117                                 var value1=Decimal.ToInt32(values);
    118                                 cell.SetCellValue(value1);
    119                             }
    120                             else if (propInfo.PropertyType == typeof(DateTime?) || propInfo.PropertyType == typeof(DateTime))
    121                             {
    122                                 var values = (DateTime)value;
    123                                 cell.SetCellValue(values.ToString("yyyy/MM/dd hh:mm:ss"));
    124                             }
    125                             else
    126                             {
    127                                 cell.SetCellValue(value.ToString());
    128                             }
    129                         }
    130 
    131                         i++;
    132                     }
    133                 }
    134 
    135             }
    136         }
    137        
    138     }
  • 相关阅读:
    因子个数筛
    原根
    Pollard Rho (大数分解算法)
    Miller-Rabin(素数测试算法)
    离不开的微服务架构,脱不开的RPC细节(值得收藏)!!!
    微服务架构,多“微”才合适?
    互联网架构,究竟为啥要做服务化?
    markdown
    docker安装、启动(挂载外部配置和数据)
    程序员代码面试指南上(1-3)
  • 原文地址:https://www.cnblogs.com/luoye-chenxin/p/9266728.html
Copyright © 2020-2023  润新知