RSS

telerik radgridview export to excel with format

26 Jan

Hi Dear……

if you want to export to excel with ideally in report format then use this code

testexprot.xaml.cs

private void btnExport_Click(object sender, RoutedEventArgs e)
{

ExpExcelReport();

}

private void grdNumbers_ElementExporting(object sender, GridViewElementExportingEventArgs e)
{

if (e.Element == ExportElement.HeaderRow ||
e.Element == ExportElement.HeaderCell)
{
e.FontSize = e.FontSize + 22;
e.FontWeight = FontWeights.Bold;
e.Foreground = Colors.Blue;
e.Background = Colors.DarkGray;
}
//Change the font size and the background color of the group headers only
else if (e.Element == ExportElement.GroupHeaderCell ||
e.Element == ExportElement.GroupHeaderRow)
{
e.FontSize = e.FontSize + 2;
e.Background = Colors.LightGray;
}
else if (e.Element == ExportElement.GroupFooterCell ||
e.Element == ExportElement.GroupFooterRow)
{
e.FontWeight = FontWeights.Bold;
e.FontSize = e.FontSize + 2;
e.Background = Colors.LightGray;
}

}
private void radGridView1_ElementExporting(object sender, GridViewElementExportingEventArgs e)
{

if (e.Element == ExportElement.HeaderRow ||
e.Element == ExportElement.HeaderCell)
{
e.FontSize = e.FontSize + 22;
e.FontWeight = FontWeights.Bold;
e.Foreground = Colors.Blue;
e.Background = Colors.DarkGray;
}
//Change the font size and the background color of the group headers only
else if (e.Element == ExportElement.GroupHeaderCell ||
e.Element == ExportElement.GroupHeaderRow)
{
e.FontSize = e.FontSize + 2;
e.Background = Colors.LightGray;
}
else if (e.Element == ExportElement.GroupFooterCell ||
e.Element == ExportElement.GroupFooterRow)
{
e.FontWeight = FontWeights.Bold;
e.FontSize = e.FontSize + 2;
e.Background = Colors.LightGray;
}

}

private void ExpExcelReport()
{

// this below line write where your reportGrid1 page exist edit his requirment becoz i create control/page/reportfilter seperate folder so i write according my reqirment so changed it
if ((((System.Windows.Controls.ScrollViewer)((System.Windows.Controls.Grid)this.Parent).Parent).Parent as SetMyLeads.Silverlight.UI.Pages.AgentReportingPage).reportGrid1.radGridView1.Items.Count > 0)
{

string extension = “xls”;
SaveFileDialog dialog = new SaveFileDialog()
{
DefaultExt = extension,
Filter = String.Format(“{1} files (*.{0})|*.{0}|All files (*.*)|*.*”, extension, “Excel”),
FilterIndex = 1
};
if (dialog.ShowDialog() == true)
{

int originalPageSize = (((System.Windows.Controls.ScrollViewer)((System.Windows.Controls.Grid)this.Parent).Parent).Parent as SetMyLeads.Silverlight.UI.Pages.AgentReportingPage).reportGrid1.radDataPager1.PageSize;
int originalPageIndex = (((System.Windows.Controls.ScrollViewer)((System.Windows.Controls.Grid)this.Parent).Parent).Parent as SetMyLeads.Silverlight.UI.Pages.AgentReportingPage).reportGrid1.radDataPager1.PageIndex;

using (System.IO.Stream stream = dialog.OpenFile())
{
(((System.Windows.Controls.ScrollViewer)((System.Windows.Controls.Grid)this.Parent).Parent).Parent as SetMyLeads.Silverlight.UI.Pages.AgentReportingPage).reportGrid1.radDataPager1.PageSize = 0;

Telerik.Windows.Controls.GridViewExportOptions options = new Telerik.Windows.Controls.GridViewExportOptions();
Telerik.Windows.Controls.ExportFormat format = Telerik.Windows.Controls.ExportFormat.Html;
options.Format = format;

options.ShowColumnFooters = true;
options.ShowColumnHeaders = true;
options.ShowGroupFooters = true;
options.Encoding = UTF8Encoding.UTF8;

System.Windows.Controls.ListBoxItem selected = lbLinks.SelectedItem as System.Windows.Controls.ListBoxItem;

string unicodeString = ” <b><font size=18 ><center>” + selected.Content.ToString() + “</center></font></b><br>”;
string _DateRange = (unicodeString + “<center><b><font size=4 >Date From: </font></b>” + Convert.ToDateTime(txtfromDate.SelectedDate).ToString(“yyyy-MM-dd”) + ”  ” + “<b><font size=4 >Date To:  </font></b>” + Convert.ToDateTime(txttoDate.SelectedDate).ToString(“yyyy-MM-dd”) + “</center>”);
byte[] title = Encoding.UTF8.GetBytes(_DateRange + System.Environment.NewLine);
stream.Write(title, 0, title.Length);

(((System.Windows.Controls.ScrollViewer)((System.Windows.Controls.Grid)this.Parent).Parent).Parent as SetMyLeads.Silverlight.UI.Pages.AgentReportingPage).reportGrid1.radGridView1.ElementExporting += radGridView1_ElementExporting;

(((System.Windows.Controls.ScrollViewer)((System.Windows.Controls.Grid)this.Parent).Parent).Parent as SetMyLeads.Silverlight.UI.Pages.AgentReportingPage).reportGrid1.radGridView1.Export(stream, options);

}

//Which is use for page which export all pages record of gridview otherwize export first page of grid
(((System.Windows.Controls.ScrollViewer)((System.Windows.Controls.Grid)this.Parent).Parent).Parent as SetMyLeads.Silverlight.UI.Pages.AgentReportingPage).reportGrid1.radDataPager1.PageSize = originalPageSize;
(((System.Windows.Controls.ScrollViewer)((System.Windows.Controls.Grid)this.Parent).Parent).Parent as SetMyLeads.Silverlight.UI.Pages.AgentReportingPage).reportGrid1.radDataPager1.PageIndex = originalPageIndex;

}
}
else
{
MessageBox.Show(“There are no records to export.”, “Export Warning!”, MessageBoxButton.OK);
}

}

 

Advertisements
 
Leave a comment

Posted by on January 26, 2012 in Silverlight

 

Tags: , , , ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: