我们的 Web 应用程序中有一些网格,我们将 Kendo 用于 ASP.Net MVC。 一些客户反射(reflect)在将网格数据导出到 Excel 时日期不同。
例如,第一行来自 Controller { "SaleDate": "2018-05-30T00:00:00", "SaleDateAndTime": "2018-05-30T08:01:40.673"}
。导出到excel后对应的单元格值分别为:05/29/2018 23:59:32
和05/30/2018 08:01:12
。
- 我尝试导出具有不同时间值的日期,导出到 excel 后的差异始终为 28 秒。
- 这只发生在某些客户端上(时区与服务器相同并且设置正确)。
- 只有在使用 Chrome 浏览器时才会发生这种情况。
有人遇到过这个问题吗?我开了一张去 Telerik 的票,但他们帮不了我。
最佳答案
对于任何浏览器,我也遇到了同样的问题。就我而言,差异是 6 小时。我没有找到合适的解决方案。我通过在导出到 excel 时将 DateTime 转换为 ISO 日期字符串来解决这个问题。
我希望这能解决您的问题。
@(Html.Kendo().Grid<DataModel>()
.Name("grid")
.ToolBar(tools => tools.Excel())
.Events(e => e.ExcelExport("excelExport"))
/* Other configuration. */
)
function excelExport(e) {
var rows = e.workbook.sheets[0].rows;
for (var ri = 0; ri < rows.length; ri++) {
var row = rows[ri];
for (var ci = 0; ci < row.cells.length; ci++) {
var cell = row.cells[ci];
if (row.type === "data" && isValidDate(cell.value)) {
var dateTime = new Date(Date.parse(cell.value))
if (dateTime != "Invalid Date") {
cell.value = ExcelISODateString(dateTime)
cell.format = "MM-dd-yyyy hh:mm:ss"
}
}
}
}
}
function ExcelISODateString(datetime) {
ISODate= "";
if (datetime != null || datetime != undefined) {
ISODate= new Date(datetime.getTime() + (datetime.getTimezoneOffset() * 60000));
}
return ISODate;
}
//To check whether the cell value is datetime or not
function isValidDate(d) {
return d instanceof Date && !isNaN(d);
}
关于excel - 从 Kendo Grid 导出到 Excel 后 DateTime 列相差 28 秒,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50825864/