【发布时间】:2016-12-07 08:28:43
【问题描述】:
下面是我的 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;
}
}
【问题讨论】:
-
属性是
DateTime,它总是有一个“时间”组件。如果您只想要日期部分,请将其格式化为字符串 -
您可以使用
DateTime.Date提取日期时间的日期部分。时间部分可通过DateTime.TimeSpan获得。 -
如果您在使用 Excel 时遇到问题,请发布 相关 代码。 Excel 的时间部分没有问题 - 它根本没有仅日期的日期!。 Excel 中的日期存储为从 1899-12-30 开始的十进制偏移量。您可以使用
DateTime.ToOADate获得该值。 很多虽然使用像 Epplus 这样的库并让它进行任何转换 -
@PanagiotisKanavos,添加了 ExportToExccel()
-
@Kgn-web 那是 Epplus。那有什么问题呢?如果您使用
.Date,则单元格将在时间部分包含00:00:00,并且存储的日期值将根本没有秒数(即没有小数部分)。EntityFunctions.TruncateTime也是如此。您是否对列使用了仅日期样式?
标签: c# sql-server asp.net-mvc linq datetime