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

Problem while exporting Radgrid to Excel

5 Answers 222 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Lokesh
Top achievements
Rank 1
Lokesh asked on 13 Jul 2011, 07:33 AM
Hi Team,
I have a radgrid which I want to export to Excel.
Columns and data in my grid, both are left aligned (please find attached images.)
But when I export the grid to excel, headers align center whereas data align left or right.
I tried with ExcelExportCellFormatting event, but got no success.

Here is my code :
protected void rgContracts_ExcelExportCellFormatting(object sender, ExcelExportCellFormattingEventArgs e)
      {
          try
          {
              GridDataItem item = e.Cell.Parent as GridDataItem;
              if (e.FormattedColumn.UniqueName == "ContractNo")
                  e.Cell.Style["Horizontal-Align"] = "Right";
              if (item.ItemType == GridItemType.Item)
                  item.Style["Horizontal-Align"] = "Left";
              if (item.ItemType == GridItemType.Header)
                  e.Cell.Style["Horizontal-Align"] = "Right";
          }
          catch (Exception ex)
          {
              XITingExceptionProcessor.ProcessException(this, ex);
          }
      }

and on button click event :
protected void btnReport_Click(object sender, EventArgs e)
       {
           try
           {
foreach (GridColumn item in rgContracts.MasterTableView.Columns)
               {
                   if ((item.UniqueName == "TemplateColumn_Select"))
                       item.Visible = false;
                   if(item.UniqueName=="Details")
                       item.Visible=false;
               }
               rgContracts.AllowSorting = false;
               rgContracts.AllowFilteringByColumn = false;
               rgContracts.ExportSettings.IgnorePaging = true;
               rgContracts.ExportSettings.OpenInNewWindow = true;
               rgContracts.ExportSettings.ExportOnlyData = true;
               rgContracts.MasterTableView.ExportToExcel();
}
           catch (Exception ex)
           {
               XITingExceptionProcessor.ProcessException(this, ex);
           }
       }

Please find attached the images of my grid and report.

Could you please help me out ??

Regards,
Lok..

5 Answers, 1 is accepted

Sort by
0
Accepted
Princy
Top achievements
Rank 2
answered on 13 Jul 2011, 08:00 AM
Hello Lokesh,

Try setting the property "text-align" as center in ExportcellFormatting event.

C#:
protected void RadGrid1_ExportCellFormatting(object sender, ExportCellFormattingEventArgs e)
{
           GridDataItem item = e.Cell.Parent as GridDataItem;
           if (e.FormattedColumn.UniqueName == "ContactNo")
        {
            e.Cell.Style["text-align"] = "center";
        }
}

Thanks,
Princy.
0
Lokesh
Top achievements
Rank 1
answered on 13 Jul 2011, 08:15 AM
Hey Princy,
Thanks a lot.
Once again u helped me.   :) :)

Could you please tell me, how can I get all such properties like for e.g. style["text-align"] ??
I dont want to disturb you in future.
 :)

Regards,
Lok..
0
Accepted
Princy
Top achievements
Rank 2
answered on 13 Jul 2011, 08:23 AM
Hello Lokesh,

The style which you use for customizing web application can be used for customizing the appearance of exported excel.

Thanks,
Princy.
0
Dilip
Top achievements
Rank 1
answered on 22 Dec 2015, 12:00 PM

Columns alignment : 

 

 protected void grdPreview_ExcelExportCellFormatting(object sender, ExcelExportCellFormattingEventArgs e)
    {
        GridDataItem item = e.Cell.Parent as GridDataItem;
        if (e.FormattedColumn.UniqueName == "Policy_Number")
        {
            e.Cell.HorizontalAlign = HorizontalAlign.Right;
        }
    }

0
Eyup
Telerik team
answered on 25 Dec 2015, 07:31 AM
Hi Dilip,

Thanks for sharing your approach with our community. I hope it may prove helpful to other developers as well. There is also detailed information available in the following article regarding customization approaches for PDF exporting:
http://docs.telerik.com/devtools/aspnet-ajax/controls/grid/functionality/exporting/export-formats/pdf-export#border-support

Regards,
Eyup
Telerik
 
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
Lokesh
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Lokesh
Top achievements
Rank 1
Dilip
Top achievements
Rank 1
Eyup
Telerik team
Share this question
or