Yoko.Excel.Core 1.4.3

dotnet add package Yoko.Excel.Core --version 1.4.3
NuGet\Install-Package Yoko.Excel.Core -Version 1.4.3
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Yoko.Excel.Core" Version="1.4.3" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Yoko.Excel.Core --version 1.4.3
#r "nuget: Yoko.Excel.Core, 1.4.3"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Yoko.Excel.Core as a Cake Addin
#addin nuget:?package=Yoko.Excel.Core&version=1.4.3

// Install Yoko.Excel.Core as a Cake Tool
#tool nuget:?package=Yoko.Excel.Core&version=1.4.3

[TOC]

Yoko.Excel.Core

  • Excel导出工具,解决十万百万级大数据导出Excel内存占用问题
  • 建议使用IDataReader

更新内容

1.4.3

  • 【修复】IDataReader模式导出的值强制转为值类型 修改为 自动判断类型

1.4.2

  • 【优化】优化性能、修复已知BUG

1.4.0

  • 【优化】动态字段映射系列优化,用法见下所示

1.3.28

  • 【优化】移除 System.Memory 依赖
  • 【优化】导出时,DateTime类型支持自定义格式,如: Format = "yyyyMMddHHmmss"
  • 【新增】SaveAs 对 List<object>的自定义样式支持

1.3.2

  • 【优化】优化部分性能

1.3.1

  • 【优化】导出时判断指定目录是否存在,不存在则创建

1.3.0

  • 【新增】读取Excel

1.2.0

  • 【新增】自定义指定列宽 ColumnWidth
  • 【新增】自定义指定列是否忽略 ExcelIgnore
  • 【修改】列为DateTime类型时,强制显示文本格式为 yyyy-MM-dd HH:mm:ss
  • 【修改】优化包体积

特点

  • 低内存耗用,避免OOM、频繁 FullGC 情况

安装

NuGet搜索 Yoko.Excel.Core

导出 Excel

除非必要,不要用 ToList,ToDatatable 等方法读取全部数据到内存!!!

除非必要,不要用 ToList,ToDatatable 等方法读取全部数据到内存!!!

除非必要,不要用 ToList,ToDatatable 等方法读取全部数据到内存!!!

1. IDataReader
  • 推荐使用,可以避免载入全部数据到内存
YokoExcel.SaveAs(path, reader);
  • DataReader 导出方式
var path = Path.Combine(@"D:\App.Excel\", $"{Guid.NewGuid()}.xlsx");
//=========== IDataReader  ===========
using var reader = ExecuteReader();
//===============================
//自定义的动态字段映射
var config = new OpenXmlConfiguration()
{
    DynamicColumns = new DynamicExcelColumn[]
    {
        new DynamicExcelColumn("OrderCode"){ Name = "订单号", Width = 50 },
        new DynamicExcelColumn("Name") { Name = "名称", Width = 30 },
        new DynamicExcelColumn("PayType") { Name = "支付类型", Width = 20, Enum = new Hashtable { { 0, "PDA扫码支付" }, { 1, "余额支付" }, { 2, "微信支付" }, { 3, "支付宝支付" }, { 4, "银联支付" }, { 5, "线下支付" }, { 6, "建行支付" } } },
        new DynamicExcelColumn("PayTime") {Name = "支付时间", Width = 25, Format="yyyy-MM-dd HH:mm:ss"}
    }
};
//导出-原始数据
//YokoExcel.SaveAs(path, reader);
//导出-自定义的动态字段映射
YokoExcel.SaveAs(path, reader, sheetName: "订单报表", configuration: config);
2. 支持集合<匿名类别>
var path = Path.Combine(Path.GetTempPath(), $"{Guid.NewGuid()}.xlsx");
YokoExcel.SaveAs(path, new[] {
    new { Column1 = "YokoExcel001", Column2 = 1 },
    new { Column1 = "YokoExcel002", Column2 = 2}
});
3. IEnumerable<IDictionary<string, object>>
var values = new List<Dictionary<string, object>>()
{
    new Dictionary<string,object>{{ "Col1", "YokoExcel001" }, { "Col0001", 1 } },
    new Dictionary<string,object>{{ "Col2", "YokoExcel002" }, { "Col0002", 2 } }
};
YokoExcel.SaveAs(path, values);
4. Datatable
  • 不推荐使用,会将数据全载入内存
var path = Path.Combine(Path.GetTempPath(), $"{Guid.NewGuid()}.xlsx");
var table = new DataTable();
{
    table.Columns.Add("Column1", typeof(string));
    table.Columns.Add("Column2", typeof(decimal));
    table.Rows.Add("YokoExcel111", 1);
    table.Rows.Add("YokoExcel222", 2);
}
YokoExcel.SaveAs(path, table);
5. SaveAs 支持 Stream
  • 生成文件不落地
using (var stream = new MemoryStream()) //支持 FileStream,MemoryStream..等
{
    stream.SaveAs(values);
}
  • DemoAPI 导出 Excel
public IActionResult DownloadExcel()
{
    //
    var values = new[] {
        new { Column1 = "YokoExcel", Column2 = 1 },
        new { Column1 = "YokoExcel222", Column2 = 2}
    };
    
    var memoryStream = new MemoryStream();
    memoryStream.SaveAs(values);
    memoryStream.Seek(0, SeekOrigin.Begin);
    
    // or 
    
    // IDataReader 
    using var db = new SqlConnection("....");
	using var cmd = new SqlCommand("select * from table", db);
	using var reader = await cmd.ExecuteReaderAsync(CommandBehavior.SequentialAccess);

	var memoryStream = new MemoryStream();
	await MiniExcel.SaveAsAsync(excelStream, reader);
	excelStream.Seek(0, SeekOrigin.Begin);
    
    
    return new FileStreamResult(memoryStream, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
    {
        FileDownloadName = "demo001.xlsx"
    };
}

读取 Excel

  • 实体对象
var path = @"D:\demo.xlsx";
var data = YokoExcel.Query<Order>(path);

// 或者
using var stream = File.OpenRead(path);
var data = stream.Query<Order>();
  • 匿名动态对象
var data = YokoExcel.Query(path).ToList();

//或者 
using var stream = File.OpenRead(path);
var data = stream.Query().ToList();
Product Compatible and additional computed target framework versions.
.NET net5.0 is compatible.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 is compatible.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETFramework 4.8

    • No dependencies.
  • .NETStandard 2.0

    • No dependencies.
  • net5.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.4.3 558 4/24/2023
1.4.2 712 10/28/2022
1.4.1 741 10/27/2022
1.4.0 769 10/27/2022
1.3.28 734 10/24/2022
1.3.2 738 5/7/2022
1.3.1 802 4/29/2022
1.3.0 736 4/14/2022
1.2.0 804 4/7/2022
1.0.1 760 3/29/2022