Search code examples
c#asp.net-mvckendo-uitelerik

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


The datetime field is being exported as an number, but if i change the cell to datetime type in excel than it gets the correct values. All of the server export is functional except the date format. Since i am doing server export i can't rely on cliente side solutions like https://docs.telerik.com/kendo-ui/knowledge-base/cell-format

Controller:

        [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);
        }

Export 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>

Solution

  • I solved it by adding:

    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"
                    };