当前位置:   article > 正文

C#使用MiniExcel导入导出数据到Excel/CSV文件_miniexcel csv格式

miniexcel csv格式

MiniExcel简介

简单、高效避免OOM的.NET处理Excel查、写、填充数据工具。

目前主流框架大多需要将数据全载入到内存方便操作,但这会导致内存消耗问题,MiniExcel 尝试以 Stream 角度写底层算法逻辑,能让原本1000多MB占用降低到几MB,避免内存不够情况。

image-20240210212456544

特点:

  • 低内存耗用,避免OOM、频繁 Full GC 情况
  • 支持即时操作每行数据
  • 兼具搭配 LINQ 延迟查询特性,能办到低消耗、快速分页等复杂查询
  • 轻量,不需要安装 Microsoft Office、COM+,DLL小于150KB
  • 简便操作的 API 风格

项目地址:mini-software/MiniExcel: Fast, Low-Memory, Easy Excel .NET helper to import/export/template spreadsheet (support Linux, Mac) (github.com)

image-20240210212654050

安装 MiniExcel

image-20240209130817512

导出数据

 public class Student
  {
      public string? Name { get; set; }
      public string? Home { get; set; }
      public int? Age {  get; set; }   
  }

 //获得当前目录的路径
 string currentDirectory = Directory.GetCurrentDirectory();

 //合并当前目录与Excel文件名
 var path = Path.Combine(currentDirectory, $"{Guid.NewGuid()}.xlsx");
 
  List<Student> studentList = new List<Student>()
  {
      new Student { Name = "小东", Home = "New York", Age = 25 },
      new Student { Name = "小西", Home = "London", Age = 22 },
      new Student { Name = "小南", Home = "Paris", Age = 28 },
      new Student { Name = "小北", Home = "Tokyo", Age = 24 },
      new Student { Name = "小王", Home = "Berlin", Age = 26 }
 };

MiniExcel.SaveAs(path, studentList);
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

导入Excel中如下图所示:

image-20240209131944852

但是目前还不支持Excel的插入:

image-20240209132453019

因此如果遇到不仅仅只是生成Excel,后续还要向Excel添加数据的需求,可以用csv代替:

 //获得当前目录的路径
 string currentDirectory = Directory.GetCurrentDirectory();

 //合并当前目录与CSV文件名
 var path = Path.Combine(currentDirectory, $"{Guid.NewGuid()}.csv");

 List<Student> studentList = new List<Student>()
  {
      new Student { Name = "小东", Home = "New York", Age = 25 },
      new Student { Name = "小西", Home = "London", Age = 22 },
      new Student { Name = "小南", Home = "Paris", Age = 28 },
      new Student { Name = "小北", Home = "Tokyo", Age = 24 },
      new Student { Name = "小王", Home = "Berlin", Age = 26 }
 };

 MiniExcel.SaveAs(path, studentList);
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

生成的CSV文件如下图所示:

image-20240209132904268

向CSV文件继续添加数据:

  //获得当前目录的路径
  string currentDirectory = Directory.GetCurrentDirectory();

  //合并当前目录与Excel文件名
  //var path = Path.Combine(currentDirectory, $"{Guid.NewGuid()}.csv");
  var path = Path.Combine(currentDirectory, "10de09bb-ad8c-4430-b717-5f24117a11a6.csv");

  List<Student> studentList = new List<Student>()
   {
       new Student { Name = "小东", Home = "New York", Age = 25 },
       new Student { Name = "小西", Home = "London", Age = 22 },
       new Student { Name = "小南", Home = "Paris", Age = 28 },
       new Student { Name = "小北", Home = "Tokyo", Age = 24 },
       new Student { Name = "小王", Home = "Berlin", Age = 26 }
  };

  //MiniExcel.SaveAs(path, studentList);
  MiniExcel.Insert(path, studentList);
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

继续添加数据后的CSV文件如下图所示:

image-20240209133412478

导入数据

 var path = "你的文件路径";
 var rows = MiniExcel.Query<Student>(path);
  • 1
  • 2

实现效果如下所示:

image-20240210214410232
公众号:DotNet学习交流,欢迎您的到来!!

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/321601
推荐阅读
相关标签
  

闽ICP备14008679号