當(dāng)前位置:首頁 >  站長 >  編程技術(shù) >  正文

Asp.Net Core實(shí)現(xiàn)Excel導(dǎo)出功能的實(shí)現(xiàn)方法

 2020-12-11 16:01  來源: 腳本之家   我來投稿 撤稿糾錯

  域名預(yù)訂/競價(jià),好“米”不錯過

這篇文章主要給大家介紹了關(guān)于Asp.Net Core實(shí)現(xiàn)Excel導(dǎo)出功能的實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

目錄

安裝 ClosedXML

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

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

下載 Excel

在web應(yīng)用程序開發(fā)時(shí),或許你會遇到這樣的需求,如何在 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,你可以通過可視化界面 NuGet package manager 去安裝,也可以使用命令行 NuGet package manager console 執(zhí)行下面命令。

Install-Package ClosedXML

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

將數(shù)據(jù)導(dǎo)成 CSV 文件是非常簡單的,畢竟每行數(shù)據(jù)都是用 , 隔開即可,可以用 NuGet 上的 CsvExport 或者 AWright18.SimpleCSVExporter 去實(shí)現(xiàn),可以親自操刀實(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 組成,下面的代碼可用來生成一個(gè) workbook。

var workbook = new XLWorkbook();

接下來生成一個(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) 再通過 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)容,如果你想對 Excel 中的數(shù)據(jù)進(jìn)行更加復(fù)雜的操控,可以使用Magicodes.IE,關(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

到此這篇關(guān)于Asp.Net Core實(shí)現(xiàn)Excel導(dǎo)出功能的文章就介紹到這了,更多相關(guān)Asp.Net Core實(shí)現(xiàn)Excel導(dǎo)出內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

來源:腳本之家

鏈接:https://www.jb51.net/article/201537.htm

申請創(chuàng)業(yè)報(bào)道,分享創(chuàng)業(yè)好點(diǎn)子。點(diǎn)擊此處,共同探討創(chuàng)業(yè)新機(jī)遇!

相關(guān)標(biāo)簽
asp.net
net開發(fā)
.net開發(fā)

相關(guān)文章

熱門排行

信息推薦