这里我试图获得正确的持续时间,但我无法在持续时间列中添加Punch并打出时间。
我试过,但根据我的要求帮助我,我得不到正确的结果。当Excel文件下载时,它应显示正确的持续时间。
@(Html.Kendo().Grid(Model)
.Name("TimeTrackingLogsGrid")
.Columns(columns =>
{
columns.Bound(c => c.Id).Hidden(true).HtmlAttributes(new { style = "text-align:center;" });
columns.Bound(c => c.IsDeleted).Hidden(true);
columns.Bound(c => c.ApprovalRequired).Visible((bool)ViewBag.IsAdmin).ClientTemplate("# if (ApprovalRequired == true && Approved == false && '" + (ViewBag.IsAdmin).ToString() + "'=== 'True') {# Yes: <a data-ajax='true' data-ajax-complete='refreshGrid' data-ajax-method='POST' href='/Punch/Approve/#:Id#' class='grid-link' >Approve</a>#} else{# No #}#");
columns.Bound(c => c.EmployeeName).Filterable(ftb => ftb.Multi(true).Search(true));
columns.Bound(c => c.Month).Filterable(ftb => ftb.Multi(true));
columns.Bound(c => c.PunchDate)
.ClientFooterTemplate("#= getFormattdDate(data.PunchDate.min) # - #= getFormattdDate(data.PunchDate.max) # ")
.ClientGroupHeaderTemplate("#:convetUtcDateToLocalDateByInput(value)#")
.HtmlAttributes(new { @class = "utc-date", utc_date_value = "#:PunchDate#" });
columns.Bound(c => c.PunchInPic).Hidden(true).Visible((bool)ViewBag.IsAdmin).ClientTemplate("<img class='punchPic' width='100' src='data: image / png; base64,#:PunchInPic#'/>").HtmlAttributes(new { style = "text-align:center;" });
columns.Bound(c => c.PunchIn).Groupable(false).Filterable(false).ClientTemplate("#:PunchIn.Hours#:#:PunchIn.Minutes#").HtmlAttributes(new { @class = "utc-time", utc_time_value = "#:PunchIn.Hours#:#:PunchIn.Minutes#" });
columns.Bound(c => c.PunchOutPic).Hidden(true).Visible((bool)ViewBag.IsAdmin).ClientTemplate("<img class='punchPic' width='100' src='data: image / png; base64,#:PunchOutPic#'/>").HtmlAttributes(new { style = "text-align:center;" });
columns.Bound(c => c.PunchOut).Groupable(false).Filterable(false).ClientTemplate("#:PunchOut == null ? 0 :PunchOut.Hours #:#: PunchOut == null ? 0 : PunchOut.Minutes #").HtmlAttributes(new { @class = "utc-time", utc_time_value = "#: PunchOut == null ? 0 : PunchOut.Hours#:#:PunchOut == null ? 0 : PunchOut.Minutes #" });
columns.Bound(c => c.Duration).Groupable(false).Filterable(false)
.ClientFooterTemplate("#= SecondsToTime(data.DurationInSeconds.sum) # ")
.ClientTemplate("#:formatDuration(Duration.Hours,Duration.Minutes) #").HtmlAttributes(new { @class = "" });
columns.Bound(c => c.Comments).Visible((bool)ViewBag.IsAdmin).Filterable(false).Groupable(false).ClientTemplate("# if (ApprovalRequired == true && Approved == false && '" + (ViewBag.IsAdmin).ToString() + "' !== 'True') {# Please get this time log approved #} else{#<span class='comments' (#:Comments# == null ? '' : title='<ul> #:Comments# </ul>')> view <span>#}#");
columns.Command(command =>
{
// command.Edit().Text(" ");
command.Destroy().Text(" ");
}).Width(50);
})
.HtmlAttributes(new { @class = "" })
//.Scrollable()
.Groupable()
.Sortable()
.Filterable()
.Navigatable()
.ColumnMenu()
.ToolBar(toolbar =>
{
//toolbar.Create();
//toolbar.Save();
//toolbar.Pdf();
toolbar.Excel();
toolbar.Custom().Text("Clear Filter").HtmlAttributes(new { @class = "clearGridFilter" });
})
.Excel(exl => exl.AllPages(true).Filterable(true).FileName("Timesheet.xlsx")
.ProxyURL(Url.Action("ExportSave", "Punch")))
.Pdf(pdf => pdf
.AllPages()
.Title("Time Tracker Report")
.Subject("Daily Activity")
.AvoidLinks()
.PaperSize("A4")
.Scale(0.8)
.Margin("2cm", "1cm", "1cm", "1cm")
.Landscape()
.RepeatHeaders()
.TemplateId("page-template")
.FileName("OfficeGx Result")
.ProxyURL(Url.Action("ExportSave", "Punch"))
)
.Events(events => events.DataBound("convertUtcToLocal").PdfExport("onPunchReportPdfExport").ExcelExport("onPunchReportExcelExport"))
//.Editable(editable => editable.Mode(GridEditMode.InCell))
.Pageable(pageable => pageable
.Refresh(true)
.PageSizes(new int[] { 10, 20, 50, 100, 500 })
.ButtonCount(5))
.Resizable(resize => resize.Columns(true))
.Reorderable(reorder => reorder.Columns(true))
.DataSource(dataSource => dataSource
.Ajax()
.Filter(f =>
{
f.Add(field => field.IsDeleted).IsEqualTo(false);
//f.Add(field => field.PunchDate).IsGreaterThan(DateTime.Now.AddDays(-1));
})
.Batch(true)
.PageSize(20)
.ServerOperation(false)
.Events(events => events.Error("gridErrorHandler"))
.Aggregates(aggrgates => aggrgates.Add(x => x.DurationInSeconds).Sum())
.Aggregates(aggrgates => aggrgates.Add(x => x.PunchDate).Min())
.Aggregates(aggrgates => aggrgates.Add(x => x.PunchDate).Max())
.Model(model =>
{
model.Id(x => x.Id);
model.Field(c => c.Id).Editable(false);
})
.Sort(sort =>
{
sort.Add(field => field.PunchDate).Descending();
})
//.Group(groups=> groups.Add(x=>x.EmployeeName))
.Group(groups =>
{
//groups.Add(x => x.Month);
})
.Read(read => read.Action("Read", "Punch"))
//.Create(update => update.Action("Add", "Ticket"))
//.Update(update => update.Action("Update", "Punch"))
.Destroy(update => update.Action("Delete", "Punch"))
)
)
这是.CS文件
public class Punch
{
[Key]
public int Id { get; set; }
public DateTime PunchDate { get; set; }
public TimeSpan PunchIn { get; set; }
[NotMapped]
public string Month {
get
{
var In = TimeZoneInfo.ConvertTimeFromUtc(PunchDate.Date + PunchIn,
TimeZoneInfo.FindSystemTimeZoneById(User.RegisteredTimeZone));
return In.ToString("MMMM");
}
}
public TimeSpan? PunchOut { get; set; }
public int UserId { get; set; }
public string UserGuid { get; set; }
public bool Approved { get; set; }
public bool ApprovalRequired { get; set; }
//[ScaffoldColumn()]
public string Comments { get; set; }
public string IpAddress { get; set; }
public string MacAddress { get; set; }
[NotMapped]
public string EmployeeName => User != null ? $"{User.FirstName} {User.LastName}" : string.Empty;
public int Hours
{
get { return PunchOut.HasValue ? Convert.ToInt32(PunchOut.Value.Subtract(PunchIn).TotalHours) : 0; }
set { }
}
public double DurationInSeconds { get; set; }
[NotMapped]
public TimeSpan Duration
{
get
{
var Out = PunchOut.HasValue
? TimeZoneInfo.ConvertTimeFromUtc(PunchDate.Date + PunchOut.Value,
TimeZoneInfo.FindSystemTimeZoneById(User == null ? "India Standard Time" : User.RegisteredTimeZone))
: DateTime.MinValue;
var In = TimeZoneInfo.ConvertTimeFromUtc(PunchDate.Date + PunchIn,
TimeZoneInfo.FindSystemTimeZoneById(User == null ? "India Standard Time": User.RegisteredTimeZone));
if (PunchOut.HasValue)
{
var duration = new TimeSpan(Out.TimeOfDay.Hours, Out.TimeOfDay.Minutes, Out.TimeOfDay.Seconds) -
new TimeSpan(In.TimeOfDay.Hours, In.TimeOfDay.Minutes, In.TimeOfDay.Seconds);
return duration;
}
return DateTime.MinValue.TimeOfDay;
}
set { }
}
public int ApprovedHours
{
get
{
if (PunchOut != null)
return ApprovalRequired
? (Approved ? PunchOut.Value.Subtract(PunchIn).Seconds : 0)
: PunchOut.Value.Subtract(PunchIn).Seconds;
return 0;
}
set { }
}
这是.JS文件
function onPunchReportExcelExport(e) {
var sheet = e.workbook.sheets[0];
var rows = sheet.rows;
var rowIdx, colIdx, cells, cell;
for (rowIdx = 0; rowIdx < rows.length; rowIdx++) {
if (rows[rowIdx].type === "data") {
cells = rows[rowIdx].cells;
var color = "#ffffff";
if (cells[0].value === true) {
color = "#ff0000";
}
for (colIdx = sheet.freezePane.colSplit; colIdx < cells.length; colIdx++) {
debugger;
cell = cells[colIdx];
cell.background = color;
//cell.value = kendo.toString(cell.value, "c");
if (colIdx === 6) {
cell.value = cell.value.Hours + ":" + cell.value.Minutes;
}
else if (cell.value !== undefined && cell.value !== null && cell.value.Hours !== undefined) {
cell.value = utcTimeToLocalTime(cell.value.Hours + ":" + cell.value.Minutes);
}
else if (cell.value !== undefined && cell.value !== null && cell.value === true) {
cell.value = "Yes";
}
else if (cell.value !== undefined && cell.value !== null && cell.value === false) {
cell.value = "No";
}
}
}
}
}
$(function () {
var exportFlag = false;
$("#TimeTrackingLogsGrid").data("kendoGrid").bind("excelExport",
function (e) {
if (!exportFlag) {
var sheet = e.workbook.sheets[0];
var columns = e.sender.columns;
//jQuery.each(columns,
// function (index) {
// if (this.template) {
// var template = kendo.template(this.template);
// for (var i = 1; i < sheet.rows.length; i++) {
// var row = sheet.rows[i];
// var dataItem = {
// UnitPrice: row.cells[index].value
// };
// row.cells[index].value = template(dataItem);
// }
// }
// });
//var columns = e.sender.columns;
//jQuery.each(columns,
// function (index) {
// if (this.attributes !== undefined && this.attributes.class !== undefined) {
// if (this.attributes.class === "excel-export-field-hide") {
// this.hidden = true;
// }
// if (this.attributes.class === "excel-export-field-show") {
// this.hidden = false;
// }
// }
// });
//e.sender.showColumn(e.sender.columns.length + 1);
e.preventDefault();
exportFlag = true;
setTimeout(function () {
e.sender.saveAsExcel();
},
1000);
} else {
// e.sender.hideColumn(0);
//e.sender.hideColumn(e.sender.columns.length + 1);
exportFlag = false;
}
});
});