您的位置:首页 > 编程语言 > ASP

ASP.NET,Web界面利用Button来实现将telerik:RadGrid表格内数据导出Excel

2015-10-04 09:51 666 查看
一开始在网上找了好久都找不到有关于telerik:RadGrid表格导出Excel的资料来参考,最后还是在telerik的官网上面才看到了几个telerik:RadGrid导出Excel和PFD的方法.但是官网上面都是英文,我也是纠结了半天一段一段的复制粘贴到自己的工具里面试验一下才弄明白.....下面就是操作过程,发上来给大家共享一下:

<asp:Button ID="btn_Export" runat="server" Text="<%$ Resources:LocalizedText, Export  %>"
OnClick="Button1_Click" Height="20px" Width="74px" />


新建一个按钮,按钮触发事件我们就直接用他系统生成的名字Button1_Click就好,在里面要写一个执行方法来触发导出Excel的程序

下面,在telerik:RadGrid里面新加两个属性,一个是OnExcelMLExportRowCreated,另一个是OnExcelMLExportStylesCreated,这两个是最关键的时间触发属性,能不能导出Excel就要看这两个属性里面的方法写的好不好了.....<telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="False" CellSpacing="0"
Culture="zh-CN" GridLines="None"
OnNeedDataSource="RadGrid1_NeedDataSource" AllowPaging="True"
PageSize="20" ShowFooter="True"
OnExcelMLExportRowCreated="RadGrid1_ExcelMLExportRowCreated"
OnExcelMLExportStylesCreated="RadGrid1_ExcelMLExportStylesCreated"></telerik:RadGrid>

我的telerik:RadGrid属性是这样写的,然后在后台代码创建三个方法,一个是Button1_Click按钮触发事件,OnExcelMLExportRowCreated触发时间(命名:RadGrid1_ExcelMLExportRowCreated),以及OnExcelMLExportStylesCreated触发事件(命名:RadGrid1_ExcelMLExportStylesCreated)

下面是各个方法的内容,我也不是弄的很明白,只是纯粹的在官网上面复制粘贴下来自己试验出的一点达到自己需要的效果的代码

Button1_Click:

<strong>protected void Button1_Click(object sender, EventArgs e)
{

RadGrid1.MasterTableView.ExportToExcel();
}</strong>


OnExcelMLExportRowCreated="RadGrid1_ExcelMLExportRowCreated":

OnExcelMLExportStylesCreated="RadGrid1_ExcelMLExportStylesCreated":

bool isConfigured = false;
protected void RadGrid1_ExcelMLExportRowCreated(object source, GridExportExcelMLRowCreatedArgs e)
{
if (!isConfigured)
{
//Set Worksheet name
e.Worksheet.Name = "Order Details Extended";

//Set Column widths
foreach (ColumnElement column in e.Worksheet.Table.Columns)
{
if (e.Worksheet.Table.Columns.IndexOf(column) == 2)
column.Width = Unit.Point(180); //set width 180 to ProductName column
else
column.Width = Unit.Point(80); //set width 80 to the rest of the columns
}

//Set Page options
PageSetupElement pageSetup = e.Worksheet.WorksheetOptions.PageSetup;
pageSetup.PageLayoutElement.IsCenteredVertical = true;
pageSetup.PageLayoutElement.IsCenteredHorizontal = true;
pageSetup.PageMarginsElement.Left = 0.5;
pageSetup.PageMarginsElement.Top = 0.5;
pageSetup.PageMarginsElement.Right = 0.5;
pageSetup.PageMarginsElement.Bottom = 0.5;
pageSetup.PageLayoutElement.PageOrientation = PageOrientationType.Landscape;

//Freeze panes
e.Worksheet.WorksheetOptions.AllowFreezePanes = true;
e.Worksheet.WorksheetOptions.LeftColumnRightPaneNumber = 1;
e.Worksheet.WorksheetOptions.TopRowBottomPaneNumber = 1;
e.Worksheet.WorksheetOptions.SplitHorizontalOffset = 1;
e.Worksheet.WorksheetOptions.SplitVerticalOffest = 1;

e.Worksheet.WorksheetOptions.ActivePane = 2;
isConfigured = true;
}

}
protected void RadGrid1_ExcelMLExportStylesCreated(object source, GridExportExcelMLStyleCreatedArgs e)
{
//Add currency and percent styles
StyleElement priceStyle = new StyleElement("priceItemStyle");
priceStyle.NumberFormat.FormatType = NumberFormatType.Currency;
priceStyle.FontStyle.Color = System.Drawing.Color.Red;
e.Styles.Add(priceStyle);

StyleElement alternatingPriceStyle = new StyleElement("alternatingPriceItemStyle");
alternatingPriceStyle.NumberFormat.FormatType = NumberFormatType.Currency;
alternatingPriceStyle.FontStyle.Color = System.Drawing.Color.Red;
e.Styles.Add(alternatingPriceStyle);

StyleElement percentStyle = new StyleElement("percentItemStyle");
percentStyle.NumberFormat.FormatType = NumberFormatType.Percent;
percentStyle.FontStyle.Italic = true;
e.Styles.Add(percentStyle);

StyleElement alternatingPercentStyle = new StyleElement("alternatingPercentItemStyle");
alternatingPercentStyle.NumberFormat.FormatType = NumberFormatType.Percent;
alternatingPercentStyle.FontStyle.Italic = true;
e.Styles.Add(alternatingPercentStyle);

//Apply background colors
foreach (StyleElement style in e.Styles)
{
if (style.Id == "headerStyle")
{
style.InteriorStyle.Pattern = InteriorPatternType.Solid;
style.InteriorStyle.Color = System.Drawing.Color.Gray;
}
if (style.Id == "alternatingItemStyle" || style.Id == "alternatingPriceItemStyle" || style.Id == "alternatingPercentItemStyle" || style.Id == "alternatingDateItemStyle")
{
style.InteriorStyle.Pattern = InteriorPatternType.Solid;
style.InteriorStyle.Color = System.Drawing.Color.LightGray;
}
if (style.Id.Contains("itemStyle") || style.Id == "priceItemStyle" || style.Id == "percentItemStyle" || style.Id == "dateItemStyle")
{
style.InteriorStyle.Pattern = InteriorPatternType.Solid;
style.InteriorStyle.Color = System.Drawing.Color.White;
}
}
}

下面是导出的Excel效果图,整体看起来还是可以的:




内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息