最近学了一个ASP.NET Core 导出Excel文件程序,在学习过程中遇到了不少的麻烦!为了让大家更明白的怎么用ASP.NET Core 导出Excel文件,我决定把我学到的东西分享出来!
首先我们要创建一个这样类型的一个项目:
项目创建好了记得下载
项目创建好了之后,我们新建个名为XlsxController.cs,我直接上代码吧!多说无益!
注意画红线的地方!!!
代码如下:
private IHostingEnvironment _hostingEnvironment;
public XlsxController(IHostingEnvironment hostingEnvironment)
{
_hostingEnvironment = hostingEnvironment;
}
public IActionResult Export()
{
string sWebRootFolder = _hostingEnvironment.WebRootPath;
string sFileName = "部落.xlsx";
FileInfo file = new FileInfo(Path.Combine(sWebRootFolder, sFileName));
file.Delete();
using (ExcelPackage package = new ExcelPackage(file))
{
// 添加worksheet
ExcelWorksheet worksheet = package.Workbook.Worksheets.Add("部落");
//添加头
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 = "For丨丶";
worksheet.Cells["C2"].Value = "网页链接
worksheet.Cells["A3"].Value = 1001;
worksheet.Cells["B3"].Value = "For丨丶Tomorrow";
worksheet.Cells["C3"].Value = "网页链接
worksheet.Cells["C3"].Style.Font.Bold = true;
package.Save();
}
return File(sFileName, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", sFileName);
}
代码已经贴出来!如果大家想要应用做好的,我已经放到百度云需要的朋友可以去下载
链接:网页链接 密码:su25
如果要不清楚的地方可以问我! 来源:https://blog.csdn.net/qq_34220236/article/details/80841529 免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作! |