ArcObjects Library Reference  

ExportReport

About the Generate a report Sample

[C#]

ExportReport.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using ESRI.ArcGIS.ArcMap;
using ESRI.ArcGIS.ArcMapUI;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Framework;
using System.IO;

namespace CustomReport_CS
{
    public partial class ExportReport : Form
    {
        #region Member Variables
        String m_FileLocation;
        Dictionary<Int32, String> reports;
        #endregion

        public ExportReport()
        {
            InitializeComponent();
        }

        private void ExportReport_Load(object sender, EventArgs e)
        {
            // store the templates information
            reports = new Dictionary<Int32,String>();
            m_FileLocation = CustomReport_CS.Properties.Settings.Default.DataLocation;
            String[] filePaths = Directory.GetFiles(m_FileLocation, "*.rlf");
            XmlDocument doc = new XmlDocument();
            try
            {
                for (Int32 c = 0; c <= (filePaths.Count() - 1); c += 1)
                {
                    String fileLocation = filePaths[c];
                    doc.Load(fileLocation);
                    String title = this.ReadRTFElement(doc, "/Report", "Name");
                    reports.Add(c, fileLocation);
                    this.lstReports.Items.Add(title);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
            finally
            {
                doc = null;
            }
        }
        private void btnGenerate_Click(object sender, EventArgs e)
        {
            SaveFileDialog savDialog = new SaveFileDialog();
            savDialog.Filter = "PDF(*.pdf)\"|*.pdf";
            savDialog.AddExtension = true;
            DialogResult result;
            result = savDialog.ShowDialog();
            String savFile = savDialog.FileName;

            if (result == DialogResult.OK)
            {
                IMxDocument mxDoc;
                IReportDataSource rDS = new Report();
                IReportTemplate rwTemplate;
                IReportEngine rwEngine;
                Process openPDFProcess = new Process();
                try
                {
                    mxDoc = (IMxDocument)ArcMap.Application.Document;

                    for (int i = 0; i < mxDoc.FocusMap.LayerCount; i += 1)
                    {
                        if (mxDoc.FocusMap.get_Layer(i).Name == "Counties")
                        {
                            rDS.Layer = mxDoc.FocusMap.get_Layer(i);
                            break;
                        }
                    }
                    rwTemplate = (IReportTemplate)rDS;
                    rwTemplate.LoadReportTemplate(m_FileLocation);
                    rwEngine = (IReportEngine)rDS;
                    rwEngine.RunReport(null);
                    String pdfReport = (savFile);
                    rwEngine.ExportReport(pdfReport, "1", esriReportExportType.esriReportExportPDF);

                    // launch PDF
                    openPDFProcess.StartInfo.UseShellExecute = true;
                    openPDFProcess.StartInfo.CreateNoWindow = true;
                    openPDFProcess.StartInfo.FileName = pdfReport;
                    openPDFProcess.Start();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
                finally
                {
                    mxDoc = null;
                    rDS = null;
                    rwTemplate = null;
                    rwEngine = null;
                    openPDFProcess = null;
                }
            }
            else
            {
                savDialog.Dispose();
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void lstReports_SelectedIndexChanged(object sender, EventArgs e)
        {
            m_FileLocation = reports[lstReports.SelectedIndex];
            this.txtReportInformation.Clear();
            XmlDocument doc = new XmlDocument();
            try
            {
                doc.Load(m_FileLocation);
                // Data source
                txtReportInformation.Text = "DataSource:" + Environment.NewLine +
                                            ReadRTFElement(doc, "/Report/DataSource/Workspace", "PathName");
                // Data name
                txtReportInformation.Text = txtReportInformation.Text + Environment.NewLine + "Name:" + Environment.NewLine +
                                            ReadRTFElement(doc, "/Report/DataSource", "Name");
                // Fields
                txtReportInformation.Text = txtReportInformation.Text + Environment.NewLine + "Fields:" + Environment.NewLine +
                                            ReadRTFElement(doc, "/Report/ReportFields/Field", "Name");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
            finally
            {
                doc = null;
            }
        }

        private String ReadRTFElement(XmlDocument doc, String tagLocation, String elementName)
        {
           
            String retValue = "";
            // Get and display all the book titles.
            XmlElement root = doc.DocumentElement;
            XmlNodeList elemList = root.SelectNodes(tagLocation);
            try
            {
                foreach (XmlNode title in elemList)
                {
                    if (retValue == "")
                    {
                        retValue = title.Attributes[elementName].Value + Environment.NewLine;
                    }
                    else
                    {
                        retValue = retValue + title.Attributes[elementName].Value + Environment.NewLine;
                    }
                }
                return retValue;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                root = null;
                elemList = null;
            }
        }
    }
}

[Visual Basic .NET]

ExportReport.vb

Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Diagnostics
Imports System.Drawing
Imports System.Linq
Imports System.Text
Imports System.Windows.Forms
Imports System.Xml
Imports ESRI.ArcGIS.ArcMap
Imports ESRI.ArcGIS.ArcMapUI
Imports ESRI.ArcGIS.esriSystem
Imports ESRI.ArcGIS.Framework
Imports System.IO

Public Class ExportReport
#Region "Member Variables"
    Private m_FileLocation As String
    Private reports As Dictionary(Of Int32, String)
#End Region

    Public Sub New()

        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        ' store the templates information
        reports = New Dictionary(Of Int32, String)()
        m_FileLocation = CustomReportVBNET.My.Settings.DataLocation
        Dim filePaths As String() = Directory.GetFiles(m_FileLocation, "*.rlf")
        Dim doc As XmlDocument = New XmlDocument()
        Try
            Dim c As Int32 = 0
            Do While c <= (filePaths.Count() - 1)
                Dim fileLocation As String = filePaths(c)
                doc.Load(fileLocation)
                Dim title As String = Me.ReadRTFElement(doc, "/Report", "Name")
                reports.Add(c, fileLocation)
                Me.lstReports.Items.Add(title)
                c += 1
            Loop
        Catch ex As Exception
            MessageBox.Show(ex.ToString())
        Finally
            doc = Nothing
        End Try
    End Sub

    Private Function ReadRTFElement(ByVal doc As XmlDocument, ByVal tagLocation As String, ByVal elementName As String) As String

        Dim retValue As String = ""
        ' Get and display all the book titles.
        Dim root As XmlElement = doc.DocumentElement
        Dim elemList As XmlNodeList = root.SelectNodes(tagLocation)
        Try
            For Each title As XmlNode In elemList
                If retValue = "" Then
                    retValue = title.Attributes(elementName).Value & Environment.NewLine
                Else
                    retValue = retValue & title.Attributes(elementName).Value & Environment.NewLine
                End If
            Next title
            Return retValue
        Catch ex As Exception
            Throw ex
        Finally
            root = Nothing
            elemList = Nothing
        End Try
    End Function

    Private Sub btnGenerate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGenerate.Click
        Dim savDialog As SaveFileDialog = New SaveFileDialog()
        savDialog.Filter = "PDF(*.pdf)""|*.pdf"
        savDialog.AddExtension = True
        Dim result As DialogResult
        result = savDialog.ShowDialog()
        Dim savFile As String = savDialog.FileName

        If result = System.Windows.Forms.DialogResult.OK Then
            Dim mxDoc As IMxDocument
            Dim rDS As IReportDataSource = New Report()
            Dim rwTemplate As IReportTemplate
            Dim rwEngine As IReportEngine
            Dim openPDFProcess As Process = New Process()
            Try
                mxDoc = CType(My.ArcMap.Application.Document, IMxDocument)

                Dim i As Integer = 0
                For i = 0 To mxDoc.FocusMap.LayerCount - 1
                    If mxDoc.FocusMap.Layer(i).Name = "Counties" Then
                        rDS.Layer = mxDoc.FocusMap.Layer(i)
                        Exit For
                    End If
                Next i
                rwTemplate = CType(rDS, IReportTemplate)
                rwTemplate.LoadReportTemplate(m_FileLocation)
                rwEngine = CType(rDS, IReportEngine)
                rwEngine.RunReport(Nothing)
                Dim pdfReport As String = (savFile)
                rwEngine.ExportReport(pdfReport, "1", esriReportExportType.esriReportExportPDF)

                ' launch PDF
                openPDFProcess.StartInfo.UseShellExecute = True
                openPDFProcess.StartInfo.CreateNoWindow = True
                openPDFProcess.StartInfo.FileName = pdfReport
                openPDFProcess.Start()
            Catch ex As Exception
                MessageBox.Show(ex.ToString())
            Finally
                mxDoc = Nothing
                rDS = Nothing
                rwTemplate = Nothing
                rwEngine = Nothing
                openPDFProcess = Nothing
            End Try
        Else
            savDialog.Dispose()
        End If
    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Me.Dispose()
    End Sub

    Private Sub lstReports_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstReports.SelectedIndexChanged
        m_FileLocation = reports(lstReports.SelectedIndex)
        Me.txtReportInformation.Clear()
        Dim doc As XmlDocument = New XmlDocument()
        Try
            doc.Load(m_FileLocation)
            ' Data source
            txtReportInformation.Text = "DataSource:" & Environment.NewLine & ReadRTFElement(doc, "/Report/DataSource/Workspace", "PathName")
            ' Data name
            txtReportInformation.Text = txtReportInformation.Text & Environment.NewLine & "Name:" & Environment.NewLine & ReadRTFElement(doc, "/Report/DataSource", "Name")
            ' Fields
            txtReportInformation.Text = txtReportInformation.Text & Environment.NewLine & "Fields:" & Environment.NewLine & ReadRTFElement(doc, "/Report/ReportFields/Field", "Name")
        Catch ex As Exception
            MessageBox.Show(ex.ToString())
        Finally
            doc = Nothing
        End Try
    End Sub
End Class