C# 使用Epplus导出Excel [2]:导出动态列数据
标签:[] selector book pair font sum int objects region
上一篇导出excel,是导出已知固定列,有时候我们根本就不知道有几列、列名是什么,因此这些动态列,可以用Dictionary接收。
1、实体Student上加上一个字段Dictionarys
Student.cs
public class Student
{
public String Name { get; set; }
public String Code { get; set; }
public Dictionarystring, string> Dictionarys { get; set; }
}
2、表头表体类上加上动态列的添加表头与表体
EpplusHelper.cs
public static class EpplusHelper
{
///
/// 添加表头
///
///
///
public static void AddHeader(ExcelWorksheet sheet, params string[] headerTexts)
{
for (var i = 0; i )
{
AddHeader(sheet, i + 1, headerTexts[i]);
}
}
///
/// 添加表头
///
///
///
///
public static void AddHeader(ExcelWorksheet sheet, int columnIndex, string headerText)
{
sheet.Cells[1, columnIndex].Value = headerText;
sheet.Cells[1, columnIndex].Style.Font.Bold = true;
}
///
/// 添加数据
///
///
///
///
///
public static void AddObjects(ExcelWorksheet sheet, int startRowIndex, IList items, Funcobject>[] propertySelectors)
{
for (var i = 0; i )
{
for (var j = 0; j )
{
sheet.Cells[i + startRowIndex, j + 1].Value = propertySelectors[j](items[i]);
}
}
}
///
/// 添加动态表头
///
///
///
///
public static void AddHeader(ExcelWorksheet sheet, string[] headerTexts, string[] headerTextsDictionary)
{
for (var i = 0; i )
{
AddHeader(sheet, i + 1 + headerTexts.Length, headerTextsDictionary[i]);
}
}
///
/// 添加动态数据
///
///
///
///
///
///
public static void AddObjects(ExcelWorksheet sheet, int startRowIndex, IList items, Funcobject>[] propertySelectors, Liststring> dictionaryKeys)
{
for (var i = 0; i )
{
for (var j = 0; j )
{
sheet.Cells[i + startRowIndex, j + 1 + propertySelectors.Length].Value = items[i].Dictionarys[dictionaryKeys[j]];
}
}
}
public static List GetDictionaryKeys(Dictionarystring, string> dics)
{
Liststring> resultList = new Liststring>();
foreach (KeyValuePairstring, string> kvp in dics)
{
resultList.Add(kvp.Key);
}
return resultList;
}
}
3、修改Main方法,导出Excel
主要代码如下:
//获得数据
List studentList = new List();
for (int i = 0; i 10; i++)
{
Student s = new Student();
s.Code = "c" + i;
s.Name = "n" + i;
studentList.Add(s);
}
//获得不固定数据
for (int i = 0; i )
{
Dictionarystring, string> dictionarys = new Dictionarystring, string>();
dictionarys.Add("D1", "d1" + i);
dictionarys.Add("D2", "d2" + i);
studentList[i].Dictionarys = dictionarys;
}
//创建excel
string fileName = @"d:\" + "导出excel" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".xlsx";
FileInfo newFile = new FileInfo(fileName);
using (ExcelPackage package = new ExcelPackage(newFile))
{
#region 固定列
List> excelExportDtoList = new List>();
excelExportDtoList.Add(new ExcelExportDto("Code", _ => _.Code));
excelExportDtoList.Add(new ExcelExportDto("Name", _ => _.Name));
Liststring> columnsNameList = new Liststring>();
Listobject>> columnsValueList = new Listobject>>();
foreach (var item in excelExportDtoList)
{
columnsNameList.Add(item.ColumnName);
columnsValueList.Add(item.ColumnValue);
}
#endregion
#region 不固定列
Liststring, string>>> excelExportDictionaryDtoList = new Liststring, string>>>();
Liststring> columnsNameDictionaryList = new Liststring>();
Liststring> dictionaryKeys = EpplusHelper.GetDictionaryKeys(studentList[0].Dictionarys);
if (studentList.Count > 0)
{
for (int i = 0; i )
{
var index = i;
excelExportDictionaryDtoList.Add(new ExcelExportDtostring, string>>(dictionaryKeys[i], _ => _.FirstOrDefault(q => q.Key == dictionaryKeys[i]).Value));
}
foreach (var item in excelExportDictionaryDtoList)
{
columnsNameDictionaryList.Add(item.ColumnName);
}
}
#endregion
ExcelWorksheet worksheet = package.Workbook.Worksheets.Add("Test");
worksheet.OutLineApplyStyle = true;
//添加表头
EpplusHelper.AddHeader(worksheet, columnsNameList.ToArray());
//添加数据
EpplusHelper.AddObjects(worksheet, 2, studentList, columnsValueList.ToArray());
if (studentList.Count > 0)
{
//添加动态表头
EpplusHelper.AddHeader(worksheet, columnsNameList.ToArray(), columnsNameDictionaryList.ToArray());
//添加动态数据
EpplusHelper.AddObjects(worksheet, 2, studentList, columnsValueList.ToArray(), dictionaryKeys);
}
package.Save();
}
完整代码详情请移步我的github:https://github.com/gordongaogithub/ExportDictionaryExcelByEpplus.git
C# 使用Epplus导出Excel [2]:导出动态列数据
标签:[] selector book pair font sum int objects region
原文地址:https://www.cnblogs.com/jishugaochao/p/10345794.html
评论