Generating Reports in Visual C# 2008 Express Edition

January 3, 2008 at 7:18 pm | Posted in C#, Visual Studio 2008, WPF | 8 Comments

A common question on the MSDN C# Express forum is how do I generate reports?  That is, how does one get a printout of data?  The Standard and higher editions of Visual Studio have reporting services that allow reports to be easily designed in a WYSIWYG way.  But Express users must either buy a third party application (√† la Crystal Reports–about $300 USD) or programmatically create the report using C# and GDI+ Classes.

Now with Visual C# 2008 Express Edition, there is a new option.  Windows Presentation Foundation (WPF) provides a set of classes for converting markup (XAML) to Microsoft’s XML Page Specification (XPS).  XPS documents can then be printed directly from your application or saved an viewed and printed by other applications and platforms.

With this system, there are a couple of options:  First, you can specify the report document directly in C# code much like you would using GDI+.  That is, you could place a textBlock or graphic at a specific x,y coordinate on a page using the properties of these elements.  The second option is almost as good as having a report designer like in the higher Visual Studio editions.  You can use the designer in Visual Studio to layout your report in a WPF grid so you can see exactly how your report should look when it is rendered.  Then it is a very simple matter to pass the name of this grid to XPS generating classes for printing or saving.

Printing and XPS in WPF are covered thoroughly in Chapter 15 of Sells & Griffiths, Programming WPF, O”Reilly.

The following example shows the routine for saving a grid1 (and all that it contains) to an XPS file.

private void button1_Click(object sender, RoutedEventArgs e)
{
    Microsoft.Win32.SaveFileDialog dialog = new Microsoft.Win32.SaveFileDialog();
    if (dialog.ShowDialog() == true)
    {
        string xpsOutputPath = dialog.FileName;
        using (XpsDocument xpsFile = new XpsDocument(xpsOutputPath, FileAccess.Write))
        {
            XpsDocumentWriter xpsdw = XpsDocument.CreateXpsDocumentWriter(xpsFile);
            xpsdw.Write(grid1);
        }
    }

}

 

This routine requires adding a couple of references to the solution: ReachFramework and System.Printing.  You will need to also add some namespace references:

using System.Windows.Xps;
using System.IO;
using Microsoft.Win32;
using System.Windows.Xps.Packaging;

The code for printing directly is not much more complex and is described in the book.

With WPF one should be able to quickly and easily create a rich looking data report that can be printed or saved.

Advertisements

8 Comments »

RSS feed for comments on this post. TrackBack URI

  1. Hello, thanks for the information, but i ned to ask if you have any adress with tutorials or something like that to lear how to use WPF to create reports.

    thanks

  2. Sorry, I don’t yet have a tutorial on this. However, the idea is simple… WPF makes it easy to lay out a page with the information that you want to display. Then you can use the method above to create an .xps file that can be printed.

  3. Hi, I currently have a windows forms application that uses GDI+ to draw my custom cell renderer for a DataGridView. As my print output is not to look like the control but each cell is to look the same I have a custom painter that draws the cells by passing the Graphics object. This allows me to use the same renderer in the DataGridView and printing while having a different layout.

    I also have a PDF function that allows the document to be printed to PDF, though this is an image file for each page.

    I’m wondering how it is possible to create an XPS document and pass it a Graphics object?

  4. creating reports using crystal report is quit easy, but i falied to generate a simple report using the report component in the .net. please help me.

  5. Nice Info, Thanks.

  6. I’ve an other nice solution.

    you can use your data to generate an html code (into a string), and render it in a webbrowser control

    after, you can alos use show the print preview dialog from the webbrowser class to print your report

  7. http://blogs.msdn.com/b/bryanke/archive/2004/02/11/71491.aspx#code
    useful link.

  8. In visual studio 2008 i don’t give option what is data source and design when i was invoke this page.


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

Blog at WordPress.com.
Entries and comments feeds.

%d bloggers like this: