[英]Export to Excel with Predicate Builder
I am researching and trying to find a way to export specific data to an excel spreadsheet. 我正在研究并试图找到一种将特定数据导出到Excel电子表格的方法。
The sites that I have found are similar to this , where they are returning every record in the database. 我发现的站点与此相似,它们在其中返回数据库中的每个记录。
In my web application, I have a View that does return all records in the database, but I also use Predicate Builder
so that the user can sort through records based on their specific parameters. 在我的Web应用程序中,我有一个视图,该视图确实返回数据库中的所有记录,但是我也使用
Predicate Builder
以便用户可以根据其特定参数对记录进行排序。 The user's that will be using this application will have 0 use/interest for exporting every record to excel. 将使用此应用程序的用户将有0个使用/兴趣,将每个记录导出到excel。
So, my question is how do I combine the parameters (predicate) with a method that exports to excel? 因此,我的问题是如何将参数(谓词)与导出为excel的方法结合起来?
I don't have any code to show because I haven't found any examples in my research to provide me with a foundation for how to combine parameters with exporting to excel. 我没有要显示的代码,因为我在研究中没有找到任何示例来为我提供如何将参数与导出结合到excel的基础。
I do not use PredicateBuilder in my code, but i made an example using it. 我没有在代码中使用PredicateBuilder,但是我举了一个使用它的示例。 Maybe you have to adapt something.
也许你必须适应一些东西。
public ActionResult Export(string PatientName, string BirthDate, string Gender, string PatientType)
{
ViewBag.PatientName = PatientName ?? "";
ViewBag.BirthDate = BirthDate ?? "";
ViewBag.Gender = Gender ?? "";
ViewBag.PatientType = PatientType ?? "";
var predicate = PredicateBuilder.True<Patient>();
if (!string.IsNullOrEmpty(PatientName))
{
predicate = predicate.And(i => i.FirstName.ToLower().StartsWith(PatientName) || i.LastName.ToLower().StartsWith(PatientName));
}
if (!string.IsNullOrEmpty(Gender))
{
int gender;
Int32.TryParse(Gender, out gender);
predicate = predicate.And(i => i.Gender == gender);
}
if (!string.IsNullOrEmpty(PatientType))
{
int type;
Int32.TryParse(PatientType, out type);
predicate = predicate.And(i => i.PatientType == type);
}
if (!string.IsNullOrEmpty(BirthDate))
{
DateTime dob;
DateTime.TryParse(BirthDate, out dob);
predicate = predicate.And(i => EntityFunctions.TruncateTime(i.BirthDate) == EntityFunctions.TruncateTime(dob));
}
var patients = db.Patients.Where(predicate).Select(i => i).Include(p => p.DropDownOption).Include(p => p.DropDownOption1);
GridView gv = new GridView();
gv.DataSource = patients.ToList();
gv.DataBind();
Response.ClearContent();
Response.Buffer = true;
Response.AddHeader("content-disposition", "attachment; filename=IbiliPasswords.xls");
Response.ContentType = "application/ms-excel";
Response.Charset = "";
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
gv.RenderControl(htw);
Response.Output.Write(sw.ToString());
Response.Flush();
Response.End();
return RedirectToAction("Index");
}
Index.cshtml: Index.cshtml:
@Html.ActionLink("Export Info", "Export", new { PatientName = ViewBag.PatientName, BirthDate= ViewBag.BirthDate, Gender= ViewBag.Gender, PatientType= ViewBag.PatientType})
Hope it helps you. 希望对您有帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.