Problem while exporting Radgrid to Excel

6 posts, 2 answers
  1. Lokesh
    Lokesh avatar
    89 posts
    Member since:
    Jul 2012

    Posted 13 Jul 2011 Link to this post

    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..
  2. Answer
    Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 13 Jul 2011 Link to this post

    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.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Lokesh
    Lokesh avatar
    89 posts
    Member since:
    Jul 2012

    Posted 13 Jul 2011 Link to this post

    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..
  5. Answer
    Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 13 Jul 2011 Link to this post

    Hello Lokesh,

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

    Thanks,
    Princy.
  6. Dilip
    Dilip avatar
    2 posts
    Member since:
    Dec 2015

    Posted 22 Dec 2015 Link to this post

    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;
            }
        }

  7. Eyup
    Admin
    Eyup avatar
    3005 posts

    Posted 25 Dec 2015 Link to this post

    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.
     
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017