AUTHOR: Marin Bratanov
DATE POSTED: April 17, 2018
Set cell format for Excel export.
This can be necessary when you have large numbers (e.g., serial numbers) that Excel can show in scientific (exponential) notation. While the end user can select the Format Cells option and choose the cell format, you want them not to have to do that.
To do this, you need to use the InfrastructureExporting event of the grid and the underlying ExportInfrastructure, then find the column that you wish to change (for example, via its header text), then loop through its cells and set their Excel format via their Format property. Here is an example:
protected
void
RadGrid1_InfrastructureExporting(
object
sender, GridInfrastructureExportingEventArgs e)
{
int
colIndex = -1;
//you can make this a list and store more indexes so you can loop through them to format more columns
//get the first row where the headers are so you can find the desired column
Telerik.Web.UI.ExportInfrastructure.CellCollection cells = e.ExportStructure.Tables[0].Rows[1].Cells;
foreach
(Telerik.Web.UI.ExportInfrastructure.Cell item
in
cells)
if
(item.Text ==
"large numbers"
)
colIndex = item.ColIndex;
}
(colIndex < 0)
return
;
//no column found, avoid error
Telerik.Web.UI.ExportInfrastructure.Column col = e.ExportStructure.Tables[0].Columns[colIndex];
//loop through the cells to set format
(Telerik.Web.UI.ExportInfrastructure.Cell cell
col.Cells)
cell.Format =
"##############"
//or use another Excel format, this is just a series of digits for number representation
col.Width = 200;
//enlarge the column to accommodate long numbers
RadGrid1_NeedDataSource(
sender, GridNeedDataSourceEventArgs e)
RadGrid1.DataSource = GetDummyData();
DataTable GetDummyData()
DataTable dt =
new
DataTable();
dt.Columns.Add(
"numbers"
,
typeof
(
decimal
));
"someField"
"someDate"
(DateTime));
dt.Rows.Add(1, 2,
DateTime(2011, 06, 12));
dt.Rows.Add(200011150032, 3,
DateTime(2011, 12, 12));
dt.Rows.Add(3, 6,
DateTime(2012, 06, 17));
dt.Rows.Add(4, 4,
DateTime(2012, 09, 18));
dt.Rows.Add(5, 7,
DateTime(2013, 03, 18));
dt;
<
telerik:RadGrid
runat
=
"server"
ID
"RadGrid1"
OnInfrastructureExporting
"RadGrid1_InfrastructureExporting"
OnNeedDataSource
"RadGrid1_NeedDataSource"
AutoGenerateColumns
"false"
>
ExportSettings
Excel-Format
"Xlsx"
IgnorePaging
"true"
ExportOnlyData
OpenInNewWindow
</
MasterTableView
CommandItemDisplay
"Top"
CommandItemSettings
ShowExportToExcelButton
/>
Columns
telerik:GridBoundColumn
DataField
UniqueName
HeaderText
DataType
"System.Decimal"
></
"someColumn"
"another column"
telerik:GridDateTimeColumn
"dateColumn"
"the date"
Resources Buy Try