C# 将临时属性添加到对象列表中
我有三种型号C# 将临时属性添加到对象列表中,c#,asp.net-mvc,list,C#,Asp.net Mvc,List,我有三种型号 public class Payroll { public int ID { get; set; } public DateTime Date { get; set; } public int PayCategoryID { get; set; } public virtual PayCategory PayCategory { get; set; } } public ActionResult ExportData() {
public class Payroll
{
public int ID { get; set; }
public DateTime Date { get; set; }
public int PayCategoryID { get; set; }
public virtual PayCategory PayCategory { get; set; }
}
public ActionResult ExportData()
{
var payrolls = (List<Payroll>)Session["payrolls"];
ExportToExel<Payroll>.Export(payrolls, "Payroll", this);
return RedirectToAction("Index");
}
这个:
public class PayCategory
{
public int ID { get; set; }
public string PayScenario { get; set; }
public int PayGroupID { get; set; }
public virtual PayGroup PayGroup { get; set; }
}
public ActionResult ExportData()
{
var payrolls = (List<Payroll>)Session["payrolls"];
ExportToExel<Payroll>.Export(payrolls, "Payroll", this);
return RedirectToAction("Index");
}
还有这个:
public class PayGroup
{
public int ID { get; set; }
public string Label { get; set; }
public string Description { get; set; }
public string EntryType { get; set; }
}
public ActionResult ExportData()
{
var payrolls = (List<Payroll>)Session["payrolls"];
ExportToExel<Payroll>.Export(payrolls, "Payroll", this);
return RedirectToAction("Index");
}
如果我创建一个工资单列表,我只会得到一个工资单对象列表,每个对象有4个属性。
我想要一个列表,其中每个工资对象都有字段
日期、付款方案、标签、说明和条目类型。我知道这些可以很容易地通过
Payroll.PayCategory.PayScenario
Payroll.PayCategory.PayGroup.Label
public ActionResult ExportData()
{
var payrolls = (List<Payroll>)Session["payrolls"];
ExportToExel<Payroll>.Export(payrolls, "Payroll", this);
return RedirectToAction("Index");
}
等等
public ActionResult ExportData()
{
var payrolls = (List<Payroll>)Session["payrolls"];
ExportToExel<Payroll>.Export(payrolls, "Payroll", this);
return RedirectToAction("Index");
}
但我正在使用以下通用方法将其导出到excel文档:
public static void Export(List<T> data, string name, Controller controller)
{
XLWorkbook workbook = new XLWorkbook();
var worksheet = workbook.Worksheets.Add(name);
worksheet.Cell(1, 1).InsertTable(data);
controller.Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheet.sheet";
controller.Response.AddHeader("content-disposition", String.Format(@"attachment;filename={0}.xlsx", name.Replace(" ", "_")));
using (MemoryStream memoryStream = new MemoryStream())
{
workbook.SaveAs(memoryStream);
memoryStream.WriteTo(controller.Response.OutputStream);
memoryStream.Close();
}
controller.Response.End();
}
public ActionResult ExportData()
{
var payrolls = (List<Payroll>)Session["payrolls"];
ExportToExel<Payroll>.Export(payrolls, "Payroll", this);
return RedirectToAction("Index");
}
publicstaticvoid导出(列表数据、字符串名称、控制器)
{
xl工作簿=新的xl工作簿();
var工作表=工作簿.工作表.添加(名称);
工作表。单元格(1,1)。插入表(数据);
controller.Response.ContentType=“application/vnd.openxmlformats of icedocument.spreadsheet.sheet”;
controller.Response.AddHeader(“内容处置”,String.Format(@“附件;文件名={0}.xlsx”,name.Replace(“,”));
使用(MemoryStream MemoryStream=new MemoryStream())
{
工作簿.SaveAs(memoryStream);
memoryStream.WriteTo(controller.Response.OutputStream);
memoryStream.Close();
}
controller.Response.End();
}
我从控制器上这样呼叫:
public ActionResult ExportData()
{
var payrolls = (List<Payroll>)Session["payrolls"];
ExportToExel<Payroll>.Export(payrolls, "Payroll", this);
return RedirectToAction("Index");
}
public ActionResult ExportData()
{
var工资总额=(列表)时段[“工资总额”];
ExportToExel.Export(工资单,“工资单”,本);
返回操作(“索引”);
}
当然,结果给了我一个简单的表,其中只有值日期(加上一些其他垃圾列,如System[…]PayCategory)
public ActionResult ExportData()
{
var payrolls = (List<Payroll>)Session["payrolls"];
ExportToExel<Payroll>.Export(payrolls, "Payroll", this);
return RedirectToAction("Index");
}
最后,我希望合并这3个模型的属性(不包括ID),以便为我提供一个可以传递给ExportData方法的列表。如中所示,是否有方法将列添加到对象列表中?类似于
public ActionResult ExportData()
{
var payrolls = (List<Payroll>)Session["payrolls"];
ExportToExel<Payroll>.Export(payrolls, "Payroll", this);
return RedirectToAction("Index");
}
for(int i = 1; i<payrolls.Count; i++)
payrolls[i].Add.(payrolls[i].PayCategory.PayScenario);
for(inti=1;i您要查找的是一个“匿名对象”
public ActionResult ExportData()
{
var payrolls = (List<Payroll>)Session["payrolls"];
ExportToExel<Payroll>.Export(payrolls, "Payroll", this);
return RedirectToAction("Index");
}
用法示例:
public ActionResult ExportData()
{
var payrolls = (List<Payroll>)Session["payrolls"];
ExportToExel<Payroll>.Export(payrolls, "Payroll", this);
return RedirectToAction("Index");
}
这里有一篇关于他们的文章
public ActionResult ExportData()
{
var payrolls = (List<Payroll>)Session["payrolls"];
ExportToExel<Payroll>.Export(payrolls, "Payroll", this);
return RedirectToAction("Index");
}
这里的建议是在循环中创建一个新的匿名对象,并将其传递到excel函数,而不是原始的payrollObject。如果可以导入Linq,可以执行以下操作:
public ActionResult ExportData()
{
var payrolls = (List<Payroll>)Session["payrolls"];
ExportToExel<Payroll>.Export(payrolls, "Payroll", this);
return RedirectToAction("Index");
}
var payrolls = from aPayroll in (List<Payroll>)Session["payrolls"]
select new {
Date = aPayroll.Date,
PayScenario = aPayroll.PayCategory.PayScenario,
Label = aPayroll.PayGroup.Label,
Description = aPayroll.PayGroup.Description,
EntryType = aPayroll.PayGroup.EntryType
};
var payrolls=来自(列表)会话中的aPayroll[“payrolls”]
选择新的{
日期=aPayroll.日期,
PayScenario=aPayroll.PayCategory.PayScenario,
Label=aPayroll.PayGroup.Label,
Description=aPayroll.PayGroup.Description,
EntryType=aPayroll.PayGroup.EntryType
};
这将创建一个具有您想要的五个属性的匿名类型
public ActionResult ExportData()
{
var payrolls = (List<Payroll>)Session["payrolls"];
ExportToExel<Payroll>.Export(payrolls, "Payroll", this);
return RedirectToAction("Index");
}
假设您使用的是ClosedXML,您也应该能够也许您应该为此用途创建新类?接下来,您可以简单地将Payroll类及其引用映射到新创建的类(例如,通过AutoMapper)。我写这篇文章花了太长时间,@user1698727是对的。我如何声明excel函数?公共静态无效导出(列表数据、字符串名称、控制器),列表不会让anon对象通过编译NVM我用列表获得它
public ActionResult ExportData()
{
var payrolls = (List<Payroll>)Session["payrolls"];
ExportToExel<Payroll>.Export(payrolls, "Payroll", this);
return RedirectToAction("Index");
}