
26 Answers, 1 is accepted
You can do this very easily. However, there is one caveat - you need to define a RadGridView in a DataTemplate and put it in your XAML resources, because we'll use this RadGridView to export the children. You just need to handle the ElementExported event:
private
void
Button1_Click(
object
sender, RoutedEventArgs e)
{
var saveDialog =
new
SaveFileDialog();
if
(saveDialog.ShowDialog() ==
true
)
{
var stream = saveDialog.OpenFile();
var streamWriter =
new
System.IO.StreamWriter(stream);
streamWriter.Write(
this
.clubsGrid.ToCsv());
streamWriter.Close();
MessageBox.Show(
"Exported."
);
}
}
private
void
clubsGrid_ElementExported(
object
sender, GridViewElementExportedEventArgs e)
{
if
(e.Element == ExportElement.Row)
{
var template =
this
.LayoutRoot.Resources[
"HierarchyChildTemplate"
]
as
DataTemplate;
var grid = template.LoadContent()
as
RadGridView;
grid.DataContext = e.Context;
var subExport = grid.ToCsv();
e.Writer.Write(subExport);
}
}
Yavor Georgiev
the Telerik team

It works fine, but only for xml, csv and html.
Although the norwegian characters are messed up.
Excel can not open the xls-file created with ..ToExcelML.
Shouldn't we use the Export-way?
RadGridView1.Export(stream, new GridViewExportOptions())...?
ToCsv, ToHtml and ToExcelMl are just convenience methods that call Export internally. If you're having problems with characters, be sure to set the Culture property of GridViewExportOptions. Also, ExcelML is an XML format, so the correct file extension is XML, not XLS. That is why Excel complains.
All the best,Yavor Georgiev
the Telerik team

var stream = saveDialog.OpenFile();
...
using (stream)
{
System.Globalization.CultureInfo cultureInfo = new System.Globalization.CultureInfo("nb-NO");
myMasterGrid.Export(stream,
new GridViewExportOptions()
{
Format = ExportFormat.ExcelML,
ShowColumnHeaders = true,
ShowColumnFooters = true,
ShowGroupFooters = true,
Culture = cultureInfo
});
}
... and saved it as test.xml.
It still won't open in Excel.
How could we tell the "childgrid" to use the same options?
private
void
clubsGrid_ElementExported(
object
sender, GridViewElementExportedEventArgs e)
{
if
(e.Element == ExportElement.Row)
{
var template =
this
.LayoutRoot.Resources[
"HierarchyChildTemplate"
]
as
DataTemplate;
var grid = template.LoadContent()
as
RadGridView;
grid.DataContext = e.Context;
var subExport = grid.ToExcelML();
e.Writer.Write(subExport);
}
}
You need to replace the 'grid.ToExcelML' line in your ElementExported handler with the Export method, passing the appropriate GridViewExportOptions. I'm afraid the ExcelML format does not support hierarchical tables. You should try with HTML.
Sincerely yours,Yavor Georgiev
the Telerik team

