乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      .Net Core 使用 NPOI 導(dǎo)入Excel

       路人甲Java 2020-05-02

      由于之前在網(wǎng)上查閱一些資料發(fā)現(xiàn)總是不能編譯通過,不能正常使用,現(xiàn)把能正常使用的代碼貼出:

      /// <summary>
         /// Excel導(dǎo)入幫助類
         /// </summary>
          public class ImportExcelUtil<T> where T : new()
          {
              //合法文件擴(kuò)展名
              private static List<string> extName = new List<string>() { ".xls", ".xlsx" };
              /// <summary>
              /// 導(dǎo)入Excel內(nèi)容讀取到List<T>/// </summary>
              /// <param name="file">導(dǎo)入Execl文件</param>
              /// <param name="sheetName">指定讀取excel工作薄sheet的名稱</param>
              /// <returns>List<T></returns>
              public static List<T> InputExcel(IFormFile file, string sheetName = null)
              {
                  //獲取文件后綴名
                  string type = Path.GetExtension(file.FileName);
                  //判斷是否導(dǎo)入合法文件
                  if(!extName.Contains(type))
                  {
                      return null;
                  }
                  //轉(zhuǎn)成為文件流
                  MemoryStream ms = new MemoryStream();
                  file.CopyTo(ms);
                  ms.Seek(0, SeekOrigin.Begin);
                  //實(shí)例化T數(shù)組
                  List<T> list = new List<T>();
                  //獲取數(shù)據(jù)
                  list = InputExcel(ms, sheetName);
                  return list;
              }
      
              /// <summary>
              /// 將Excel文件內(nèi)容讀取到List<T>/// </summary>
              /// <param name="fileName">文件完整路徑名</param>
              /// <param name="sheetName">指定讀取excel工作薄sheet的名稱</param>
              /// <param name="isFirstRowColumn">第一行是否是DataTable的列名:true=是,false=否</param>
              /// <returns>List<T></returns>
              public static List<T> InputExcel(string fileName, string sheetName = null)
              {
                  if (!File.Exists(fileName))
                  {
                      return null;
                  }
                  //根據(jù)指定路徑讀取文件
                  FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
                  //實(shí)例化T數(shù)組
                  List<T> list = new List<T>();
                  //獲取數(shù)據(jù)
                  list = InputExcel(fs, sheetName);
      
                  return list;
              }
      
              /// <summary>
              /// 將Excel文件內(nèi)容讀取到List<T>/// </summary>
              /// <param name="fileStream">文件流</param>
              /// <param name="sheetName">指定讀取excel工作薄sheet的名稱</param>
              /// <returns>List<T></returns>
              private static List<T> InputExcel(Stream fileStream, string sheetName = null)
              {
                  //創(chuàng)建Excel數(shù)據(jù)結(jié)構(gòu)
                  IWorkbook workbook = WorkbookFactory.Create(fileStream);
                  //如果有指定工作表名稱
                  ISheet sheet = null;
                  if (!string.IsNullOrEmpty(sheetName))
                  {
                      sheet = workbook.GetSheet(sheetName);
                      //如果沒有找到指定的sheetName對應(yīng)的sheet,則嘗試獲取第一個(gè)sheet
                      if (sheet == null)
                      {
                          sheet = workbook.GetSheetAt(0);
                      }
                  }
                  else
                  {
                      //如果沒有指定的sheetName,則嘗試獲取第一個(gè)sheet
                      sheet = workbook.GetSheetAt(0);
                  }
                  //實(shí)例化T數(shù)組
                  List<T> list = new List<T>();
                  if (sheet != null)
                  {
                      //一行最后一個(gè)cell的編號 即總的列數(shù)
                      IRow cellNum = sheet.GetRow(0);
                      int num = cellNum.LastCellNum;
                      //獲取泛型對象T的所有屬性
                      var propertys = typeof(T).GetProperties();
                      //每行轉(zhuǎn)換為單個(gè)T對象
                      for (int i = 1; i <= sheet.LastRowNum; i++)
                      {
                          IRow row = sheet.GetRow(i);
                          var obj = new T();
                          for (int j = 0; j < num; j++)
                          {
                              //沒有數(shù)據(jù)的單元格都默認(rèn)是null
                              ICell cell = row.GetCell(j);
                              if (cell != null)
                              {
                                  var value = row.GetCell(j).ToString();
                                  string str = (propertys[j].PropertyType).FullName;
                                  if (str == "System.String")
                                  {
                                      propertys[j].SetValue(obj, value, null);
                                  }
                                  else if (str == "System.DateTime")
                                  {
                                      DateTime pdt = Convert.ToDateTime(value, CultureInfo.InvariantCulture);
                                      propertys[j].SetValue(obj, pdt, null);
                                  }
                                  else if (str == "System.Boolean")
                                  {
                                      bool pb = Convert.ToBoolean(value);
                                      propertys[j].SetValue(obj, pb, null);
                                  }
                                  else if (str == "System.Int16")
                                  {
                                      short pi16 = Convert.ToInt16(value);
                                      propertys[j].SetValue(obj, pi16, null);
                                  }
                                  else if (str == "System.Int32")
                                  {
                                      int pi32 = Convert.ToInt32(value);
                                      propertys[j].SetValue(obj, pi32, null);
                                  }
                                  else if (str == "System.Int64")
                                  {
                                      long pi64 = Convert.ToInt64(value);
                                      propertys[j].SetValue(obj, pi64, null);
                                  }
                                  else if (str == "System.Byte")
                                  {
                                      byte pb = Convert.ToByte(value);
                                      propertys[j].SetValue(obj, pb, null);
                                  }
                                  else
                                  {
                                      propertys[j].SetValue(obj, null, null);
                                  }
                              }
                          }
                          list.Add(obj);
                      }
                  }
                  return list;
              }
      
          }
      View Code

      如使用有問題,請留言。希望能幫到你~~~

        本站是提供個(gè)人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點(diǎn)擊一鍵舉報(bào)。
        轉(zhuǎn)藏 分享 獻(xiàn)花(0

        0條評論

        發(fā)表

        請遵守用戶 評論公約

        類似文章 更多