• 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; }
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    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;
            }
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52

    四、测试Api

    在这里插入图片描述

  • 相关阅读:
    论文阅读笔记 | 三维目标检测——SECOND算法
    思腾云计算
    51.【Java String方法的小结】
    基于ssm设备租赁报修借用管理系统java项目源码
    时代的一粒沙,压在个人头上便是整整的一座山
    Python3 笔记:字符串的 encode() 和 bytes.decode()
    鸡卵清白蛋白偶联维生素A(VA-OVA),Vitamin A-Ovalbumin Conjugate
    ubuntu22.04下面安装josm
    Spring 配置使用介绍
    BC v1.2充电规范
  • 原文地址:https://blog.csdn.net/wsnbbdbbdbbdbb/article/details/125996773