控制器方法
public virtual IActionResult ImportExcel(IFormFile importexcelfile)
{
var string sWebRootFolder = _hostingEnvironment.WebRootPath;
string sFileName = importexcelfile.FileName;
FileInfo file = new FileInfo(Path.Combine(filePath, sFileName));
try
{
using (FileStream fs = new FileStream(file.ToString(), FileMode.Create))
{
importexcelfile.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;
bool bHeaderRow = true;
for (int row = 1; row <= rowCount; row++)
{
for (int col = 1; col <= ColCount; col++)
{
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);
}
return RedirectToAction("List");
}
}
catch (Exception ex)
{
return RedirectToAction("List");
}
}