这里主要记录下asp.net core web页面上进行导入导出excel的操作。

主要是导入,因为现在使用的很多前端框架(例如kendo ui)本身就有导出的功能。

这里使用到EPPlus.Core,其实对于excel的导入导出还可以使用NPOI,

这里讲解EPPlus的方式

1.创建asp.net core web (mvc)项目

效果图如下

2.在项目上右键,进入nuget管理器,安装EPPlus.Core

3.添加一个XlsxController控制器,在其中添加导入和导出功能

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using OfficeOpenXml; namespace EPPlusDemo.Controllers
{
public class XlsxController : Controller
{
//用来获取路径相关
private IHostingEnvironment _hostingEnvironment; public XlsxController(IHostingEnvironment hostingEnvironment)
{
_hostingEnvironment = hostingEnvironment;
}
public IActionResult Index()
{
return View();
} /// <summary>
/// excel导出功能
/// </summary>
/// <returns></returns>
public IActionResult Export()
{
string sWebRootFolder = _hostingEnvironment.WebRootPath;
string sFileName = $"{Guid.NewGuid()}.xlsx";
FileInfo file = new FileInfo(Path.Combine(sWebRootFolder, sFileName)); //Path.Combine把多个字符串组成一个路径
using (ExcelPackage package = new ExcelPackage(file)) //ExcelPackage 操作excel的主要对象
{
// 添加worksheet
ExcelWorksheet worksheet = package.Workbook.Worksheets.Add("aspnetcore");
//添加头
worksheet.Cells[, ].Value = "ID";
worksheet.Cells[, ].Value = "Name";
worksheet.Cells[, ].Value = "Url";
//添加值
worksheet.Cells["A2"].Value = ;
worksheet.Cells["B2"].Value = "baidu";
worksheet.Cells["C2"].Value = "https://www.baidu.com/"; worksheet.Cells["A3"].Value = ;
worksheet.Cells["B3"].Value = "博客园";
worksheet.Cells["C3"].Value = "https://www.cnblogs.com/";
worksheet.Cells["C3"].Style.Font.Bold = true; package.Save();
}
return File(sFileName, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
}

     //导入功能
[HttpPost]
public IActionResult Import(IFormFile excelfile)
{
string sWebRootFolder = _hostingEnvironment.WebRootPath;
string sFileName = $"{Guid.NewGuid()}.xlsx";
FileInfo file = new FileInfo(Path.Combine(sWebRootFolder, sFileName));
try
{
//把excelfile中的数据复制到file中
using (FileStream fs = new FileStream(file.ToString(), FileMode.Create)) //初始化一个指定路径和创建模式的FileStream
{
excelfile.CopyTo(fs);
fs.Flush(); //清空stream的缓存,并且把缓存中的数据输出到file
} using (ExcelPackage package = new ExcelPackage(file))
{
StringBuilder sb = new StringBuilder();
ExcelWorksheet worksheet = package.Workbook.Worksheets[];
int rowCount = worksheet.Dimension.Rows;
int ColCount = worksheet.Dimension.Columns; for (int row = ; row <= rowCount; row++)
{
for (int col = ; col <= ColCount; col++)
{
//sb.Append(worksheet.Cells[row, col].Value.ToString() + "\t"); //这种写法遇到为null的为报错
sb.Append(worksheet.Cells[row, col].Value + "\t");
}
sb.Append(Environment.NewLine);
}
return Content(sb.ToString());
}
}
catch (Exception ex)
{
return Content(ex.Message);
}
} }

4.在 Xlsx / Index.cshtml 文件上进行如下编辑

@{
ViewData["Title"] = "Index";
} <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>

5.运行程序,效果如下

最后,附上参考网址:  

https://www.cnblogs.com/linezero/p/aspnetcoreexcel.html

后续继续学习网址:

https://github.com/JanKallman/EPPlus/wiki

asp.net core web的导入导出excel功能的更多相关文章

  1. ASP.NET Core使用EPPlus导入导出Excel

    开发过程中,经常会遇到导入导出数据的需求,本篇博客介绍在.NET Core中如何使用EPPlus组件导入导出Excel EPPlus: EPPlus是使用Open Office XML格式(xlsx) ...

  2. ASP.Net MVC利用NPOI导入导出Excel

    因近期项目遇到所以记录一下: 首先导出Excel: 首先引用NPOI包 http://pan.baidu.com/s/1i3Fosux (Action一定要用FileResult) /// <s ...

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

    ASP.NET Core 使用EPPlus.Core导入导出Excel xlsx 文件,EPPlus.Core支持Excel 2007/2010 xlsx文件导入导出,可以运行在Windows, Li ...

  4. Vue框架下实现导入导出Excel、导出PDF

    项目需求:开发一套基于Vue框架的工程档案管理系统,用于工程项目资料的填写.编辑和归档,经调研需支持如下功能: Excel报表的导入.导出 PDF文件的导出 打印表格 经过技术选型,项目组一致决定通过 ...

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

    ASP.NET Core导入导出Excel文件 希望在ASP.NET Core中导入导出Excel文件,在网上搜了一遍,基本都是使用EPPlus插件,EPPlus挺好用,但商用需要授权,各位码友若有好 ...

  6. C#中缓存的使用 ajax请求基于restFul的WebApi(post、get、delete、put) 让 .NET 更方便的导入导出 Excel .net core api +swagger(一个简单的入门demo 使用codefirst+mysql) C# 位运算详解 c# 交错数组 c# 数组协变 C# 添加Excel表单控件(Form Controls) C#串口通信程序

    C#中缓存的使用   缓存的概念及优缺点在这里就不多做介绍,主要介绍一下使用的方法. 1.在ASP.NET中页面缓存的使用方法简单,只需要在aspx页的顶部加上一句声明即可:  <%@ Outp ...

  7. .Net core NPOI导入导出Excel

    最近在想.net core NPOI 导入导出Excel,一开始感觉挺简单的,后来真的遇到很多坑.所以还是写一篇博客让其他人少走一些弯路,也方便忘记了再重温一遍.好了,多的不说,直接开始吧. 在.Ne ...

  8. .NET CORE webapi epplus 导入导出 (实习第一个月的笔记)

    最近有个需求就是网页表格里面的数据导出到excel  于是从各位前辈的博客园搜了搜demo  大部分非为两类 都是用的插件NPOI和Eppluse ,因此在这里就介绍Eppluse 用法,还有就是在博 ...

  9. ASP.Net MVC中数据库数据导出Excel,供HTTP下载(转)

    转自http://www.cnblogs.com/hipo/archive/2012/03/13/2394019.html 一.关于下载 一般对下载权限有没有限制,或安全性要求不高的情况下,基于web ...

随机推荐

  1. Ubuntu18.04安装cudnn7.6.1

    注:如果使用anaconda,貌似不需要安装cuda和cudnn,安装tensorflow时会自动安装 1.进入nvidia官网https://developer.nvidia.com/cudnn下载 ...

  2. JMX——以可视化形式管理与监控正在运行中的Java程序

    简单理解: MBean:管理的最小单元,一个MBean就是一个可以被监控的JavaBean. MBeanServer:一个池子,各个MBean都会注册到该池子中,并且该池子提供一系列的管理.监控API ...

  3. Windows 2016 & Windows 10 中IIS安装和配置PHP的步骤

    Windows 2016 和 Windows 10 内核是相同的,我们首先需要安装 Internet Information Services (IIS),当然 Win2016 跟 Win10 安装  ...

  4. 安装Ubuntu后需要做的事

    卸载软件 纸牌 麻将 扫雷 数独 火狐 邮件 扫描 拍照 照片 视频 计划 日历 打印 备份 计算器 亚马逊 电源统计 音乐播放 远程桌面 To Do LibreOffice 换下载源 装机的时候下载 ...

  5. 模型选择---KFold,StratifiedKFold k折交叉切分

    StratifiedKFold用法类似Kfold,但是他是分层采样,确保训练集,测试集中各类别样本的比例与原始数据集中相同. 例子: import numpy as np from sklearn.m ...

  6. Ubuntu配置samba服务器

    假设我的Ubuntu用户名:myname 1. 安装和卸载samba: sudo apt-get install samba samba-common sudo apt-get autoremove ...

  7. JS高阶---数据、变量、内存

    [一]基础 (1)什么是数据? 存储在内存里 代表特定信息 本质为0101,二进制数据 (2)什么是内存? 内存条通电后产生的可存储数据的空间(临时的) 拓展: 1.2种数据 2.内存分类--栈和堆 ...

  8. centos virtualbox虚拟机无法连接外网

    各种方法都试了,不好使. 最后重启了很多次,最后一次成功了... ----详情---- 发生的原因是因为突然断电导致的异常. 先通过systemctl restart network 来启动,结果报错 ...

  9. 12 opencv图像合成

    #include < stdio.h > #include < opencv2\opencv.hpp > #include < opencv2\stitching.hpp ...

  10. DCI学习链接及文章

    https://www.jianshu.com/u/c1b1137d5886 李永顺 https://www.jianshu.com/users/7386692d5489/timeline 袁英杰 小 ...