3.3 C#逻辑操作代码
说明:对Excel转换后的List<T>进行后续操作;如:检测有效性、持久化存储等等
步骤:
①获取List<T>集合。
②调用3.2,将List<T>转换为Excel文件。
③服务器存储Excel文件并返回下载链接。
代码:
public void ExportExcel(HttpContext context)
{
try
{
// 1.获取数据集合
List<UserEntity> enlist = new List<UserEntity>() {
new UserEntity{Name="刘一",Age=22,Gender="Male",TranscriptsEn=new TranscriptsEntity{ChineseScores=80,MathScores=90}},
new UserEntity{Name="陈二",Age=23,Gender="Male",TranscriptsEn=new TranscriptsEntity{ChineseScores=81,MathScores=91} },
new UserEntity{Name="张三",Age=24,Gender="Male",TranscriptsEn=new TranscriptsEntity{ChineseScores=82,MathScores=92} },
new UserEntity{Name="李四",Age=25,Gender="Male",TranscriptsEn=new TranscriptsEntity{ChineseScores=83,MathScores=93} },
new UserEntity{Name="王五",Age=26,Gender="Male",TranscriptsEn=new TranscriptsEntity{ChineseScores=84,MathScores=94} },
};
// 2.设置单元格抬头
// key:实体对象属性名称,可通过反射获取值
// value:Excel列的名称
Dictionary<string, string> cellheader = new Dictionary<string, string> {
{ "Name", "姓名" },
{ "Age", "年龄" },
{ "GenderName", "性别" },
{ "TranscriptsEn.ChineseScores", "语文成绩" },
{ "TranscriptsEn.MathScores", "数学成绩" },
};
// 3.进行Excel转换操作,并返回转换的文件下载链接
string urlPath = ExcelHelper.EntityListToExcel2003(cellheader, enlist, "学生成绩");
System.Web.Script.Serialization.JavaScriptSerializer js = new System.Web.Script.Serialization.JavaScriptSerializer();
context.Response.ContentType = "text/plain";
context.Response.Write(js.Serialize(urlPath)); // 返回Json格式的内容
}
catch (Exception ex)
{
throw ex;
}
}
3.4 代码分析
核心代码主要是cellheader与List<T>之间的映射关系:










