当前位置:网站首页>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

边栏推荐
- China's first chip stamp released: built-in 120um ultra-thin NFC chip
- SaltStack常用的模块
- Serial port receiving application ring buffer
- Scrapy Spider源码分析
- stc12c5a60s2功能说明(STC12C5A60S2默认触发)
- Ardupilot software in the loop simulation and online debugging
- Redis master-slave architecture (how to calculate sizeof function)
- Android section 13 03xutils detailed explanation of database framework (addition, deletion and modification)
- English article translation - English article translation software - free batch translation
- OpenOCD如何通过stlink直接下载程序到stm32板子(已解决)
猜你喜欢

Saltstack configuration management

Prometheus部署

Cvpr21 unsupervised anomaly detection cutpaste:self supervised learning for anomaly detection and localization

Cvpr19 - adjust reference dry goods bag of tricks for image classification with revolutionary neural network

11. Learn MySQL union operator

彻底理解位运算——左移、右移

英文翻译西班牙语-批量英文翻译西班牙工具免费

Using Baidu easydl to realize chef hat recognition of bright kitchen and stove

NDK 系列(5):JNI 从入门到实践,爆肝万字详解!

宝塔面板搭建小说CMS管理系统源码实测 - ThinkPHP6.0
随机推荐
英文翻译西班牙语-批量英文翻译西班牙工具免费
High beam software has obtained Alibaba cloud product ecological integration certification, and is working with Alibaba cloud to build new cooperation
English article translation - English article translation software - free batch translation
JS preventDefault() 键盘输入限制 onmousewheel stopPropagation停止事件传播
The United States will provide $25billion in subsidies to encourage chip manufacturers such as Intel to move back to production lines
Getting started with saltstack
德国、葡萄牙均宣布不会禁用华为5G设备,但德国会设定严格限制条件!
Question bank and answers of the latest national fire-fighting facility operators (intermediate fire-fighting facility operators) in 2022
Left alignment function of Lua language (handwriting)
Android-第十三节03xUtils-数据库框架(增删改查)详解
After reading the thesis for three years, I learned to read the abstract today
Have you ever seen this kind of dynamic programming -- the stock problem of state machine dynamic programming (Part 2)
SaltStack进阶
Jestson nano Object detection
npm安装和卸载全局包
leetcode day5 删除重复的电子邮箱
MySQL8 基于clone创建主从复制
彻底理解位运算——与(&)、非(~)、或(|)、异或(^)
When CNN meets transformer cmt:revolutionary neural networks meet vision transformers
MySQL8 Status Variables: Internal Temporary Tables and Files