I try to export radgrid to excel. But there is a problem in .xls. The first column (datetime) and second column (string) are not separated. See the image attached. I wonder what is the problem and how to fix it. Thanks.
12 Answers, 1 is accepted
This issue arises because date field is aligned right. In order to avoid this, align the date field before exporting. Attach ExportCellFormatting event where you can apply desired styles. Hope this helps.
C#:
protected
void
RadGrid1_ExportCellFormatting(
object
sender, ExportCellFormattingEventArgs e)
{
GridDataItem item = e.Cell.Parent
as
GridDataItem;
if
(e.FormattedColumn.UniqueName ==
"OrderDate"
)
{
e.Cell.Style[
"text-align"
] =
"center"
;
}
}
Also check the following help documentation which explains more about this.
Word/Excel export (HTML-based).
Thanks,
Shinu.
Thanks for reply. I have a question, where to declare
RadGrid1_ExportCellFormatting
?You can attach the event ExportCellFormatting to your RadGrid which fires when a grid is exporting. The above documentation explains the same which allows you to set styles explicitly before exporting to excel. Hope this helps.
Thanks,
Shinu.
ExportCellFormatting event is available since RadControls for ASP.NET AJAX Q1 2011. For older versions you can use ExcelExportCellFormatting instead.
Regards,
Daniel
the Telerik team
Browse the vast support resources we have to jump start your development with RadControls for ASP.NET AJAX. See how to integrate our AJAX controls seamlessly in SharePoint 2007/2010 visiting our common SharePoint portal.
Thanks a lot. It is working except the event should be OnExcelExportCellFormatting.
One more question:
How to change alignment of header text like "OrderDate"?
Regard,
York
You can change the alignment of header text in ItemCommand event as shown below.
C#:
protected
void
RadGrid1_ItemCommand(
object
sender, GridCommandEventArgs e)
{
if
(e.CommandName == RadGrid.ExportToExcelCommandName)
{
GridHeaderItem HeaderItem = (GridHeaderItem)RadGrid1.MasterTableView.GetItems(GridItemType.Header)[0];
foreach
(TableCell cell
in
HeaderItem.Cells)
{
cell.Style[
"text-align"
] =
"left"
;
}
}
}
Thanks,
Shinu.
The code is working except it is not in
RadGrid1_ItemCommand
, but in RadGrid1_ExportCellFormatting. Thanks.Am very new to telerick control. Please let me know how to export data from radGridview to Excel. Please help me
While esporting the data if there is date field/combo box field in RadGrid, that data is not getting export to Excel instead it is showing empty cell(data missing ).
Thanks in advance. please help me.
Prithvi
Try setting the following.
aspx:
<
ExportSettings
ExportOnlyData
=
"false"
Excel-Format
=
"ExcelML"
></
ExportSettings
>
ExcelML basics.
-Shinu.
if (this.StructureWorkspaceRadTreeView != null)
{
string ext = "xml";
ExportFormat format = ExportFormat.ExcelML;
SaveFileDialog dialog = new SaveFileDialog()
{
DefaultExt = ext,
Filter =
string.Format("{1} files(*.{0})|*.{0}|All files(*.*)|*.*", ext, format),
FilterIndex = 1
};
if (dialog.ShowDialog() == true)
{
using (Stream stream = dialog.OpenFile())
{
GridViewExportOptions options = new GridViewExportOptions();
options.Format = format;
options.ShowColumnHeaders =
true;
options.ShowColumnFooters =
true;
options.ShowGroupFooters =
true;
//options.Encoding = System.Text.Encoding.UTF8;
this.StructureWorkspaceRadTreeView.Export(stream, options);
}
}
}
This Logic is working upto some extent(not export date and combo box values) when written in button event, not at all working when writing in MVVM model(export from code behind).
For instance, if we are writting in MVVM, it is asking for Excel file name, but while opening the Excel file, error is comming(screen shot attached).
Please help me in this regard.....
This error means that there is an error (bad syntax, invalid settings, etc) in the Worksheet settings. I recommend that you examine the log file that is mentioned at the bottom of the error message.
Nevertheless, please post your questions about Silverlight controls in the corresponding forum.
Silverlight GridView subforum
Thanks,
Daniel
the Telerik team