以下是我的LINQ
IQueryable<ReportMapper> query;
query = (from c in entities.tDocumentStatus
join d in entities.tTOCStructures on c.DocumentId equals d.DocumentID
join e in entities.tUsers on d.LastUpdatedBy equals e.UserUID
orderby d.CreatedOn descending
where
(docMode <= 0 || docModes.Contains(c.StatusId)) &&
d.FolderType == 2 && d.isDeleted == false && d.ClientID == clientId &&
(EntityFunctions.TruncateTime(d.CreatedOn) >= startDate.Date && EntityFunctions.TruncateTime(d.CreatedOn) <= endDate.Date)
select new ReportMapper()
{
DocumentName = d.DocumentName,
AssignedDate = c.AssignedDate==null? (EntityFunctions.TruncateTime(d.LastUpdatedOn)): (EntityFunctions.TruncateTime(c.AssignedDate)),
ReviewStatus = c.tStatu.StatusName,
ActionPerformedBy = e.FirstName + " " + e.LastName
});
我需要将此数据导出为excel。我需要从指定日期删除完整的时间部分。
当我使用EntityFunctions.TruncateTime
时,它会将时间截断为00:00:00
但我需要删除此部分。
我尝试了以下方法: -
(EntityFunctions.TruncateTime(d.LastUpdatedOn)).Value.Date
(EntityFunctions.TruncateTime(d.LastUpdatedOn).GetValueOrDefault().Date)
(EntityFunctions.TruncateTime(d.LastUpdatedOn)).ToString().Date
导出到Excel
public void ExportToExcel()
{
ExcelPackage excel = new ExcelPackage();
var worksheet = excel.Workbook.Worksheets.Add("Sheet1");
fileName = "Document_Status_Report_" + DateTime.Now.ToString("yyyyMMdHHmmss") + ".xlsx";
worksheet.Cells[1, 1].LoadFromCollection(data, true);
using (MemoryStream swObj = new MemoryStream())
{
response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
response.AddHeader("Content-Disposition", "attachment; filename=" + fileName + "");
excel.SaveAs(swObj);
swObj.WriteTo(response.OutputStream);
return;
}
}
答案 0 :(得分:1)
使用.Value.Date
已经尝试过了。 从不字符串。
您的问题不是时间部分(@PanagiotisKanavos解释),但格式应用于Excel中的单元格。只需调整此项即可仅显示日期部分。
答案 1 :(得分:0)
DateTime
总是有时间,你不能截断或删除Datetime
的时间。如果你只想要日期,那么建议在ReportMapper
类中添加新的字符串属性并仅指定日期string。如下面的代码所示,我已将“MM/dd/yyyy
”格式的日期指定给AssignedDateString
。
IQueryable<ReportMapper> query;
query = (from c in entities.tDocumentStatus
join d in entities.tTOCStructures on c.DocumentId equals d.DocumentID
join e in entities.tUsers on d.LastUpdatedBy equals e.UserUID
orderby d.CreatedOn descending
where
(docMode <= 0 || docModes.Contains(c.StatusId)) &&
d.FolderType == 2 && d.isDeleted == false && d.ClientID == clientId &&
(EntityFunctions.TruncateTime(d.CreatedOn) >= startDate.Date && EntityFunctions.TruncateTime(d.CreatedOn) <= endDate.Date)
select new ReportMapper()
{
DocumentName = d.DocumentName,
AssignedDate = c.AssignedDate==null? (EntityFunctions.TruncateTime(d.LastUpdatedOn)): (EntityFunctions.TruncateTime(c.AssignedDate)),
ReviewStatus = c.tStatu.StatusName,
ActionPerformedBy = e.FirstName + " " + e.LastName
AssignedDateString= c.AssignedDate==null? (d.LastUpdatedOn.ToString("MM/dd/yyyy")): (c.AssignedDate.ToString("MM/dd/yyyy")),
});