using ExcelDataReader;
using Microsoft.AspNetCore.Http;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ZR.Common
{
    public class ExcelReader
    {
        public static DataSet ReadExcel(IFormFile formFile)
        {
            IExcelDataReader? readerData = null;
            using Stream stream = formFile.OpenReadStream();
            //根据excel扩展名选择对应的读取文件流方式
            switch (formFile.FileName.Split('.').Last().ToLower())
            {
                case "xls": readerData = ExcelReaderFactory.CreateBinaryReader(stream); break;
                case "xlsx": readerData = ExcelReaderFactory.CreateOpenXmlReader(stream); break;
                default: break;
            }
            return readerData?.AsDataSet();
        }
    }
}
