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

边栏推荐
- 英文翻译阿拉伯语-批量英文翻译阿拉伯语工具免费
- 【笔记】《启示录》:产品经理的实践经验与反省清单
- Smart contract security - overflow vulnerability
- Pytoch: implementation of crossentropyloss and labelsmoothing
- Sudo rosdep init error: cannot download default
- BLDC 6-step commutation simulink
- SaltStack之return与job管理
- App自动化测试是怎么实现H5测试的
- Salt SSH of saltstack
- MySQL8 tmp_ table_ Size and Max_ heap_ table_ size
猜你喜欢
随机推荐
MySQL 8 creates master-slave replication based on Clone
Smart contract security - overflow vulnerability
MATLAB实现的图像分割之边缘检测和连接
This customized keyboard turns me on~
文章翻译软件-批量免费翻译软件支持各大翻译接口
Preliminary learning function (3rd blog)
Business visualization - let your flowchart "run" (4. Actual business scenario test)
远光软件获得阿里云产品生态集成认证,携手阿里云共建新合作
Transformer for anomaly detection - instra "painting transformer for anomaly detection"
What parameters should be passed in calling integer or character array functions
Basic concept and essence of Architecture
我的第二次博客——C语言
ES6 new - arrow function
Getting started with saltstack
lua语言的左对齐函数(手写)
亚马逊推出Amazon One手掌支付系统,非接触式掌静脉识别市场有望爆发
使用SaltStack自动化部署LNMP
shared_ptr 和 make_shared 的使用
Have you ever seen this kind of dynamic programming -- the stock problem of state machine dynamic programming (Part 2)
博途1200/1500PLC上升沿下降沿指令编程应用技巧(bool数组)









