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

Problem in export radgrid to excel

12 Answers 342 Views
Grid
This is a migrated thread and some comments may be shown as answers.
york
Top achievements
Rank 1
york asked on 21 Jun 2011, 06:27 AM
Hi,
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

Sort by
0
Accepted
Shinu
Top achievements
Rank 2
answered on 21 Jun 2011, 08:35 AM
Hello York,

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.
0
york
Top achievements
Rank 1
answered on 21 Jun 2011, 09:59 PM
Hi Sindu,
Thanks for reply. I have a question, where to declare RadGrid1_ExportCellFormatting?
0
Shinu
Top achievements
Rank 2
answered on 22 Jun 2011, 05:17 AM
Hello York,

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.
0
Accepted
Daniel
Telerik team
answered on 22 Jun 2011, 07:40 AM
Hello York,

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.

0
york
Top achievements
Rank 1
answered on 23 Jun 2011, 05:24 AM
Hi Shinu and Daniel,

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
0
Accepted
Shinu
Top achievements
Rank 2
answered on 24 Jun 2011, 08:41 AM
Hello 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.
0
york
Top achievements
Rank 1
answered on 28 Jun 2011, 06:30 AM
Hi Shinu,
The code is working except  it is not in RadGrid1_ItemCommand, but in RadGrid1_ExportCellFormatting. Thanks.

0
Prithvi
Top achievements
Rank 1
answered on 08 Mar 2012, 04:05 PM
Hi,
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
0
Shinu
Top achievements
Rank 2
answered on 09 Mar 2012, 05:05 AM
Hello,

Try setting the following.
aspx:
<ExportSettings ExportOnlyData="false" Excel-Format="ExcelML"></ExportSettings>
Also check the following help documentation.
ExcelML basics.

-Shinu.
0
Prithvi
Top achievements
Rank 1
answered on 09 Mar 2012, 07:08 AM
@abv....sry...not in ASP.Net.....I want that in Silverlight
0
Prithvi
Top achievements
Rank 1
answered on 09 Mar 2012, 10:46 AM

 

 

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.....
0
Daniel
Telerik team
answered on 13 Mar 2012, 10:14 AM
Hello Prithvi,

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
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
Tags
Grid
Asked by
york
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
york
Top achievements
Rank 1
Daniel
Telerik team
Prithvi
Top achievements
Rank 1
Share this question
or