ASP.NET Core 导入导出Excel xlsx 文件
来源:互联网 发布:mac怎么进入屏保 编辑:程序博客网 时间:2024/05/22 00:45
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包管理器安装。
导出xlsx文件
新建一个XlsxController ,添加Export 操作。
public class XlsxController : Controller {
private IHostingEnvironment _hostingEnvironment; public XlsxController(IHostingEnvironment hostingEnvironment) { _hostingEnvironment = hostingEnvironment; }
public IActionResult Index() {
return View(); }
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"); } }
通过依赖注入获取HostingEnvironment,对应可以获取程序的相关目录及属性。
然后添加Index 视图增加一个链接导出Excel
@{ }<h2>ASP.NET Core 导入导出Excel xlsx 文件</h2><a asp-action="Export">导出Excel</a>
点击导出文件,打开结果如下。
导入xlsx文件
在index视图中添加一个上传文件,添加Import操作。
Index.cshtml
@{ }<h2>ASP.NET Core 导入导出Excel xlsx 文件</h2><a asp-action="Export">导出Excel</a><hr /><form enctype="multipart/form-data" method="post" asp-action="Import"> <input type="file" name="excelfile" /> <input type="submit" value="上传" /></form>
[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); } }
运行程序打开http://localhost:5000/xlsx
上传对应文件,显示如下。
ASP.NET Core简单的导入导出Excel 功能也就完成了。
相关文章:
.NET应用迁移到.NET Core(一)
.NET应用迁移到.NET Core(二)风险评估
.NET应用迁移到.NET Core(三)从商业角度看移植过程
.NET应用迁移到.NET Core--调查案例
.NET Core 首例 Office 开源跨平台组件(NPOI Core)
原文地址:http://www.cnblogs.com/savorboard/p/netcore-npoi.html
.NET社区新闻,深度好文,微信中搜索dotNET跨平台或扫描二维码关注
- ASP.NET Core 导入导出Excel xlsx 文件
- asp.net Excel导入&导出
- asp.net Excel导入&导出
- asp.net Excel导入&导出
- asp.net导入导出EXCEL
- asp.net Excel导入&导出
- asp.net excel导入导出
- asp.net导出/导入规则的Excel(.xls)文件
- Asp.Net下导出/导入规则的Excel(.xls)文件
- ASP.NET中导入和导出标准Excel文件
- ASP.NET 实现Excel文件的导入与导出
- ASP.NET中导入和导出标准Excel文件
- ASP.NET中导入和导出标准Excel文件
- ASP.NET中导入和导出标准Excel文件
- asp.net 导出excel文件
- ASP.NET 导出Excel文件
- Asp.net 导出Excel文件
- ASP.NET Excel文件导入
- Hangfire项目实践分享
- WebSocket in ASP.NET Core
- 博客园官方 NuGet镜像上线试运行
- .NET泛型初探
- 微软称开源.NET吸引了更多开发者
- ASP.NET Core 导入导出Excel xlsx 文件
- ASP.NET Core 在 Swagger UI 中显示自定义的 Header Token
- iava 第二课笔记
- 猴子吃桃(GO和lua实现)
- 一个复杂系统的拆分改造实践
- get√—搜索微信公众号【Dotnet跨平台】指定文章的办法
- 分享我的第一个asp.net core开发过程
- 在收购 Sun 的六年后,Oracle 终于瞄准了 Java 的非付费用户
- 从抵触到力推,.Net Core的成功让微软正视开源