Kendo 网格 MVC - 服务器导出 Excel 日期时间字段(自定义格式)

Kendo Grid MVC - Server Export Excel datetime field (custom format)

日期时间字段正在导出为数字,但如果我在 excel 中将单元格更改为日期时间类型,它会得到正确的值。 除日期格式外,所有服务器导出功能均正常。 因为我在做服务器导出,所以我不能依赖像 https://docs.telerik.com/kendo-ui/knowledge-base/cell-format

这样的客户端解决方案

控制器:

        [HttpPost]
        public FileStreamResult ExportServer([DataSourceRequest]DataSourceRequest request, string model, string data)
        {
            var columnsData = JsonConvert.DeserializeObject<IList<ExportColumnSettings>>(HttpUtility.UrlDecode(model));
            dynamic options = JsonConvert.DeserializeObject(HttpUtility.UrlDecode(data));
            SpreadDocumentFormat exportFormat = options.format.ToString() == "csv" ? exportFormat = SpreadDocumentFormat.Csv : exportFormat = SpreadDocumentFormat.Xlsx;
            Action<ExportCellStyle> cellStyle = new Action<ExportCellStyle>(ChangeCellStyle);
            Action<ExportRowStyle> rowStyle = new Action<ExportRowStyle>(ChangeRowStyle);
            Action<ExportColumnStyle> columnStyle = new Action<ExportColumnStyle>(ChangeColumnStyle);
            

            string fileName = string.Format("{0}.{1}", options.title, options.format);
            string mimeType = Helpers.GetMimeType(exportFormat);

            

            Stream exportStream = exportFormat == SpreadDocumentFormat.Xlsx ?
                db.VWMapaCompleto.ToList().ToDataSourceResult(request).Data.ToXlsxStream(columnsData, (string)options.title.ToString(), cellStyleAction: cellStyle, rowStyleAction: rowStyle, columnStyleAction: columnStyle) :
                db.VWMapaCompleto.ToList().ToDataSourceResult(request).Data.ToCsvStream(columnsData);

            var fileStreamResult = new FileStreamResult(exportStream, mimeType);
            fileStreamResult.FileDownloadName = fileName;
            fileStreamResult.FileStream.Seek(0, SeekOrigin.Begin);

            return fileStreamResult;
        }

        private void ChangeCellStyle(ExportCellStyle e)
        {
            bool isHeader = e.Row == 0;
            SpreadCellFormat format = new SpreadCellFormat
            {
                ForeColor = isHeader ? SpreadThemableColor.FromRgb(216, 184, 168) : SpreadThemableColor.FromRgb(0,0,0),
                //IsItalic = true,
                //VerticalAlignment = SpreadVerticalAlignment.Center,
                WrapText = true,
                Fill = SpreadPatternFill.CreateSolidFill(isHeader ? new SpreadColor(50, 54, 58) : new SpreadColor(255,255,255))                
            };
            e.Cell.SetFormat(format);            
        }

        private void ChangeRowStyle(ExportRowStyle e)
        {
            e.Row.SetHeightInPixels(e.Index == 0 ? 30 : 30);
        }

        private void ChangeColumnStyle(ExportColumnStyle e)
        {
            double width = e.Name == "Product name" || e.Name == "Category Name" ? 250 : 100;
            e.Column.SetWidthInPixels(width+50);
        }

导出Form/Button:

        <form action="@Url.Action("ExportServer", "Mapa")" method="POST">
            <input type="hidden" id="export-data" name="data" />
            <input type="hidden" id="export-model" name="model" />
            
            <input type="hidden" id="export-filter" name="filter" value="NumeroPedido~eq~'0001'" />

            <input type="submit" class="k-button download" data-format="xlsx" data-title="Exporta Lista" value="Export XLSX Completo" />
            
        </form>

我通过添加解决了它:

NumberFormat = "yyyy/MM/dd h:mm"

format = new SpreadCellFormat
                {
                    ForeColor = isHeader ? SpreadThemableColor.FromRgb(216, 184, 168) : SpreadThemableColor.FromRgb(0, 0, 0),
                    //IsItalic = true,
                    //VerticalAlignment = SpreadVerticalAlignment.Center,
                    WrapText = true,
                    Fill = SpreadPatternFill.CreateSolidFill(isHeader ? new SpreadColor(50, 54, 58) : new SpreadColor(255, 255, 255)),
                    NumberFormat = "yyyy/MM/dd h:mm"
                };

在我的例子中,问题是 cshtml 中日期列的格式。 格式为“{0:d}”,excel 无法解释此格式,因此将日期转换为数字。 将日期格式更改为 {0:MM/dd/yyyy} 对我有用。