This is a migrated thread and some comments may be shown as answers.

Export to Excel - hide header row?

5 Answers 416 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Alex Imas
Top achievements
Rank 1
Alex Imas asked on 10 Oct 2008, 06:47 PM
On an export to Excel, I can't seem to find a way to suppress the header row.  I'm creating the grid programmatically.  Here's the relevant code:

RadGrid

exportGrid = new RadGrid();

 

exportGrid.NeedDataSource +=

new GridNeedDataSourceEventHandler( exportGrid_NeedDataSource );

 

exportGrid.Skin =

"Default";
exportGrid.Width =
Unit.Percentage( 100 );
exportGrid.ShowHeader =
false;
exportGrid.AutoGenerateColumns =
true;
exportGrid.ExportSettings.ExportOnlyData =
true;
exportGrid.ExportSettings.Excel.Format =
GridExcelExportFormat.ExcelML;
exportGrid.DataMember =
"ExportData";
PlaceHolder phExport = new PlaceHolder();
currentPage.Controls.Add( phExport );
exportGrid.DataSource = _exportData;
phExport.Controls.Add( exportGrid );
exportGrid.Rebind();

 

 

foreach ( GridHeaderItem headerItem in exportGrid.MasterTableView.GetItems( GridItemType.Header ) )
headerItem.Visible =
false;

 

exportGrid.MasterTableView.ExportToExcel();

I also tried a couple of different variations on this approach, without any luck:

 

protected

void exportGrid_ExcelMLExportRowCreated( object source, Telerik.Web.UI.GridExcelBuilder.GridExportExcelMLRowCreatedArgs e ){
if ( e.RowType == Telerik.Web.UI.GridExcelBuilder.GridExportExcelMLRowType.HeaderRow ){
foreach ( Telerik.Web.UI.GridExcelBuilder.CellElement cell in e.Row.Cells ){
if( cell.Data != null )
cell.Data.DataItem =
null;
}
}
}

Any help is appreciated.

 

5 Answers, 1 is accepted

Sort by
0
Daniel
Telerik team
answered on 14 Oct 2008, 08:26 AM
Hello Alex,

Please try the approach shown below:
protected void RadGrid1_ExcelMLExportRowCreated(object source, Telerik.Web.UI.GridExcelBuilder.GridExportExcelMLRowCreatedArgs e) 
    if (e.RowType == GridExportExcelMLRowType.HeaderRow) 
    { 
        e.Worksheet.Table.Rows.Remove(e.Row); 
        e.Worksheet.AutoFilter.Range = ""
    } 

Regards,
Daniel
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Alex Imas
Top achievements
Rank 1
answered on 14 Oct 2008, 05:23 PM
Perfect, this worked beautifully - exactly what I needed.

Thanks very much.
0
Jorge
Top achievements
Rank 1
answered on 24 Jul 2015, 11:50 PM

Hi.

some suggestion for the case: Telerik.Web.UI.GridExcelExportFormat.Html ?

Regards.

0
Konstantin Dikov
Telerik team
answered on 29 Jul 2015, 09:03 AM
Hi Jorge,

Detailed information about the HTML format and how to manipulate the structure is available in the following help article:
Additionally, for your convenience, please take a look at the following example:
<telerik:RadGrid runat="server" ID="RadGrid1" OnNeedDataSource="RadGrid1_NeedDataSource" OnItemCommand="RadGrid1_ItemCommand">
    <ExportSettings HideStructureColumns="true">
        <Excel Format="Html" />
    </ExportSettings>
    <MasterTableView CommandItemDisplay="Top" CommandItemSettings-ShowExportToExcelButton="true">              
    </MasterTableView>
</telerik:RadGrid>

And the code-behind:
protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
{
    DataTable table = new DataTable();
    table.Columns.Add("ID", typeof(int));
    table.Columns.Add("FirstName", typeof(string));
    table.Columns.Add("LastName", typeof(string));
    table.Columns.Add("Age", typeof(int));
    table.Columns.Add("Date", typeof(DateTime));
    table.Columns.Add("BoolValue", typeof(Boolean));
    for (int i = 0; i < 5; i++)
    {
        table.Rows.Add(i, "FirstName" + i, "LastName" + i, 20 + i, DateTime.Now.AddDays(i), i % 2 == 0);
    }
 
    (sender as RadGrid).DataSource = table;
}
 
protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
{
    if (e.CommandName == RadGrid.ExportToExcelCommandName)
    {
        (RadGrid1.MasterTableView.GetItems(GridItemType.Header)[0] as GridHeaderItem).Visible = false;
        (RadGrid1.MasterTableView.GetItems(GridItemType.CommandItem)[0] as GridCommandItem).Visible = false;
    }
}

Hope this helps.


Regards,
Konstantin Dikov
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
0
Nasir
Top achievements
Rank 1
answered on 21 Mar 2017, 05:35 AM

Hi Konstantin Dikov
First of all thanks for the help though :)  .. I am able to hide the detail grid headers name from the grid. but when I tried to export them to excel it still appears there. Is there any way to hide the detail grid column name from the excel sheet as well ?

BTW I am using Kendo Grid in Asp.Net MVC 5 project
Thanks

Tags
Grid
Asked by
Alex Imas
Top achievements
Rank 1
Answers by
Daniel
Telerik team
Alex Imas
Top achievements
Rank 1
Jorge
Top achievements
Rank 1
Konstantin Dikov
Telerik team
Nasir
Top achievements
Rank 1
Share this question
or