private
void
clubsGrid_ElementExported(
object
sender, GridViewElementExportedEventArgs e)
{
if
(e.Element == ExportElement.Row)
{
var template =
this
.LayoutRoot.Resources[
"HierarchyChildTemplate"
]
as
DataTemplate;
var grid = template.LoadContent()
as
RadGridView;
grid.DataContext = e.Context;
var subExport = ????
myMasterGrid.Export(stream?????, new GridViewExportOptions()...
e.Writer.Write(subExport);
}
}
var stream =
new
MemoryStream();
grid.Export(stream, options);
var subExport =
new
StreamReader(stream).ReadToEnd();
Greetings,
Yavor Georgiev
the Telerik team

System.Globalization.CultureInfo cultureInfo = new System.Globalization.CultureInfo("nb-NO");
The grid has items, the options is ok, the stream is not null, but the subExport is empty / null / nix...
Is it possible to look at an example where a hierarchical grid is exported?
Before the 'var subExport = ...' line insert this one:
stream.Seek(0, SeekOrigin.Begin);
Yavor Georgiev
the Telerik team

Using ExportFormat.Text all looks well. But a txt-file is not so very usefull.
Using ExportFormat.Csv or ExportFormat.Html works OK, but here the norwegian characters are changed to weird chars...
Using ExportFormat.ExcelML (and filename.xml) can't get open in Excel...
Exporting to Excel is a very important feature for our customers.
I guess I have to use automation to Excel to get this right...
Can you post more info about your Excel version? ExcelML can be opened with Office 2003 and above.
Regards,Vlad
the Telerik team

XML ANALYSEFEIL: Misformet eller ulovlig behandlingsinstruksjon
Translated something like this:
Analyze-error: Malformed or illegal processing instruction
Error occured in or under this element stack...
The excel-version:
Excel 2007, (12.0.6535.5002) SP2 MSO (12.0.6535.5002)
Part of Microsoft Office Professional Plus 2007.
Norwegian
I forgot to add in my previous reply that hierarchy in ExcelML is not supported. Sorry for this miscommunication!
Regards,Vlad
the Telerik team

<<Exporting to Excel is a very important feature for our customers.
I guess I have to use automation to Excel to get this right...>>
Do you know if - and when - exporting of hierarchy to Excel will be supported?
We have plans to add this however I'm not sure if we will be able to include it before the end of the year.
Greetings,Vlad
the Telerik team

I hope anwers, please.
The closest option we can offer you is demonstrated here.
Best wishes,Vlad
the Telerik team

in this method, I need exclude rows that not belong to filtering. Actually, the parent row is ok. but hierarchy row is obtaining all children rows.
private void RadGridView1_ElementExported(object sender, GridViewElementExportedEventArgs e)
{
if (e.Element == ExportElement.Row)
{
string codeHTML;
var template = localContainer.Resources["childrenGridView"] as DataTemplate;
var grid = template.LoadContent() as RadGridView;
grid.ItemsSource = localDataSet.Tables[1].Select("ParentIdentificador=" + ((DataRowView)e.Context).Row["Identificador"]);
grid.ShowGroupPanel = false;
grid.IsFilteringAllowed = true;
var subExport = grid.ToHtml();
codeHTML = "<tr><td> </td><td colspan ='10'>" + subExport + "</td></tr>";
e.Writer.Write(codeHTML);
}
}
the line
grid.ItemsSource = localDataSet.Tables[1].Select("ParentIdentificador=" + ((DataRowView)e.Context).Row["Identificador"]);
I get all children data of row parent. But I need exported only rows that belong to the filter of child row.
help me, please.

my boss is a demanding person

The export data from a gridradview appear the follow at last row of excel.
[QCVG: Key=SARTA DE PRUEBA; ItemCount=1; HasSubgroups=False; ParentGroup=[QCVG: Key=Root; ItemCount=1; HasSubgroups=True; ParentGroup=null];];
I used the Q2 2010 2 0294. and exist only a group int the panel group.

I hope any response. The product Telerick is a good product, but I have delivering development the product to the client. you konw this concept.

[QCVG: Key=Midwest; ItemCount=6; HasSubgroups=False; ParentGroup=[QCVG: Key=Root; ItemCount=300; HasSubgroups=True; ParentGroup=null];];
shows up in the Group Footer in the Excel Export
Any resolution on this?
By default the grid will not export group footer cells. You need to set the value using ElementExporting similar to our demo. The WPF version of the code is exactly the same. This feature is supported currently only when exporting with HTML format.
Best wishes,Vlad
the Telerik team

I've just spent time evaluating the WPF controls and for the most part find them quite impressive and simple to use. However, the lack of decent grid exporting is a deal breaker and unfortunately we will not be using your controls because of it.

I would like to export a hierarchical grid to Excel. My user has defined custom styling on specific columns, headers and footers. Conditional cell styling has also been applied using GridViewColumn.CellStyleSelector. I would like these stylings to be exported to Excel too.
Can you, please, supply a sample project that demonstrates how to do this?
Kind Regards,
Albert
You can check the "Exporting RowDetails" WPF Demos on how to export hierarchical GridView. In order to export Styles, you should invoke the Export method with ExportFormat.HTML. Then you can set the e.Styles dictionary with proper CSS values for text alignment of the cell's element when the ElementExporting is raised for the RadGridView. You can refer to the "Exporting" demo for an example.
Didie
the Telerik team
Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.