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

Excel Reporting from RadGridView

5 Answers 73 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Siddhart
Top achievements
Rank 1
Siddhart asked on 30 Apr 2013, 04:42 PM
Hi,

I am using following method to save my Radgridview data to excel but am not able to,

save my data coming into this grid, Any Idea??

Thanks for the Great Help...!!

5 Answers, 1 is accepted

Sort by
0
Dimitrina
Telerik team
answered on 01 May 2013, 07:08 AM
Hi,

Would you please share how do you export the data when the "SaveToExcelCommand" is invoked? 

Greetings,
Didie
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

0
Siddhart
Top achievements
Rank 1
answered on 01 May 2013, 06:51 PM
public static void SaveToExcel(object parameter, RadGridView radGridView, params string[] columnsRequiredFormatting)
       {
           try
           {
               string newLine = "\n";
               string tab = "\t";
               SaveFileDialog saveFileDialog = new SaveFileDialog();
               saveFileDialog.Filter = "Excel Files (.xls)|*.xls|All Files (*.*)|*.*";
               var objectList = (IList)parameter;
               bool? dialogResult = saveFileDialog.ShowDialog();
               if (dialogResult.Value)
               {
                   TextWriter writer = new StreamWriter(saveFileDialog.OpenFile());
                   try
                   {
                       IList<string> binders = new List<string>();
                       writer.Write(string.Format("<html xmlns:x=\"urn:schemas-microsoft-com:office:excel\"> <table border=\"1\" width=\"100%\">"));
                       if (radGridView != null)
                       {
                           writer.Write(string.Format("<thead>"));
                           writer.Write(string.Format("<tr>"));
                           binders = radGridView.Columns.Where(c => c.Visibility == Visibility.Visible).Select(c =>
                           {
                               writer.Write("<th bgcolor=\"#5882FA\">" + c.Header.ToString() + "</th>");
                               writer.Write(tab);
                               return c.ClipboardContentBinding.Path.Path.ToString();
                           }).ToList();
                           writer.Write(string.Format("</tr>"));
                           writer.Write(string.Format("</thead>"));
                       }
                       writer.Write(newLine);
                       if (objectList.Count == 0)
                       {
                           writer.Write(string.Format("<tbody>"));
                           writer.Write(string.Format("<tr>"));
                           writer.WriteLine("There are no rows  to be written under this section");
                           writer.Write(string.Format("</tr>"));
                           writer.Write(string.Format("</tbody>"));
                           writer.Write(newLine);
                           writer.Write(newLine);
                           return;
                       }
                       string tdFormat = "<td x:str=\"'{0}\">{0}</td>";
                       string tdSimple = "<td>{0}</td>";
                       writer.Write(string.Format("<tbody>"));
                       if (columnsRequiredFormatting == null)
                       {
                           foreach (var reportData in objectList)
                           {
                               writer.Write(string.Format("<tr>"));
                               foreach (string binder in binders)
                               {
                                   //Bar the properties which should not go 
                                   string propVal = string.Empty;
                                   PropertyInfo propertyInfo = reportData.GetType().GetProperty(binder);
                                   object prop = reportData.GetType().GetProperty(binder).GetValue(reportData, null);
                                   if (prop != null)
                                       propVal = prop.ToString();
                                   writer.Write(String.Format(tdSimple, propVal));
                               }
                               writer.Write(string.Format("</tr>"));
                           }
                       }
                       else
                       {
                           foreach (var reportData in objectList)
                           {
                               writer.Write(string.Format("<tr>"));
                               //IEnumerable<bool> a = binders.Select(c =>
                               // {
                               //     object propVal = "";
                               //     bool formatReq = false;
                               //     PropertyInfo prop = reportData.GetType().GetProperty(c);
                               //     propVal = prop.GetValue(reportData, null);
                               //     if (columnsRequiredFormatting.Contains(prop.Name.ToString()))
                               //         formatReq = true;
                               //     if (formatReq)
                               //         writer.Write(String.Format(tdFormat, propVal.ToString()));
                               //     else
                               //         writer.Write(String.Format(tdSimple, propVal.ToString()));
                               //     return true;
                               // });
                               foreach (string binder in binders)
                               {
                                   //Bar the properties which should not go 
                                   string propVal = string.Empty;
                                   PropertyInfo propertyInfo = reportData.GetType().GetProperty(binder);
                                   object prop = reportData.GetType().GetProperty(binder).GetValue(reportData, null);
                                   if (prop != null)
                                       propVal = prop.ToString();
                                   if (columnsRequiredFormatting.Contains(propertyInfo.Name.ToString()))
                                       writer.Write(String.Format(tdFormat, propVal));
                                   else
                                       writer.Write(String.Format(tdSimple, propVal));
                               }
                               writer.Write(string.Format("</tr>"));
                           }
                       }
                       writer.Write(string.Format("</tbody>"));
                       writer.Write("</table> </html>");
                   }
                   finally
                   {
                       if (writer != null)
                       {
                           writer.Flush();
                           writer.Close();
                       }
                   }
               }
           }
           catch (Exception ex)
           {
               //MessageBox.Show(ex.ToString());
               throw;
           }
       }
