tuiwucarrer/Admin.NET/Admin.NET.Core/Util/InputExcelUtil.cs

52 lines
1.4 KiB
C#

using MiniExcelLibs;
using SharpCompress.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Admin.NET.Core;
/// <summary>
///
/// </summary>
/// <typeparam name="T"></typeparam>
public class InputExcelUtil<T> where T : class, new()
{
private static List<string> extName = new List<string>() { ".xls", ".xlsx" };
/// <summary>
/// 导入Excel内容读取到List<T>中
/// </summary>
/// <param name="file"></param>
/// <param name="sheetName"></param>
/// <returns></returns>
public static List<T> InputExcel(IFormFile file, string sheetName = null)
{
//获取文件后缀名
string type = Path.GetExtension(file.FileName);
//判断是否导入合法文件
if (!extName.Contains(type))
{
return null;
}
// List<T> list = new List<T>();
//转成为文件流
//MemoryStream ms = new MemoryStream();
//file.CopyTo(ms);
//ms.Seek(0, SeekOrigin.Begin);
////实例化T数组
////获取数据
// list = InputExcel(ms, sheetName);
//根据指定路径读取文件
var fs = file.OpenReadStream();
List<T> list = fs.Query<T>(sheetName: sheetName).ToList();
// list = ms.Query().ToList();
return list;
}
}