|
@@ -1,137 +0,0 @@
|
|
|
-using OfficeOpenXml.Style;
|
|
|
-using OfficeOpenXml;
|
|
|
-using System;
|
|
|
-using System.Collections.Generic;
|
|
|
-using System.ComponentModel;
|
|
|
-using System.IO;
|
|
|
-using System.Linq;
|
|
|
-using System.Reflection;
|
|
|
-using System.Text;
|
|
|
-using System.Threading.Tasks;
|
|
|
-using ZhonTai.Admin.Core.Dto;
|
|
|
-
|
|
|
-namespace ZhonTai.Admin.Core.Helpers
|
|
|
-{
|
|
|
- public class ExcelHelper
|
|
|
- {
|
|
|
- //public static void DownloadExcel<T>(List<T> list, string sWebRootFolder, string FileName, bool isFirstCloumnName = true)
|
|
|
- //{
|
|
|
- // try
|
|
|
- // {
|
|
|
- // //判断文件是否存在,不存在创建
|
|
|
- // if (!Directory.Exists(sWebRootFolder))
|
|
|
- // Directory.CreateDirectory(sWebRootFolder);
|
|
|
-
|
|
|
- // //创建默认文件
|
|
|
- // if (FileName.IsNull())
|
|
|
- // {
|
|
|
- // FileName = "Sheet1.xlsx";
|
|
|
- // }
|
|
|
- // if (!FileName.EndsWith(".xlsx"))
|
|
|
- // {
|
|
|
- // FileName += ".xlsx";
|
|
|
- // }
|
|
|
- // //判断同名文件
|
|
|
- // string filepath = Path.Combine(sWebRootFolder, FileName);
|
|
|
- // FileInfo file = new FileInfo(filepath);
|
|
|
- // if (file.Exists)
|
|
|
- // {
|
|
|
- // file.Delete();
|
|
|
- // }
|
|
|
- // using (ExcelPackage package = new ExcelPackage(file))
|
|
|
- // {
|
|
|
- // //添加 workesheet
|
|
|
- // ExcelWorksheet worksheet = package.Workbook.Worksheets.Add(FileName);
|
|
|
-
|
|
|
- // //添加数据
|
|
|
- // PropertyInfo[] props = typeof(T).GetProperties();
|
|
|
- // for (int i = 0; i < list.Count; i++)
|
|
|
- // {
|
|
|
- // worksheet.Row(i + 1).CustomHeight = true;//自动调整行高
|
|
|
- // for (int j = 0; j < props.Length; j++)
|
|
|
- // {
|
|
|
- // //设置第一列
|
|
|
- // if (isFirstCloumnName && i == 0)
|
|
|
- // {
|
|
|
- // worksheet.Cells[i + 1, j + 1].Style.Font.Bold = true;
|
|
|
- // //worksheet.Cells[i + 1, j + 1].Style.WrapText = true;
|
|
|
- // }
|
|
|
-
|
|
|
- // worksheet.Cells[1, 1].Style.Font.Size = 12;//字体大小
|
|
|
- // worksheet.Cells[i + 1, j + 1].Style.Font.Name = "微软雅黑";//字体
|
|
|
- // worksheet.Cells[i + 1, j + 1].Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
|
|
|
- // worksheet.Cells[i + 1, j + 1].Style.VerticalAlignment = ExcelVerticalAlignment.Center;
|
|
|
- // //设置单元格内容
|
|
|
- // var v = props[j].GetValue(list[i]);
|
|
|
- // string val = v == null ? "" : v.ToString();
|
|
|
- // worksheet.Cells[i + 1, j + 1].Value = props[j].GetValue(list[i]);
|
|
|
- // }
|
|
|
- // }
|
|
|
- // worksheet.Cells.Style.ShrinkToFit = true;//单元格自动适应大小
|
|
|
- // package.Save();
|
|
|
- // }
|
|
|
- // return ResultOutput.Ok(filepath, "导出成功");
|
|
|
- // }
|
|
|
- // catch (Exception ex)
|
|
|
- // {
|
|
|
- // return ResultOutput.NotOk("导出失败");
|
|
|
- // }
|
|
|
- //}
|
|
|
-
|
|
|
- /// <summary>
|
|
|
- /// 导入
|
|
|
- /// </summary>
|
|
|
- /// <typeparam name="T"></typeparam>
|
|
|
- /// <param name="FileName"></param>
|
|
|
- /// <returns></returns>
|
|
|
- public static IEnumerable<T> LoadFromExcel<T>(string FileName) where T : new()
|
|
|
- {
|
|
|
- FileInfo existingFile = new FileInfo(FileName);
|
|
|
- List<T> resultList = new List<T>();
|
|
|
- Dictionary<string, int> dicHeader = new Dictionary<string, int>();
|
|
|
- using (ExcelPackage package = new ExcelPackage(existingFile))
|
|
|
- {
|
|
|
- ExcelWorksheet worksheet = package.Workbook.Worksheets[1];
|
|
|
- int colstart = worksheet.Dimension.Start.Column;
|
|
|
- int colend = worksheet.Dimension.End.Column;
|
|
|
- int rowstart = worksheet.Dimension.Start.Row;
|
|
|
- int rowend = worksheet.Dimension.End.Row;
|
|
|
-
|
|
|
- for (int i = colstart; i <= colend; i++)
|
|
|
- {
|
|
|
- dicHeader[worksheet.Cells[rowstart, i].Value.ToString()] = i;
|
|
|
- }
|
|
|
-
|
|
|
- List<PropertyInfo> propertyInfosList = new List<PropertyInfo>(typeof(T).GetProperties());
|
|
|
- for (int row = rowstart + 1; row <= rowend; row++)
|
|
|
- {
|
|
|
- T result = new T();
|
|
|
- foreach (PropertyInfo p in propertyInfosList)
|
|
|
- {
|
|
|
- try
|
|
|
- {
|
|
|
- ExcelRange cell = worksheet.Cells[row, dicHeader[p.Name]];
|
|
|
- if (cell.Value == null)
|
|
|
- continue;
|
|
|
- switch (p.PropertyType.Name.ToLower())
|
|
|
- {
|
|
|
- case "string":
|
|
|
- p.SetValue(result, cell.GetValue<string>());
|
|
|
- break;
|
|
|
- default:
|
|
|
- break;
|
|
|
- }
|
|
|
- }
|
|
|
- catch (Exception)
|
|
|
- {
|
|
|
-
|
|
|
- throw;
|
|
|
- }
|
|
|
- }
|
|
|
- resultList.Add(result);
|
|
|
- }
|
|
|
- }
|
|
|
- return resultList;
|
|
|
- }
|
|
|
- }
|
|
|
-}
|