65 Star 337 Fork 86

magicodes / Magicodes.IE

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
5.Dynamic Export.md 2.31 KB
一键复制 编辑 原始数据 按行查看 历史
just42 提交于 2021-03-24 15:31 . 新增英语翻译

Dynamic export of table headers

The following code snippet allows us to export the Excel table header, we can pass it as an array to generate our dynamic table header, and at the same time call it as exporter.ExportHeaderAsByteArray and export our result of type byte[] to the specified filepath(path ).

public async Task ExportHeaderAsByteArrayWithItems_Test()
{
    IExcelExporter exporter = new ExcelExporter();
    var arr = new[] { "Name1", "Name2", "Name3", "Name4", "Name5", "Name6" };
    var sheetName = "Test";
    var result = await exporter.ExportHeaderAsByteArray(arr, sheetName);
    
    result.ToExcelExportFileInfo(filePath);
}

DDataTable Dynamic Export

In this export exportDatas is a List, but in the following code snippet we have converted it to a DataTable and exported it, maxRowNumberOnASheet is the number of rows each sheet holds.

public async Task LargeDataDynamicExport_Test()
{
    IExcelExporter exporter = new ExcelExporter();
   
    var dt = new DataTable();
    //Create columns with column names and type names
    dt.Columns.Add("Text", Type.GetType("System.String"));
    dt.Columns.Add("Name", Type.GetType("System.String"));
    dt.Columns.Add("Number", Type.GetType("System.Decimal"));
    dt = EntityToDataTable(dt, exportDatas);

    var result = await exporter.Export(filePath, dt, maxRowNumberOnASheet: 100000);
}

ExpandoObject Export

ExpandoObject is a dynamic object that is part of the Dynamic Language (DLR). With ExpandoObject we can add and remove members of its instances at runtime, and of course set and get these values. In IE we support ExpandoObject objects, so here we can use ExpandoObject to implement our dynamic export behavior.

public async Task ExportAsByteArraySupportDynamicType_Test()
{
    IExporter exporter = new ExcelExporter();
    
    var source = GenFu.GenFu.ListOf<ExportTestDataWithAttrs>();
    string fields = "text,number,name";
    var shapedData = source.ShapeData(fields) as ICollection<ExpandoObject>;

    var result = await exporter.ExportAsByteArray<ExpandoObject>(shapedData);
    File.WriteAllBytes(filePath, result);
}

In addition, for ExpandoObject export process for table headers and other information, you can use filters to achieve, please refer to the tutorial article on the use of filters for details.

C#
1
https://gitee.com/magicodes/Magicodes.IE.git
git@gitee.com:magicodes/Magicodes.IE.git
magicodes
Magicodes.IE
Magicodes.IE
master

搜索帮助