using Microsoft.AspNetCore.Http; using System; using System.Collections.Generic; using System.Data; using System.IO; using System.Text; using System.Threading; using System.Threading.Tasks; using OfficeOpenXml; namespace MadRunFabric.Common { public interface IFormFile { string ContentType { get; } string ContentDisposition { get; } IHeaderDictionary Headers { get; } long Length { get; } string Name { get; } string FileName { get; } Stream OpenReadStream(); void CopyTo(Stream target); Task CopyToAsync(Stream target, CancellationToken cancellationToken); } public class ExcelHelper { /// /// 导入Excel /// /// /// /// /// public DataTable ImportExcel(string filepath, string filename, FileStream excelfile) { //string sWebRootFolder = _hostingEnvironment.WebRootPath; //string sFileName = $"{Guid.NewGuid()}.xlsx"; FileInfo file = new FileInfo(Path.Combine(filepath, filename)); DataTable dt = new DataTable(); try { using (FileStream fs = new FileStream(file.ToString(), FileMode.Create)) { excelfile.CopyTo(fs); fs.Flush(); } using (ExcelPackage package = new ExcelPackage(file)) { //StringBuilder sb = new StringBuilder(); ExcelWorksheet worksheet = package.Workbook.Worksheets[1]; int rowCount = worksheet.Dimension.Rows; int ColCount = worksheet.Dimension.Columns; for (int col = 1; col <= ColCount; col++) { dt.Columns.Add(worksheet.Cells[1, col].Value.ToString()); } //bool bHeaderRow = true; for (int row = 2; row <= rowCount; row++) { for (int col = 1; col <= ColCount; col++) { dt.Rows[row - 1][col - 1] = worksheet.Cells[row, col].Value.ToString(); //if (bHeaderRow) //{ // sb.Append(worksheet.Cells[row, col].Value.ToString() + "\t"); //} //else //{ //sb.Append(worksheet.Cells[row, col].Value.ToString() + "\t"); //} } //sb.Append(Environment.NewLine); //bHeaderRow = false; } return dt;// Content(sb.ToString()); } } catch (Exception ex) { return null; } } } }