I am using Save to excel code above  Parameter, for That Save to excel even i put this code in view model,

#region

 

 

Public Methods

 

 

 

 

 

 

public void SaveToExcel(object parameter)

 

{

 

 

DataGrid dg = parameter as DataGrid;

 

 

 

Utils.SaveToExcel(PaymentDataList, dg, "xyz", "abc", "mno");

 

}

 

 

public bool SaveToExcelCanExecute(object parameter)

 

{

 

 

return true;

 

}

i am still not getting into it,to catch save to excel.

Thanks
Siddharth


0
Dimitrina
Telerik team
answered on 02 May 2013, 10:31 AM
Hello,

As I can see you write the data element by element. Do you mean that the PaymentDataList CommandParamenter is empty and does not have data?

I would suggest you to check this online demo on how the command is invoked and the data is exported. The idea would be the same, although you do not use the in built exporting functionality.

All the best,
Didie
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

0
Siddhart
Top achievements
Rank 1
answered on 02 May 2013, 03:21 PM
Hi,
i saw it, far better than typical exporting..but i quickl question how do you denote Examples, What exactly you mean by this??

<Examples:GridViewExample.Resources>
  <local:ExportingModel x:Key="context" />


I simply start my code like this and below down after defining user control resources i define grid and underthat radgridview. 
<UserControl x:Class="Sid.Modules.Views.CurrentYearDenomView"
           
    xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity" 
    xmlns:local="clr-namespace:TaxReporting.Modules.Views"      
    xmlns:sys="clr-namespace:System;assembly=mscorlib"         
    mc:Ignorable="d"
      d:DesignHeight="660" d:DesignWidth="440" 
    xmlns:conv="clr-namespace:TaxReporting.Modules.Converters"
    xmlns:data="clr-namespace:TaxReporting.Modules.ViewModels"
    xmlns:telerikGrp="clr-namespace:Telerik.Windows.Controls.GridView;assembly=Telerik.Windows.Controls.GridView"
    xmlns:type="clr-namespace:System;assembly=mscorlib"
             >
  
    <UserControl.Resources>
  
          
         
  
        <!--<Style x:Key="NewStyle"
                   TargetType="telerik:GridViewRow"
                   BasedOn="{StaticResource GridViewRowStyle}">
                    <Setter Property="Background"
                        Value="{StaticResource AccentBrush}" />
                    <Setter Property="Foreground"
                        Value="{StaticResource MainBrush}" />
                </Style>
                <Style x:Key="ExistingStyle"
                   TargetType="telerik:GridViewRow"
                   BasedOn="{StaticResource GridViewRowStyle}" />

Thanks
Sid
0
Dimitrina
Telerik team
answered on 03 May 2013, 11:26 AM
Hi Sid,

This is the Resources section of your UserControl.
You could interpret it like so:

<UserControl x:Class="RadGridView_SL5_AR_1.MainPage"
             ...
      xmlns:local="clr-namespace:RadGridView_SL5_AR_1">
<UserControl.Resources>
    <local:ExportingModel x:Key="context" />
</UserControl.Resources>

 

Kind regards,
Didie
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

Tags
General Discussions
Asked by
Siddhart
Top achievements
Rank 1
Answers by
Dimitrina
Telerik team
Siddhart
Top achievements
Rank 1
Share this question
or