当前位置:网站首页>NetCoreAPI操作Excel表格
NetCoreAPI操作Excel表格
2022-07-28 17:48:00 【有诗亦有远方】
NetCoreAPI操作Excel表格
一、开源框架MiniExcel
MiniExcel(推荐使用)
MiniExcel简单、高效避免OOM的.NET处理Excel查、写、填充数据工具。
目前主流框架(EPPlus ,NPIO)大多需要将数据全载入到内存方便操作,但这会导致内存消耗问题,MiniExcel 尝试以 Stream 角度写底层算法逻辑,能让原本1000多MB占用降低到几MB,避免内存不够情况。
二、引入MiniExce的Nuget包
Install-Package MiniExcel -Version 1.26.5
三、操作Excel示例
1.准备两个Excel表格

两张数据格式

2.创建Student类
因为MiniExcel生成的是强类型数据,所以需要一个类来承接数据
using System.ComponentModel.DataAnnotations;
namespace _02_EFWithOptionExcel
{
public class Student
{
public int ID {
get; set; }
[StringLength(50)]
public string Name {
get; set; }
[StringLength(2)]
public string Sex {
get; set; }
[StringLength(11)]
public string Phone {
get; set; }
[StringLength(200)]
public string Address {
get; set; }
}
}
3.使用EFCore来操作数据
创建Excel控制器
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using MiniExcelLibs;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
namespace _02_EFWithOptionExcel.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class MinExcelController : ControllerBase
{
private readonly StudentDbContext _studentDbContext;
private readonly IWebHostEnvironment _webHostingEnvironment;
public MinExcelController(IWebHostEnvironment hostingEnvironment,StudentDbContext studentDbContext)
{
_webHostingEnvironment = hostingEnvironment;
_studentDbContext = studentDbContext;
}
[HttpPost]
public List<Student> Get([FromForm] IFormCollection formCollection)
{
//文件集合
FormFileCollection fileCollection = (FormFileCollection)formCollection.Files;
//学生集合
List<Student> lists = new List<Student>();
//遍历集合中的文件
foreach (IFormFile file in fileCollection)
{
//打开文件stream
Stream stream = file.OpenReadStream();
//操纵stream,生成集合
var rows = stream.Query<Student>().ToList();
//将两个excel合并为一个集合
lists = lists.Union(rows).ToList();
}
//批量添加数据
_studentDbContext.Student.AddRange(lists);
//保存数据
_studentDbContext.SaveChanges();
return lists;
}
}
}
四、测试Api

边栏推荐
- The opening price soared by 215%! Domestic signal chain chip enterprise Xinhai Technology landed on the scientific innovation board
- MySQL8 Status Variables: Internal Temporary Tables and Files
- Ardupilot software in the loop simulation and online debugging
- How does app automated testing achieve H5 testing
- JS preventDefault() 键盘输入限制 onmousewheel stopPropagation停止事件传播
- Rust Getting Started Guide (crite Management)
- Rust Getting Started Guide (rustup, cargo)
- 为研发高端光刻胶,晶瑞股份斥资7500万元购买SK海力士的ASML光刻机
- MySQL8 基于clone创建主从复制
- China's first chip stamp released: built-in 120um ultra-thin NFC chip
猜你喜欢
随机推荐
彻底理解位运算——左移、右移
leetcode day4 部门工资最高的员工
并发程序设计,你真的懂吗?
MySQL8 Encrypting InnoDB Tablespaces
远光软件获得阿里云产品生态集成认证,携手阿里云共建新合作
Nips18 (AD) - unsupervised anomaly detection using geometric transformations using geometric augmentation
adb remount of the / superblock failed: Permission denied
MySQL8 Encrypting InnoDB Tablespaces
OpenOCD如何通过stlink直接下载程序到stm32板子(已解决)
[深入研究4G/5G/6G专题-44]: URLLC-15-《3GPP URLLC相关协议、规范、技术原理深度解读》-9-低延时技术-3-非时隙调度Mini slot
Salt SSH of saltstack
When CNN meets transformer cmt:revolutionary neural networks meet vision transformers
MySQL性能测试工具sysbench学习
以数字化转型为契机,3C企业如何通过SRM供应商云协同平台实现高效协同?
[solved] ac86u ml revision firmware virtual memory creation failed, prompting that the USB disk reading and writing speed does not meet the requirements
Saltstack configuration management
MySQL8 基于clone创建主从复制
为什么在telnet登入界面下没有日志输出?
英文翻译意大利语-批量英文翻译意大利语工具免费
SaltStack常用的模块









