ASP.NET Core 导入导出Excel xlsx 文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
ASP.NET Core 导入导出Excel xlsx 文件
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 功能也就完成了。

  

posted @   八英里  阅读(651)  评论(0编辑  收藏  举报
编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
点击右上角即可分享
微信分享提示