当前位置:网站首页>Netcoreapi operation excel table
Netcoreapi operation excel table
2022-07-28 19:42:00 【Poetry and distance】
NetCoreAPI operation Excel form
One 、 Open source framework MiniExcel
MiniExcel( Recommended )
MiniExcel Simple 、 Efficient avoidance OOM Of .NET Handle Excel check 、 Write 、 Fill data tool .
The current mainstream framework (EPPlus ,NPIO) Most of them need to load all data into memory for easy operation , But this can lead to memory consumption problems ,MiniExcel Try to Stream Write the underlying algorithmic logic from an angle , Can make the original 1000 many MB The occupancy is reduced to a few MB, Avoid out of memory situations .
Two 、 introduce MiniExce Of Nuget package
Install-Package MiniExcel -Version 1.26.5
3、 ... and 、 operation Excel Example
1. Prepare two Excel form

Two data formats

2. establish Student class
because MiniExcel Generated is strongly typed data , So we need a class to accept data
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. Use EFCore To manipulate data
establish Excel controller
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)
{
// File set
FormFileCollection fileCollection = (FormFileCollection)formCollection.Files;
// Students gather
List<Student> lists = new List<Student>();
// Traverse the files in the collection
foreach (IFormFile file in fileCollection)
{
// Open file stream
Stream stream = file.OpenReadStream();
// manipulation stream, Generate sets
var rows = stream.Query<Student>().ToList();
// Put two excel Merge into a collection
lists = lists.Union(rows).ToList();
}
// Batch add data
_studentDbContext.Student.AddRange(lists);
// Save the data
_studentDbContext.SaveChanges();
return lists;
}
}
}
Four 、 test Api

边栏推荐
- STC12C5A60S2 function description (STC12C5A60S2 is triggered by default)
- 我的第二次博客——C语言
- Have you ever seen this kind of dynamic programming -- the stock problem of state machine dynamic programming (Part 2)
- 远光软件获得阿里云产品生态集成认证,携手阿里云共建新合作
- 可转债概念表 x Notion 给你方便快捷的体验!
- 基于QTGUI图像界面的空战游戏设计
- 华为入股南京芯视界,布局固态激光雷达芯片领域
- 冲刺金九银十丨熬夜半个月汇集大厂Android岗1600道面试真题
- Taking the opportunity of digital transformation, how can 3C enterprises achieve efficient collaboration through SRM supplier cloud collaboration platform?
- SaltStack入门
猜你喜欢

SaltStack系统初始化

Report redirect after authorized login on wechat official account_ The problem of wrong URI parameters

App自动化测试是怎么实现H5测试的

NDK series (5): from introduction to practice, JNI explodes the liver and explains everything in detail!

Cell综述:人类微生物组研究中的单细胞方法

宝塔面板搭建小说CMS管理系统源码实测 - ThinkPHP6.0

英文翻译阿拉伯语-批量英文翻译阿拉伯语工具免费

Prometheus部署

High beam software has obtained Alibaba cloud product ecological integration certification, and is working with Alibaba cloud to build new cooperation

SaltStack入门
随机推荐
Have you ever seen this kind of dynamic programming -- the stock problem of state machine dynamic programming (Part 2)
leetcode day4 部门工资最高的员工
初步学习函数(第3篇博客)
CodeIgnier框架实现restful API接口编程
MySQL8 tmp_ table_ Size and Max_ heap_ table_ size
文章翻译软件-批量免费翻译软件支持各大翻译接口
This customized keyboard turns me on~
lua语言的左对齐函数(手写)
Serial port receiving application ring buffer
Smart contract security - overflow vulnerability
Design of library management database system
Amazon launched Amazon one palm payment system, and the contactless palm vein recognition market is expected to explode
SaltStack入门
调用整数或字符数组函数里的参数应该传入啥
Question bank and answers of the latest national fire-fighting facility operators (intermediate fire-fighting facility operators) in 2022
leetcode day5 删除重复的电子邮箱
MySQL performance testing tool sysbench learning
English article translation - English article translation software - free batch translation
Sword finger offer II 109. unlock the password lock
我的第二次博客——C语言