1. 程式人生 > 其它 >.netcore excel匯出回車換行_在.NET Core(C#)中使用EPPlus.Core匯出Excel文件

.netcore excel匯出回車換行_在.NET Core(C#)中使用EPPlus.Core匯出Excel文件

技術標籤:.netcore excel匯出回車換行

首先安裝

直接NuGet安裝就行,賊方便。

使用方法

有很多種辦法匯出,本文只介紹最方便,程式碼最少的那種。

首先定義一個實體類,例如:

public class OutputData {
    public string Address { get; set; }
    public int Title { get; set; }
    public int Description { get; set; }
    public int Keywords { get; set; }
    public int Content { get; set; }
    public int Score { get; set; }
}

然後直接傳一個 OutputData 型別的列表就能輸出Excel檔案了。

private static void ExportExcel(IEnumerable<OutputData> results) {
    using var package = new ExcelPackage();
    var worksheet = package.Workbook.Worksheets.Add("結果匯出");
    worksheet.Cells.LoadFromCollection(results, true);
    package.SaveAs(new FileStream("output.xlsx", FileMode.Create));
}

注:worksheet.Cells.LoadFromCollection()的第二個引數表示輸出表頭~

參考資料

  • 專案主頁:https://github.com/EPPlusSoftware/EPPlus
  • https://my.oschina.net/u/4380330/blog/3413607