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:
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);
}