在asp.net mvc中使用rotativa包的小例子
我们将创建一个函数来填充数据。我们将插入 7 天(2018 年 2 月 1 日 - 2018 年 2 月 7 日)的数据,显示特定日期的第一拳和最后一拳并附上备注。
public ReportViewModel PopulateData()
{
var attendances = new List<Attendance>
{
new Attendance{ClassName = "present",Day = new DateTime(2018, 02, 01).ToString("ddd"),Date = new DateTime(2018, 02, 01).ToString("d"),FirstPunch = "09:01:00",LastPunch = "06:00:01",Remarks = ""},
new Attendance{ClassName = "absent",Day = new DateTime(2018, 02, 02).ToString("ddd"),Date = new DateTime(2018, 02, 02).ToString("d"),FirstPunch = "",LastPunch = "",Remarks = "Absent"},
new Attendance{ClassName = "holiday",Day = new DateTime(2018, 02, 03).ToString("ddd"),Date = new DateTime(2018, 02, 03).ToString("d"),FirstPunch = "",LastPunch = "",Remarks = "Democracy Day"},
new Attendance{ClassName = "present",Day = new DateTime(2018, 02, 04).ToString("ddd"),Date = new DateTime(2018, 02, 04).ToString("d"),FirstPunch = "09:05:00",LastPunch = "06:30:01",Remarks = ""},
new Attendance{ClassName = "present",Day = new DateTime(2018, 02, 05).ToString("ddd"),Date = new DateTime(2018, 02, 05).ToString("d"),FirstPunch = "09:01:00",LastPunch = "06:00:01",Remarks = ""},
new Attendance{ClassName = "leave",Day = new DateTime(2018, 02, 06).ToString("ddd"),Date = new DateTime(2018, 02, 06).ToString("d"),FirstPunch = "",LastPunch = "",Remarks = "Sick Leave"},
new Attendance{ClassName = "present",Day = new DateTime(2018, 02, 07).ToString("ddd"),Date = new DateTime(2018, 02, 07).ToString("d"),FirstPunch = "08:35:00",LastPunch = "06:15:01",Remarks = ""}
};
return new ReportViewModel
{
UserInformation = new UserInformation
{
FullName = "Ritesh Man Chitrakar",
Department = "Information Science"
},
StartDate = new DateTime(2018, 02, 01),
EndDate = new DateTime(2018, 02, 07),
AttendanceData = attendances
};
}
然后我们将创建一个下载 PDF 的函数。要下载 pdf,我们需要创建 2 个函数。
1.下载pdf
2.查看pdf
public ActionResult DownloadPdf()
{
var filename = "attendance.pdf";
/*get the current login cookie*/
var cookies = Request.Cookies.AllKeys.ToDictionary(k => k, k => Request.Cookies[k]?.Value);
return new ActionAsPdf("PdfView", new
{
startDate = Convert.ToDateTime(Request["StartDate"]),
endDate = Convert.ToDateTime(Request["EndDate"])
})
{
FileName = filename,
/*pass the retrieved cookie inside the cookie option*/
RotativaOptions = {Cookies = cookies}
};
}
public ActionResult PdfView()
{
var reportAttendanceData = PopulateData();
return View(reportAttendanceData);
}
您可以在此链接上查看详细说明。
访问here。
柯特索伊:thelearninguy.com