Excel Export, how to remove filter buttons etc

Issue 1: How to export RadGrid without the filter item (HTML Excel, ExportOnlyData="false")

When IgnorePaging is set to "false" you can use this approach:
protected void Button1_Click(object sender, EventArgs e)
{
    foreach (GridFilteringItem item in RadGrid1.MasterTableView.GetItems(GridItemType.FilteringItem))
        item.Visible = false;
    RadGrid1.MasterTableView.ExportToExcel();
}


This will work with IgnorePaging="true":

bool isExport = false;
protected void Button1_Click(object sender, EventArgs e)
{
    isExport = true;
    RadGrid1.MasterTableView.ExportToExcel();
}
 
protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e)
{
    if (isExport && e.Item is GridFilteringItem)
        e.Item.Visible = false;
}


Issue 2: How to export RadGrid along with the aggregates and the calculated columns (ExcelML)
RadGrid's ExcelML engine fetches the data directly from the data source - all non-data rows/columns like footer aggregates and calculated columns will be ignored.

int currentItem = 0;
    protected void RadGrid1_ExcelMLExportRowCreated(object source, GridExportExcelMLRowCreatedArgs e)
    {
        if (e.Worksheet.Table.Rows.Count == RadGrid1.Items.Count + 1)
        {
            GridFooterItem footerItem = RadGrid1.MasterTableView.GetItems(GridItemType.Footer)[0] as GridFooterItem;
            RowElement row = new RowElement(); //create new row for the footer aggregates

            for (int i = 2; i < footerItem.Cells.Count; i++)
            {
                TableCell fcell = footerItem.Cells[i];
                CellElement cell = new CellElement();
                cell.Data.DataItem = fcell.Text == " " ? "" : fcell.Text;
                row.Cells.Add(cell);
            }
            e.Worksheet.Table.Rows.Add(row);

            //correct the autofilter
            e.Worksheet.AutoFilter.Range = String.Format("R1C1:R1C{0}", e.Worksheet.Table.Columns.Count + 1);
        }

        //create new cell for this row (part of the calculated column)
        CellElement calculatedCell = new CellElement();

        if (e.RowType == GridExportExcelMLRowType.DataRow) //data cell
        {
            calculatedCell.Data.DataItem = double.Parse(RadGrid1.MasterTableView.Items[currentItem]["myCalculatedColumn"].Text);
            currentItem++;
        }
        if (e.RowType == GridExportExcelMLRowType.HeaderRow)
            calculatedCell.Data.DataItem = RadGrid1.MasterTableView.GetColumn("myCalculatedColumn").HeaderText; //header cell
        e.Row.Cells.Add(calculatedCell);        
    }


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值