网上找了很多方法,感觉这是最简约直接的。
ASP.NET Core 使用EPPlus.Core导入导出Excel xlsx 文件,EPPlus.Core支持Excel 2007/2010 xlsx文件导入导出,可以运行在Windows, Linux和Mac。
EPPlus.Core 是基于EPPlus 更改而来,在Linux 下需要安装libgdiplus 。
EPPlus:http://epplus.codeplex.com/
EPPlus.Core:https://github.com/VahidN/EPPlus.Core
下面在ASP.NET Core 中导入导出Excel xlsx 文件。
新建项目
新建一个ASP.NET Core Web Application 项目ASPNETCoreExcel,选择Web 应用程序 不进行身份验证。
然后添加EPPlus.Core 引用。
使用NuGet 命令行:
Install-Package EPPlus.Core
也可以使用NuGet包管理器安装。
涉及:一个视图index,控制器
视图index
@{
}
ASP.NET Core 导入导出Excel xlsx 文件
导出Excel
控制器代码
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using epplus_excelDemo.Models;
using Microsoft.AspNetCore.Http;
using OfficeOpenXml;
using System.Text;
using System.IO;
using Microsoft.AspNetCore.Hosting;
namespace epplus_excelDemo.Controllers
{
public class HomeController : Controller
{
private IHostingEnvironment _hostingEnvironment;
public HomeController(IHostingEnvironment hostingEnvironment)
{
_hostingEnvironment = hostingEnvironment;
}
public IActionResult Index()
{
return View();
}
[HttpPost]
public IActionResult Import(IFormFile excelfile)
{
string sWebRootFolder = _hostingEnvironment.WebRootPath;
string sFileName = $"{Guid.NewGuid()}.xlsx";
FileInfo file = new FileInfo(Path.Combine(sWebRootFolder, sFileName));
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;
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 Content(sb.ToString());
}
}
catch (Exception ex)
{
return Content(ex.Message);
}
}
public IActionResult Export()
{
string sWebRootFolder = _hostingEnvironment.WebRootPath;
string sFileName = $"{Guid.NewGuid()}.xlsx";
FileInfo file = new FileInfo(Path.Combine(sWebRootFolder, sFileName));
using (ExcelPackage package = new ExcelPackage(file))
{
// 添加worksheet
ExcelWorksheet worksheet = package.Workbook.Worksheets.Add("aspnetcore");
//添加头
worksheet.Cells[1, 1].Value = "ID";
worksheet.Cells[1, 2].Value = "Name";
worksheet.Cells[1, 3].Value = "Url";
//添加值
worksheet.Cells["A2"].Value = 1000;
worksheet.Cells["B2"].Value = "LineZero";
worksheet.Cells["C2"].Value = "http://www.cnblogs.com/linezero/";
worksheet.Cells["A3"].Value = 1001;
worksheet.Cells["B3"].Value = "LineZero GitHub";
worksheet.Cells["C3"].Value = "https://github.com/linezero";
worksheet.Cells["C3"].Style.Font.Bold = true;
package.Save();
}
return File(sFileName, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
}
}
}
转自:http://www.oaos.cn/msg.php?i=4341093449ec88