Kendo Grid MVC - 服务器导出 Excel 日期时间字段(自定义格式)
Posted
技术标签:
【中文标题】Kendo Grid MVC - 服务器导出 Excel 日期时间字段(自定义格式)【英文标题】:Kendo Grid MVC - Server Export Excel datetime field (custom format) 【发布时间】:2021-02-03 03:08:03 【问题描述】:日期时间字段被导出为数字,但如果我在 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 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>
【问题讨论】:
查看文档中的 excel 格式部分。 docs.telerik.com/kendo-ui/knowledge-base/cell-formatToXlsxStream
和 ToCsvStream
定义在哪里?
【参考方案1】:
我通过添加解决了它:
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"
;
【讨论】:
以上是关于Kendo Grid MVC - 服务器导出 Excel 日期时间字段(自定义格式)的主要内容,如果未能解决你的问题,请参考以下文章
Kendo Grid MVC 结合了 ajax 绑定和服务器编辑
Kendo UI 将 DropDownList 添加到 Grid (MVC)