转自:http://www.telerik.com/help/aspnet-ajax/grid-html-export.html
Using 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.
Number 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:
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; }}
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:
Format | Description |
---|---|
\@ | text |
"0\.000" | 3 decimals |
\#\,\#\#0\.000 | comma separators (and 3 decimals) |
"mm\/dd\/yy" | Date format |
"d\\-mmm\\-yyyy" | another date format |
Percent | percent |
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