1. <strong id="7actg"></strong>
    2. <table id="7actg"></table>

    3. <address id="7actg"></address>
      <address id="7actg"></address>
      1. <object id="7actg"><tt id="7actg"></tt></object>

        如何在 Asp.Net Core 實(shí)現(xiàn) Excel 導(dǎo)出功能

        共 1853字,需瀏覽 4分鐘

         ·

        2021-01-06 14:33

        在web應(yīng)用程序開(kāi)發(fā)時(shí),或許你會(huì)遇到這樣的需求,如何在 Asp.Net Core 中實(shí)現(xiàn) excel 或者 word 的導(dǎo)入導(dǎo)出,在 NuGet 上有大量的工具包可以實(shí)現(xiàn)這樣的功能,本篇就討論下如何使用 ClosedXML 實(shí)現(xiàn) Excel 數(shù)據(jù)導(dǎo)出。

        安裝 ClosedXML

        如果想實(shí)現(xiàn) Excel 的導(dǎo)出功能,在 Asp.Net Core 中有很多的dll可以做到,其中的一個(gè)叫做 ClosedXML,你可以通過(guò)可視化界面 NuGet package manager 去安裝,也可以使用命令行 NuGet package manager console 執(zhí)行下面命令。


        Install-Package?ClosedXML

        將數(shù)據(jù)導(dǎo)出成 CSV 文件

        將數(shù)據(jù)導(dǎo)成 CSV 文件是非常簡(jiǎn)單的,畢竟每行數(shù)據(jù)都是用 , 隔開(kāi)即可,可以用 NuGet 上的 CsvExport 或者 AWright18.SimpleCSVExporter 去實(shí)現(xiàn),當(dāng)然你覺(jué)得自己很 ??,可以親自操刀實(shí)現(xiàn),下面我準(zhǔn)備親自實(shí)現(xiàn)一下,先看下面定義的 Author 類。


        public?class?Author
        {
        ??public?int?Id?{?get;?set;?}
        ??public?string?FirstName?{?get;?set;?}
        ??public?string?LastName?{?get;?set;?}
        }

        然后塞一些數(shù)據(jù)到 authors 列表中,如下代碼所示:


        List?authors?=?new?List
        {
        ????new?Author?{?Id?=?1,?FirstName?=?"Joydip",?LastName?=?"Kanjilal"?},
        ????new?Author?{?Id?=?2,?FirstName?=?"Steve",?LastName?=?"Smith"?},
        ????new?Author?{?Id?=?3,?FirstName?=?"Anand",?LastName?=?"Narayaswamy"}
        };

        定義一個(gè) DownloadCommaSeperatedFile 方法,用于實(shí)現(xiàn) Action 的 csv 導(dǎo)出功能。


        public?IActionResult?DownloadCommaSeperatedFile()
        {
        ????try
        ????{
        ???????StringBuilder?stringBuilder?=?new?StringBuilder();
        ???????stringBuilder.AppendLine("Id,FirstName,LastName");
        ???????foreach?(var?author?in?authors)
        ???????{
        ???????????stringBuilder.AppendLine($"{author.Id},
        ???????????{author.FirstName},{author.LastName}"
        );
        ???????}
        ??????return?File(Encoding.UTF8.GetBytes
        ??????(stringBuilder.ToString()),?"text/csv",?"authors.csv");
        ????}
        ????catch
        ????{
        ???????return?Error();
        ????}
        }

        將數(shù)據(jù)導(dǎo)出成 XLSX 文件

        Excel 中的 workbook 是由若干個(gè) worksheet 組成,下面的代碼可用來(lái)生成一個(gè) workbook。


        var?workbook?=?new?XLWorkbook();

        接下來(lái)生成一個(gè) worksheet,然后在 worksheet 中填一些數(shù)據(jù),代碼如下:


        IXLWorksheet?worksheet?=?workbook.Worksheets.Add("Authors");
        worksheet.Cell(1,?1).Value?=?"Id";
        worksheet.Cell(1,?2).Value?=?"FirstName";
        worksheet.Cell(1,?3).Value?=?"LastName";
        for?(int?index?=?1;?index?<=?authors.Count;?index++)
        {
        ???worksheet.Cell(index?+?1,?1).Value?=?authors[index?-?1].Id;
        ???worksheet.Cell(index?+?1,?2).Value?=?authors[index?-?1].FirstName;
        ???worksheet.Cell(index?+?1,?3).Value?=?authors[index?-?1].LastName;
        }

        最后,將 workbook 轉(zhuǎn)成 內(nèi)存流 (memory stream) 再通過(guò) Controller.Action 的 FileContentResult 返回給客戶端,代碼如下:


        using?(var?stream?=?new?MemoryStream())
        {
        ?????workbook.SaveAs(stream);
        ?????var?content?=?stream.ToArray();
        ?????return?File(content,?contentType,?fileName);
        }

        下載 Excel

        下面是導(dǎo)出 Excel 所有的業(yè)務(wù)邏輯代碼,這個(gè) Action 實(shí)現(xiàn)了 Excel 導(dǎo)出功能。


        ????????public?IActionResult?DownloadExcelDocument()
        ????????{
        ????????????string?contentType?=?"application/vnd.openxmlformats-
        ????????????officedocument.spreadsheetml.sheet"
        ;
        ????????????string?fileName?=?"authors.xlsx";
        ????????????try
        ????????????{
        ????????????????using?(var?workbook?=?new?XLWorkbook())
        ????????????????{
        ????????????????????IXLWorksheet?worksheet?=
        ????????????????????workbook.Worksheets.Add("Authors");
        ????????????????????worksheet.Cell(1,?1).Value?=?"Id";
        ????????????????????worksheet.Cell(1,?2).Value?=?"FirstName";
        ????????????????????worksheet.Cell(1,?3).Value?=?"LastName";
        ????????????????????for?(int?index?=?1;?index?<=?authors.Count;?index++)
        ????????????????????{
        ????????????????????????worksheet.Cell(index?+?1,?1).Value?=
        ????????????????????????authors[index?-?1].Id;
        ????????????????????????worksheet.Cell(index?+?1,?2).Value?=
        ????????????????????????authors[index?-?1].FirstName;
        ????????????????????????worksheet.Cell(index?+?1,?3).Value?=
        ????????????????????????authors[index?-?1].LastName;
        ????????????????????}
        ????????????????????using?(var?stream?=?new?MemoryStream())
        ????????????????????{
        ????????????????????????workbook.SaveAs(stream);
        ????????????????????????var?content?=?stream.ToArray();
        ????????????????????????return?File(content,?contentType,?fileName);
        ????????????????????}
        ????????????????}
        ????????????}
        ????????????catch(Exception?ex)
        ????????????{
        ????????????????return?Error();
        ????????????}
        ????????}

        這篇就是 ClosedXML 的所有內(nèi)容,如果你想對(duì) Excel 中的數(shù)據(jù)進(jìn)行更加復(fù)雜的操控,可以使用 EPPlus 或者 NPOI,關(guān)于 ClosedXML 的更多內(nèi)容,可參考:https://github.com/ClosedXML/ClosedXML

        譯文鏈接:https://www.infoworld.com/article/3538413/how-to-export-data-to-excel-in-aspnet-core-30.html

        瀏覽 30
        點(diǎn)贊
        評(píng)論
        收藏
        分享

        手機(jī)掃一掃分享

        分享
        舉報(bào)
        評(píng)論
        圖片
        表情
        推薦
        點(diǎn)贊
        評(píng)論
        收藏
        分享

        手機(jī)掃一掃分享

        分享
        舉報(bào)
        1. <strong id="7actg"></strong>
        2. <table id="7actg"></table>

        3. <address id="7actg"></address>
          <address id="7actg"></address>
          1. <object id="7actg"><tt id="7actg"></tt></object>
            高清无码免费看 | 张柏芝裸体下面毛毛片 | 婷婷精品免费久久 | 超逼小视频 | 悠悠资源音影先锋在线观看 | 波多野结衣伦理电影 | 久草新在线 | ㊣最新国产の精品bt伙计 | 最新亚州人姓交 | 五用丁香花婷婷开心 |