日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > asp.net >内容正文

asp.net

如何在 Asp.Net Core 实现 Excel 导出功能

發(fā)布時(shí)間:2023/12/4 asp.net 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 如何在 Asp.Net Core 实现 Excel 导出功能 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

在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<Author>?authors?=?new?List<Author> {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

總結(jié)

以上是生活随笔為你收集整理的如何在 Asp.Net Core 实现 Excel 导出功能的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。