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

Custom column headers and export to excel

1 Answer 174 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Kumar
Top achievements
Rank 1
Kumar asked on 07 Jun 2010, 06:57 PM
I have few question regarding export and custom column headers and follow up here from the following url

my code is somewhat like the following
if (dialog.ShowDialog() == true)  
            {  
                if (dialog.FilterIndex == 1)  
                {  
                    format = ExportFormat.ExcelML;  
                    extension = "xml";  
                }  
                else 
                {  
                    format = ExportFormat.Html;  
                    extension = "xls";  
                }  
                using (Stream stream = dialog.OpenFile())  
                {  
                    myGrid.Export(stream,  
                        new GridViewExportOptions()  
                        {  
                            Format = format,  
                            ShowColumnHeaders = true,  
                            ShowColumnFooters = false,  
                            ShowGroupFooters = false 
                        });  
                }  
            } 
and also I have exporting method like the following

void myGrid_Exporting(object sender, GridViewExportEventArgs e)  
        {  
            if (e.Value is Telerik.Windows.Controls.GridView.AlignmentContentPresenter)  
            {  
                Telerik.Windows.Controls.GridView.AlignmentContentPresenter x = e.Value as Telerik.Windows.Controls.GridView.AlignmentContentPresenter;  
                e.Value = x.Content.ToString();  
            }  
            else if (e.Element == ExportElement.Cell)  
            {  
                if (e.Value != null)  
                {  
                    if (e.Value.ToString().ToUpper().Equals("TRUE"))  
                        e.Value = "X";  
                    else if (e.Value.ToString().ToUpper().Equals("FALSE"))  
                        e.Value = "";  
                }  
            }              
        } 


Now questions;
1. Even though I have access to the stream, I want to write the custom headers after the standard excel header and before the grid data. I am writing the writing data using _exporting method. What is the best place to write the custom column header without writing full XML generation ?
2. As you can see from the code, I am writing both the XML and XLS format, do I have do anything different for each formats?
3. Current export, export all the visible part of the grid with proper column width in excel, but all the columns that are not visible was set to <Column ss:Width="20"> except the last one. I changed the code in _Exporting to the following still did not work
 else if (e.Element == ExportElement.HeaderCell)  
            {  
                e.Width = 100;  
            } 

1 Answer, 1 is accepted

Sort by
0
Veselin Vasilev
Telerik team
answered on 10 Jun 2010, 01:32 PM
Hi Kumar,

Straight to your questions:

1. You can write to the stream in the new ElementExported event (which is introduced in Q1 2010 SP2) release. As far as I understood you want to write something before the grid's header, right? If so, here is the idea - cancel the ElementExporting event for the HeaderCells so nothing is exported and then in the ElementExported check whether the current element is HeaderRow and if so - write your custom content in the stream and then manually add the headers of the gridView. Find attached a sample project.

2. ExcelML is a special MS format and is different from the HTML format which is pure html. The aforementioned approach would not work in ExcelML scenario.

3. Can you check the source of the exported data? You can open the .xls file with notepad and examine the HTML code. Search for style='width:100px" or something like this. If it is there, then it is Excel's fault for not applying it.

All the best,
Veskoni
the Telerik team

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 Public Issue Tracking system and vote to affect the priority of the items.
Tags
GridView
Asked by
Kumar
Top achievements
Rank 1
Answers by
Veselin Vasilev
Telerik team
Share this question
or