我需要在Excel中导出RadGridView
的Excel导出格式为“Number”,格式样式为“{0:#,## 0.00}”。
我使用.Export实现了这个,处理ElementExporting
事件:
grid.ElementExporting += Grid_ElementExporting;
grid.Export(stream, new GridViewExportOptions()
{
Format = ExportFormat.ExcelML,
ShowColumnHeaders = true,
ShowColumnFooters = true
});
private void Grid_ElementExporting(object sender, GridViewElementExportingEventArgs e)
{
if (e.Element == ExportElement.Cell)
{
var column = e.Context as GridViewDataColumn;
if (column?.DataType?.Name == "Decimal")
{
e.Value = string.Format(@"{0:#,##0.00}", e.Value);
}
}
}
但是我在Excel中打开时收到错误“X的文件格式和扩展名不匹配。”,尽管它肯定是.xls扩展名。我可以点击它,它正确加载。
更多地阅读它,听起来我应该更新以使用.ExportToXlsx,而获取.xlsx中的文件无论如何都将是一个特权。
我将.Export
更改为.ExportToXlxs
,将ElementExporting
更改为ElementExportingToDocument
,格式化工作正常,但所有单元格都恢复为Excel中的“常规”格式,而我需要它们作为“数字”。
有关于应用视觉样式的文档:https://docs.telerik.com/devtools/wpf/controls/radgridview/export/how-to/style-exported-documents
但是不要改变我能找到的基础格式。
有什么建议?
您应该使用CellValueFormat
中解释的the official docs。
CellValueFormat
类在Telerik.Windows.Documents.Spreadsheet.dll
中定义,因此您需要引用此程序集