日韩黑丝制服一区视频播放|日韩欧美人妻丝袜视频在线观看|九九影院一级蜜桃|亚洲中文在线导航|青草草视频在线观看|婷婷五月色伊人网站|日本一区二区在线|国产AV一二三四区毛片|正在播放久草视频|亚洲色图精品一区

分享

MiniExcel:一個非常高效的Excel讀寫工具

 新用戶0118F7lQ 2023-09-02 發(fā)布于山東

最近沒什么時間,主線劇情是玩不來了,所以偷個懶,打一個直線任務(wù)。

如果我們的程序?qū)Σ僮鱁xcel有需求,那么NPOI應(yīng)該不會陌生。不過這里并不打算說NPOI,而是另外一個使用簡單的工具:MiniExcel

https://gitee.com/dotnetchina/MiniExcel

MiniExcel簡單、高效避免OOM的.NET處理Excel查、寫、填充數(shù)據(jù)工具。

目前主流框架大多需要將數(shù)據(jù)全載入到內(nèi)存方便操作,但這會導(dǎo)致內(nèi)存消耗問題,MiniExcel 嘗試以 Stream 角度寫底層算法邏輯,能讓原本1000多MB占用降低到幾MB,避免內(nèi)存不夠情況。

圖片

特點

  • 低內(nèi)存耗用,避免OOM、頻繁 Full GC 情況

  • 支持即時操作每行數(shù)據(jù)

  • 兼具搭配 LINQ 延遲查詢特性,能辦到低消耗、快速分頁等復(fù)雜查詢

  • 輕量,不需要安裝 Microsoft Office、COM+,DLL小于150KB

  • 簡便操作的 API 風(fēng)格

上面是關(guān)于MiniExcel的簡單介紹,下面開始實戰(zhàn)。

我這里新建一個WPF項目,添加一個DataGrid用于顯示數(shù)據(jù)。

圖片

綁定Students數(shù)據(jù),

private List<StudentInfo> students;public List<StudentInfo> Students { get => students; set => SetProperty(ref students, value); }

StudentInfo類屬性如下,

public class StudentInfo { public string Name { get; set; } public string Age { get; set; } public string Grade { get; set; } }

新建一個xlsx表格,數(shù)據(jù)如下,title與StudentInfo對應(yīng)

圖片

通過Nuget加載MiniExcel。

圖片

當(dāng)我們需要讀取數(shù)據(jù),使用Query方法;

string filePath = Path.Combine(AppContext.BaseDirectory, 'students.xlsx');Students = MiniExcel.Query<StudentInfo>(filePath).ToList();

圖片

我們可以看到,MiniExcel使用的是靜態(tài)方法,返回的數(shù)據(jù)是IEnumerable

public static IEnumerable<T> Query<T>(string path, string sheetName = null, ExcelType excelType = ExcelType.UNKNOWN, string startCell = 'A1', IConfiguration configuration = null) where T : class, new() { using FileStream stream = FileHelper.OpenSharedRead(path); foreach (T item in stream.Query<T>(sheetName, ExcelTypeHelper.GetExcelType(path, excelType), startCell, configuration)) { yield return item; } }

當(dāng)我們轉(zhuǎn)換為List,就可以進行更多的處理。

將數(shù)據(jù)寫入Excel,使用SaveAS方法。

Students = new List<StudentInfo>             {                 new StudentInfo() { Name='熏兒',Age='25',Grade='斗帝'},                new StudentInfo() { Name='小醫(yī)仙',Age='24',Grade='斗帝'},            };            string filePath = Path.Combine(AppContext.BaseDirectory, 'students.xlsx');            MiniExcel.SaveAs(filePath, Students,overwriteFile:true);

圖片

注意我們這里的overwriteFile:true

如果不設(shè)定覆蓋文件,那么當(dāng)student.xlsx存在,那么就引發(fā)IO異常

圖片

    本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多