Monday, May 29, 2017

Exporting Telerik WPF GridView to CSV, Excel and Word

As everybody knows, Windows 8 is coming very soon and now a wave of developers and companies are making a big effort to implement their products for this new platform with its new style, etc. This desktop applications are based on Windows Presentation Foundation (or WPF) that it is a computer-software graphical subsystem for rendering user interfaces in Windows-based applications. WPF employs XAML, a derivative of XML, to define and link various UI elements. WPF applications can also be deployed as standalone desktop programs, or hosted as an embedded object in a website. WPF aims to unify a number of common user interface elements, such as 2D/3D rendering, fixed and adaptive documents, typography, vector graphics, runtime animation, and pre-rendered media. These elements can then be linked and manipulated based on various events, user interactions, and data bindings. Microsoft Silverlight provides functionality that is mostly a subset of WPF to provide embedded web controls comparable to Adobe Flash. 3D runtime rendering is supported in Silverlight since Silverlight 5 
Now everybody are seeing that with the controls that Windows is giving inside of Visual Studio 2010 are not fitting their needs or to accomplish them they need more time to develop exactly what they exactly need. But, fortunately we have another options that are doing much easier our lives. They are third party controls with an huge extension of choices, as Infragistics,   DevExpress, Mindscape, Telerik, etc.

<Window x:Class="WpfApplication1.Window1"
    Xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    Xmlns:x = "http://schemas.microsoft.com/winfx/2006/xaml"
    Xmlns:telerik = "http://schemas.telerik.com/2008/xaml/presentation"
    Xmlns:local = "clr-namespace:WpfApplication1"
    Title="Window1">
    <Grid>
        <Grid.Resources>
            <ObjectDataProvider x:Key = "Customers" 
                 ObjectType = "{x:Type local:NorthwindDataContext}" MethodName = "get_Customers">
            </ObjectDataProvider>
        </Grid.Resources>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="100" />
            <ColumnDefinition Width="100" />
            <ColumnDefinition />
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="30" />
            <RowDefinition Height="10" />
            <RowDefinition />
        </Grid.RowDefinitions>
        <ComboBox x:Name="ComboBox1" SelectedIndex="0">
            <ComboBoxItem Content="Excel"/>
            <ComboBoxItem Content="Word"/>
            <ComboBoxItem Content="Csv"/>
        </ComboBox>
        <Button Content="Export" Grid.Column="1" Click="Button_Click" />
        <telerik:RadGridView Name="RadGridView1" ShowGroupPanel="False" 
                             Grid.ColumnSpan="3" Grid.Row="2"
                             ItemsSource="{Binding Source={StaticResource Customers}}" />
    </Grid>
</Window>

After this, we have to write the code to export in the “.cs” file of the Xaml view, with the Button_Click event, that it is calling the method to export the gridview:
private void Button_Click(object sender, RoutedEventArgs e)
{
  string content = "";
  ComboBoxItem comboItem = ComboBox1.SelectedItem as ComboBoxItem;
  string selectedItem = comboItem.Content.ToString();
  if (selectedItem == "Excel")
  {
    extension = "xls";
    content = RadGridView1.ToHtml(true);
  }
  else if (selectedItem == "Word")
  {
    extension = "doc";
    content = RadGridView1.ToHtml(true);
  }
  else if (selectedItem == "Csv")
  {
    extension = "csv";
    content = RadGridView1.ToCsv(true);
  }
  string path = String.Format("Export.{0}", extension);
  if (File.Exists(path)) { File.Delete(path);
  }
  using (FileStream fs = File.Create(path))
  {
    Byte[] info = Encoding.Default.GetBytes(content);
    fs.Write(info, 0, info.Length);
  }
}

As you can see, this process is very simple and easy, but it doesn’t stop here. Telerik have an huge control collections also for another technologies, as well as, Silverlight, Ajax, ASP .NET, Mobilphones and for the most recent Metro Styles of Windows 8 for the new Visual Studio 2012.
I add also the project example to this post to download and see with more detail how I accomplished it.
I hope I have helped you.


No comments:

Post a Comment