升级excel操作
This commit is contained in:
@@ -1,60 +1,222 @@
|
||||
using System;
|
||||
using OfficeOpenXml;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using OfficeOpenXml;
|
||||
|
||||
namespace Yi.Framework.Common.Helper
|
||||
{
|
||||
public class ExcelHelper
|
||||
{
|
||||
/// <summary>
|
||||
///
|
||||
/// 导出Excel
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="dataList">数据</param>
|
||||
/// <param name="headers">表头</param>
|
||||
/// <returns></returns>
|
||||
public static string CreateExcelFromList<T>(List<T> dataList, List<string> headers,string evn)
|
||||
/// <param name="list"></param>
|
||||
/// <param name="sheetName"></param>
|
||||
/// <param name="fileName"></param>
|
||||
public static string ExportExcel<T>(List<T> list, string sheetName, string fileName,string path)
|
||||
{
|
||||
string sFileName = $"{fileName}.xlsx";
|
||||
string newFileName = Path.Combine(path, sFileName);
|
||||
//调试模式需要加上
|
||||
ExcelPackage.LicenseContext = LicenseContext.NonCommercial;
|
||||
string sWebRootFolder = Path.Combine($"{evn}", "wwwroot/Excel");//如果用浏览器url下载的方式 存放excel的文件夹一定要建在网站首页的同级目录下!!!
|
||||
if (!Directory.Exists(sWebRootFolder))
|
||||
Directory.CreateDirectory(Path.GetDirectoryName(newFileName)!);
|
||||
using (ExcelPackage package = new(new FileInfo(newFileName)))
|
||||
{
|
||||
Directory.CreateDirectory(sWebRootFolder);
|
||||
}
|
||||
string sFileName = $@"Excel_{DateTime.Now.ToString("yyyyMMddHHmmss")}.xlsx";
|
||||
var path = Path.Combine(sWebRootFolder, sFileName);
|
||||
FileInfo file = new FileInfo(path);
|
||||
if (file.Exists)
|
||||
{
|
||||
file.Delete();
|
||||
file = new FileInfo(path);
|
||||
}
|
||||
using (ExcelPackage package = new(file))
|
||||
{
|
||||
//创建sheet
|
||||
ExcelWorksheet worksheet = package.Workbook.Worksheets.Add("sheet1");
|
||||
worksheet.Cells.LoadFromCollection(dataList, true);
|
||||
//表头字段
|
||||
for (int i = 0; i < headers.Count; i++)
|
||||
{
|
||||
worksheet.Cells[1, i + 1].Value = headers[i];
|
||||
}
|
||||
for (int i = 0; i < headers.Count + 1; i++)
|
||||
{//删除不需要的列
|
||||
string aa = worksheet.Cells[1, i + 1].Value.ToString();
|
||||
if (aa == "总行数")
|
||||
{
|
||||
worksheet.DeleteColumn(i + 1);
|
||||
}
|
||||
}
|
||||
// 添加worksheet
|
||||
ExcelWorksheet worksheet = package.Workbook.Worksheets.Add(sheetName);
|
||||
//单元格自动适应大小
|
||||
worksheet.Cells.Style.ShrinkToFit = true;
|
||||
//全部字段导出
|
||||
worksheet.Cells.LoadFromCollection(list, true, OfficeOpenXml.Table.TableStyles.Light13);
|
||||
package.Save();
|
||||
}
|
||||
//return path;//这是返回文件的方式
|
||||
return sFileName; //如果用浏览器url下载的方式 这里直接返回生成的文件名就可以了
|
||||
|
||||
return newFileName;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 下载导入模板
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="list"></param>
|
||||
/// <param name="stream"></param>
|
||||
/// <param name="fileName">下载文件名</param>
|
||||
/// <returns></returns>
|
||||
protected string DownloadImportTemplate<T>(List<T> list, Stream stream, string fileName,string path)
|
||||
{
|
||||
string sFileName = $"{fileName}.xlsx";
|
||||
string newFileName = Path.Combine(path, sFileName);
|
||||
//调试模式需要加上
|
||||
ExcelPackage.LicenseContext = LicenseContext.NonCommercial;
|
||||
if (!Directory.Exists(newFileName))
|
||||
{
|
||||
Directory.CreateDirectory(Path.GetDirectoryName(newFileName)!);
|
||||
}
|
||||
using (ExcelPackage package = new(new FileInfo(newFileName)))
|
||||
{
|
||||
// 添加worksheet
|
||||
ExcelWorksheet worksheet = package.Workbook.Worksheets.Add(fileName);
|
||||
//单元格自动适应大小
|
||||
worksheet.Cells.Style.ShrinkToFit = true;
|
||||
//全部字段导出
|
||||
worksheet.Cells.LoadFromCollection(list, true, OfficeOpenXml.Table.TableStyles.Light13);
|
||||
package.SaveAs(stream);
|
||||
}
|
||||
|
||||
return sFileName;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 导入数据
|
||||
/// </summary>
|
||||
/// <param name="stream"></param>
|
||||
/// <returns></returns>
|
||||
public static List<T> ImportData<T>(Stream stream) where T : new()
|
||||
{
|
||||
using ExcelPackage package = new(stream);
|
||||
ExcelPackage.LicenseContext = LicenseContext.NonCommercial;
|
||||
ExcelWorksheet worksheet = package.Workbook.Worksheets[0];//读取第1个sheet
|
||||
//获取表格的列数和行数
|
||||
|
||||
int colStart = worksheet.Dimension.Start.Column;
|
||||
int colEnd = worksheet.Dimension.End.Column;
|
||||
int rowStart = worksheet.Dimension.Start.Row;
|
||||
int rowEnd = worksheet.Dimension.End.Row;
|
||||
//int rowCount = worksheet.Dimension.Rows;
|
||||
//int ColCount = worksheet.Dimension.Columns;
|
||||
|
||||
List<T> resultList = new();
|
||||
List<PropertyInfo> propertyInfos = new();// new(typeof(T).GetProperties());
|
||||
Dictionary<string, int> dictHeader = new();
|
||||
for (int i = colStart; i < colEnd; i++)
|
||||
{
|
||||
var name = worksheet.Cells[rowStart, i].Value.ToString();
|
||||
dictHeader[name!] = i;
|
||||
|
||||
PropertyInfo propertyInfo = MapPropertyInfo<T>(name!);
|
||||
if (propertyInfo != null)
|
||||
{
|
||||
propertyInfos.Add(propertyInfo);
|
||||
}
|
||||
}
|
||||
for (int row = rowStart + 1; row <= rowEnd; row++)
|
||||
{
|
||||
T result = new();
|
||||
|
||||
foreach (PropertyInfo p in propertyInfos)
|
||||
{
|
||||
try
|
||||
{
|
||||
ExcelRange cell = worksheet.Cells[row, dictHeader[p.Name]];
|
||||
if (cell.Value == null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
switch (p.PropertyType.Name.ToLower())
|
||||
{
|
||||
case "string":
|
||||
p.SetValue(result, cell.GetValue<string>());
|
||||
break;
|
||||
case "int16":
|
||||
p.SetValue(result, cell.GetValue<short>()); break;
|
||||
case "int32":
|
||||
p.SetValue(result, cell.GetValue<int>()); break;
|
||||
case "int64":
|
||||
p.SetValue(result, cell.GetValue<long>()); break;
|
||||
case "decimal":
|
||||
p.SetValue(result, cell.GetValue<decimal>());
|
||||
break;
|
||||
case "double":
|
||||
p.SetValue(result, cell.GetValue<double>()); break;
|
||||
case "datetime":
|
||||
p.SetValue(result, cell.GetValue<DateTime>()); break;
|
||||
case "boolean":
|
||||
p.SetValue(result, cell.GetValue<bool>()); break;
|
||||
case "char":
|
||||
p.SetValue(result, cell.GetValue<string>()); break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
catch (KeyNotFoundException ex)
|
||||
{
|
||||
Console.WriteLine("未找到该列将继续循环," + ex.Message);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
resultList.Add(result);
|
||||
}
|
||||
|
||||
return resultList.ToList();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 查找Excel列名对应的实体属性
|
||||
/// </summary>
|
||||
/// <param name="columnName"></param>
|
||||
/// <returns></returns>
|
||||
private static PropertyInfo MapPropertyInfo<T>(string columnName)
|
||||
{
|
||||
PropertyInfo[] propertyList = GetProperties(typeof(T));
|
||||
PropertyInfo propertyInfo = propertyList.Where(p => p.Name == columnName).FirstOrDefault()!;
|
||||
if (propertyInfo != null)
|
||||
{
|
||||
return propertyInfo;
|
||||
}
|
||||
else
|
||||
{
|
||||
foreach (PropertyInfo tempPropertyInfo in propertyList)
|
||||
{
|
||||
System.ComponentModel.DescriptionAttribute[] attributes = (System.ComponentModel.DescriptionAttribute[])tempPropertyInfo.GetCustomAttributes(typeof(System.ComponentModel.DescriptionAttribute), false);
|
||||
if (attributes.Length > 0)
|
||||
{
|
||||
if (attributes[0].Description == columnName)
|
||||
{
|
||||
return tempPropertyInfo;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return null!;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 得到类里面的属性集合
|
||||
/// </summary>
|
||||
/// <param name="type"></param>
|
||||
/// <param name="columns"></param>
|
||||
/// <returns></returns>
|
||||
private static PropertyInfo[] GetProperties(Type type, string[] columns = null!)
|
||||
{
|
||||
PropertyInfo[] properties = null!;
|
||||
properties = type.GetProperties();
|
||||
|
||||
if (columns != null && columns.Length > 0)
|
||||
{
|
||||
// 按columns顺序返回属性
|
||||
var columnPropertyList = new List<PropertyInfo>();
|
||||
foreach (var column in columns)
|
||||
{
|
||||
var columnProperty = properties.Where(p => p.Name == column).FirstOrDefault();
|
||||
if (columnProperty != null)
|
||||
{
|
||||
columnPropertyList.Add(columnProperty);
|
||||
}
|
||||
}
|
||||
return columnPropertyList.ToArray();
|
||||
}
|
||||
else
|
||||
{
|
||||
return properties;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user