RadControls - Grid Export Excel 功能,如何预定义,预格式化Excel 的样式?

 转自:http://www.telerik.com/help/aspnet-ajax/grid-html-export.html

转自:http://www.telerik.com/community/forums/aspnet-ajax/grid/can-i-format-number-when-export-to-excelml-in-radgrid.aspx

 

Collapse imageUsing ItemCreated/ItemDataBound

These events are useable in different scenarios, for example:

  • to apply styles to items other than GridDataItem
  • to format Word document, since the ExcelExportCellFormatting event is Excel-specific

They are not as convenient as the ExcelExportCellFormatting event because the developer should use flag to distinguish whether the current item/cell is being exported or displayed.

Please keep in mind that if you don't use IgnorePaging="true"RadGrid will be exported directly and the ItemCreated/ItemDataBound events won't be fired.

 

Collapse imageNumber formats / Formulas

Microsoft Office HTML format allows you to define custom number formats by using the mso-number-format style attribute. Several examples are shown below:

CopyC#
protected void RadGrid1_ExcelExportCellFormatting(object source, ExcelExportCellFormattingEventArgs e){    switch (e.FormattedColumn.UniqueName)    {        case "C1":            e.Cell.Style["mso-number-format"] = @"\@";            break;        case "C2":            e.Cell.Style["mso-number-format"] = @"$0.00";            break;        case "C3":            e.Cell.Style["mso-number-format"] = @"0000";            break;        case "C4":            e.Cell.Style["mso-number-format"] = @"mm\/dd\/yyyy";            break;    }}
CopyVB.NET
Protected Sub RadGrid1_ExcelExportCellFormatting(ByVal source As Object, ByVal e As ExcelExportCellFormattingEventArgs) Handles RadGrid1.ExcelExportCellFormatting    Select Case e.FormattedColumn.UniqueName        Case "C1"            e.Cell.Style("mso-number-format") = "\@"            Exit Select        Case "C2"            e.Cell.Style("mso-number-format") = "$0.00"            Exit Select        Case "C3"            e.Cell.Style("mso-number-format") = "0000"            Exit Select        Case "C4"            e.Cell.Style("mso-number-format") = "mm\/dd\/yyyy"            Exit Select    End SelectEnd Sub

You can see some of the most common custom number formats and their description in the following table:

FormatDescription
\@text
"0\.000"3 decimals
\#\,\#\#0\.000comma separators (and 3 decimals)
"mm\/dd\/yy"Date format
"d\\-mmm\\-yyyy"another date format
Percentpercent

Another interesting feature, supported by the Excel export are the formulas. You can assign the desired formula to a specific cell by applying the formula attribute.

Note

The formula attribute is valid in the context of the Office HTML only. You shouldn't apply this attribute unconditionally in order to prevent validation problems.

 
 

 

另一种方式:

 

You should assign a style on ExcelMLExportRowCreated and then set the appropriate number format for this style on ExcelMLExportStylesCreated:

protected void RadGrid1_ExcelMLExportRowCreated(object source, GridExportExcelMLRowCreatedArgs e)
{
   e.Row.Cells.GetCellByName("Name").StyleValue = "myCustomStyle";
}
protected void RadGrid1_ExcelMLExportStylesCreated(object source, GridExportExcelMLStyleCreatedArgs e)
{
   StyleElement myStyle = new StyleElement("myCustomStyle");
   myStyle.NumberFormat.FormatType = NumberFormatType.Currency;
   myStyle.FontStyle.Bold = true;
   e.Styles.Add(myStyle);
}


You can also try one of the following formats:
General, General Number, General Date, Long Date, Medium Date, Short Date, Long Time, Medium Time, Short Time, Currency, Euro Currency, Fixed, Standard, Percent, Scientific
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值