MiniExcel简介
简单、高效避免OOM的.NET处理Excel查、写、填充数据工具。
目前主流框架大多需要将数据全载入到内存方便操作,但这会导致内存消耗问题,MiniExcel 尝试以 Stream 角度写底层算法逻辑,能让原本1000多MB占用降低到几MB,避免内存不够情况。
特点:
低内存耗用,避免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)
安装 MiniExcel
Nuget
导出数据
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);
/// <summary> /// 操作3 /// 导出数据 /// 导出指定库全部数据,或导出指定库用户选中的数据 /// </summary> /// <returns></returns> [RelayCommand(CanExecute = nameof(CanExport))] private async Task ExportAsync() { SaveFileDialog saveFileDialog = new SaveFileDialog(); //打开的文件选择对话框上的标题 saveFileDialog.Title = "请选择文件夹"; //设置文件类型 saveFileDialog.Filter = "文本文件(*.xlsx)|*.xlsx"; saveFileDialog.FileName = @$"{CurrentQALibrary.QALibraryName}{DateTime.Now:M}.xlsx"; //设置默认文件类型显示顺序 saveFileDialog.FilterIndex = 1; //保存对话框是否记忆上次打开的目录 saveFileDialog.RestoreDirectory = true; //按下确定选择的按钮 if (saveFileDialog.ShowDialog() == true) { //获得文件路径 string localFilePath = saveFileDialog.FileName.ToString(); if (QAModels.Where(e => e.IsChecked).Count() == 0) { if (File.Exists(localFilePath)) File.Delete(localFilePath); var qaModels = from qa in QAModels where qa.IsChecked == true && qa.QALibraryID == CurrentQALibrary.QALibraryID select new { 题型 = qa.Questiontype, 试卷内容 = qa.Content, 题目 = qa.SubjectTitle }; await MiniExcel.SaveAsAsync(localFilePath, qaModels); } else { if (File.Exists(localFilePath)) File.Delete(localFilePath); var qaModels = from qa in QAModels where qa.IsChecked == true && qa.QALibraryID == CurrentQALibrary.QALibraryID select new { 题型 = qa.Questiontype, 试卷内容=qa.Content,题目=qa.SubjectTitle }; await MiniExcel.SaveAsAsync(localFilePath, qaModels); } } }
导入数据
var path = "你的文件路径"; var rows = MiniExcel.Query<Student>(path);
标签:MiniExcel,C#,Age,Excel,localFilePath,qa,saveFileDialog,new From: https://www.cnblogs.com/cdaniu/p/18154915