• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 384
  • Last Modified:

C# Infragistics UltraGrid - Export to Excel

Does anyone have a link or sample code to export grid data to excel?

I was attempting to use the code sample from Infragistic website but ran into an issue with this line of code in the initialize component:

            this.ultraGridExcelExporter1 = new Infragistics.Win.UltraWinGrid.ExcelExport.UltraGridExcelExporter(this.components);

Says ExcelExport Namespace can't be found.

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using Infragistics.Documents.Excel;
using Infragistics.Win.UltraWinGrid;
using Infragistics.Win.UltraWinGrid.ExcelExport;

namespace SamplesExplorer
{
    /// <summary>
    /// Summary description for frmExcelExport.
    /// </summary>
    public class frmExcelExport : System.Windows.Forms.Form, ISamplesBase
    {
        internal System.Windows.Forms.Label lblDescription;
        internal Infragistics.Win.UltraWinGrid.UltraGrid UltraGrid1;
        internal System.Windows.Forms.Button btnTitle;
        private IContainer components;
        private System.Windows.Forms.Button ExportToFile;
        private System.Windows.Forms.TextBox exportFileName;
        private UltraGridExcelExporter ultraGridExcelExporter1;
        private TextBox txtExcelFileNamexlsx;
        private Button btnExportxlsxFile;

        //private bool m_bInInit = false;
        private clsADOFlights m_ADOFlights;

        public frmExcelExport()
        {
            //
            // Required for Windows Form Designer support
            //
            InitializeComponent();

            this.InitializeForm();

            //
            // TODO: Add any constructor code after InitializeComponent call
            //

            this.exportFileName.Text = Application.StartupPath + "\\Test.xls";
            this.txtExcelFileNamexlsx.Text = Application.StartupPath + "\\Test1.xlsx";
        }

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        protected override void Dispose( bool disposing )
        {
            if( disposing )
            {
                if(components != null)
                {
                    components.Dispose();
                }
            }
            base.Dispose( disposing );
        }

        #region Windows Form Designer generated code
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.components = new System.ComponentModel.Container();
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(frmExcelExport));
            this.lblDescription = new System.Windows.Forms.Label();
            this.UltraGrid1 = new Infragistics.Win.UltraWinGrid.UltraGrid();
            this.btnTitle = new System.Windows.Forms.Button();
            this.ExportToFile = new System.Windows.Forms.Button();
            this.exportFileName = new System.Windows.Forms.TextBox();
            this.ultraGridExcelExporter1 = new Infragistics.Win.UltraWinGrid.ExcelExport.UltraGridExcelExporter(this.components);
            this.txtExcelFileNamexlsx = new System.Windows.Forms.TextBox();
            this.btnExportxlsxFile = new System.Windows.Forms.Button();
            ((System.ComponentModel.ISupportInitialize)(this.UltraGrid1)).BeginInit();
            this.SuspendLayout();
            // 
            // lblDescription
            // 
            resources.ApplyResources(this.lblDescription, "lblDescription");
            this.lblDescription.Name = "lblDescription";
            // 
            // UltraGrid1
            // 
            resources.ApplyResources(this.UltraGrid1, "UltraGrid1");
            this.UltraGrid1.DataMember = "Shuttles";
            this.UltraGrid1.DisplayLayout.Override.AllowColSizing = Infragistics.Win.UltraWinGrid.AllowColSizing.Free;
            this.UltraGrid1.DisplayLayout.Override.HeaderClickAction = Infragistics.Win.UltraWinGrid.HeaderClickAction.SortMulti;
            this.UltraGrid1.DisplayLayout.ScrollStyle = Infragistics.Win.UltraWinGrid.ScrollStyle.Immediate;
            this.UltraGrid1.Name = "UltraGrid1";
            this.UltraGrid1.InitializeLayout += new Infragistics.Win.UltraWinGrid.InitializeLayoutEventHandler(this.ultraGrid1_InitializeLayout);
            // 
            // btnTitle
            // 
            resources.ApplyResources(this.btnTitle, "btnTitle");
            this.btnTitle.Name = "btnTitle";
            // 
            // ExportToFile
            // 
            resources.ApplyResources(this.ExportToFile, "ExportToFile");
            this.ExportToFile.Name = "ExportToFile";
            this.ExportToFile.Click += new System.EventHandler(this.ExportToFile_Click);
            // 
            // exportFileName
            // 
            resources.ApplyResources(this.exportFileName, "exportFileName");
            this.exportFileName.Name = "exportFileName";
            // 
            // txtExcelFileNamexlsx
            // 
            resources.ApplyResources(this.txtExcelFileNamexlsx, "txtExcelFileNamexlsx");
            this.txtExcelFileNamexlsx.Name = "txtExcelFileNamexlsx";
            // 
            // btnExportxlsxFile
            // 
            resources.ApplyResources(this.btnExportxlsxFile, "btnExportxlsxFile");
            this.btnExportxlsxFile.Name = "btnExportxlsxFile";
            this.btnExportxlsxFile.UseVisualStyleBackColor = true;
            this.btnExportxlsxFile.Click += new System.EventHandler(this.btnExportxlsxFile_Click);
            // 
            // frmExcelExport
            // 
            resources.ApplyResources(this, "$this");
            this.Controls.Add(this.btnExportxlsxFile);
            this.Controls.Add(this.txtExcelFileNamexlsx);
            this.Controls.Add(this.exportFileName);
            this.Controls.Add(this.ExportToFile);
            this.Controls.Add(this.lblDescription);
            this.Controls.Add(this.UltraGrid1);
            this.Controls.Add(this.btnTitle);
            this.Name = "frmExcelExport";
            this.Closing += new System.ComponentModel.CancelEventHandler(this.frmSort_Closing);
            ((System.ComponentModel.ISupportInitialize)(this.UltraGrid1)).EndInit();
            this.ResumeLayout(false);
            this.PerformLayout();

        }
        #endregion

        

        private void frmSort_Closing(object sender, CancelEventArgs e)
        {
            e.Cancel = true;

            this.Visible = false;
        }

        private void ExportToFile_Click(object sender, System.EventArgs e)
        {
            DateTime start = DateTime.Now;

            try
            {
                this.ultraGridExcelExporter1.Export(this.UltraGrid1, this.exportFileName.Text);

                TimeSpan timeSpan = DateTime.Now - start;

                MessageBox.Show(String.Format(SamplesExplorer.Strings.frmExcelExport_ExportedRowsTime, this.totalRowNum, timeSpan.Seconds, timeSpan.Milliseconds) + 
                    "\n\n" + SamplesExplorer.Strings.frmExcelExport_File + this.exportFileName.Text);    
            }
            catch
            {
                MessageBox.Show( SamplesExplorer.Strings.frmExcelExport_ErrorMessage, SamplesExplorer.Strings.frmExcelExport_ErrorTitle );
            }
        }
        private void btnExportxlsxFile_Click(object sender, EventArgs e)
        {
            DateTime start = DateTime.Now;

            try
            {
                this.ultraGridExcelExporter1.Export(this.UltraGrid1, this.txtExcelFileNamexlsx.Text);

                TimeSpan timeSpan = DateTime.Now - start;

                MessageBox.Show(String.Format(SamplesExplorer.Strings.frmExcelExport_ExportedRowsTime, this.totalRowNum, timeSpan.Seconds, timeSpan.Milliseconds) +
                    "\n\n" + SamplesExplorer.Strings.frmExcelExport_File + this.txtExcelFileNamexlsx.Text);
            }
            catch
            {
                MessageBox.Show(SamplesExplorer.Strings.frmExcelExport_ErrorMessage, SamplesExplorer.Strings.frmExcelExport_ErrorTitle);
            }
        }

        private void InitializeForm()
        {
            this.LoadFlightsData();

            this.UltraGrid1.DisplayLayout.Bands[0].Columns[0].AllowRowSummaries = AllowRowSummaries.True;
            this.UltraGrid1.DisplayLayout.Bands[0].Columns[1].AllowRowSummaries = AllowRowSummaries.True;
        }

        private void LoadFlightsData()
        {
            if (this.m_ADOFlights == null)
                this.m_ADOFlights = new clsADOFlights();

            if (this.m_ADOFlights.DBExists)
                this.UltraGrid1.SetDataBinding(this.m_ADOFlights.GetDataSet, this.m_ADOFlights.GetRootTable);
        }

        private void ultraGrid1_InitializeLayout(object sender, Infragistics.Win.UltraWinGrid.InitializeLayoutEventArgs e)
        {
            e.Layout.ViewStyleBand = Infragistics.Win.UltraWinGrid.ViewStyleBand.Vertical;
            
            e.Layout.Override.HeaderClickAction =  Infragistics.Win.UltraWinGrid.HeaderClickAction.SortMulti;
            e.Layout.Bands[0].Columns[0].Header.VisiblePosition = 0;
            //e.Layout.Bands[0].Columns[3].Hidden = true;
            e.Layout.Bands[1].Columns[0].Hidden = true;
            e.Layout.Bands[1].Columns[1].Hidden = true;
            e.Layout.Bands[2].Columns[0].Hidden = true;
        }

        #region ISamplesBase Interface

        void ISamplesBase.HideMe()
        {
            this.Hide();
        }

        void ISamplesBase.ShowMe( Form owner )
        {
            this.InitializeForm();
            if (owner != null)
                this.Owner = owner;

            this.Show();
            this.CancelButton = btnTitle;
            this.CenterToScreen();
            this.MaximizeBox = true;
        }

        string ISamplesBase.Description
        {
            get { return lblDescription.Text; }
        }

        string ISamplesBase.SampleName
        {
            get { return btnTitle.Text; }
        }

        System.Drawing.Image ISamplesBase.PreviewPicture
        {
            get
            {
                return Image.FromFile( clsSamplesPath.GraphicsPath + @"\Previews\ExcelExport.png");
            }
        }

        SamplesExplorer.Constants_SampleType ISamplesBase.SampleType
        {
            get { return SamplesExplorer.Constants_SampleType.SampleType_Feature; }
        }

        #endregion ISamplesBase Interface

        private void ultraGridExcelExporter1_EndExport(object sender, EndExportEventArgs e)
        {
            this.totalRowNum = e.CurrentRowIndex;
        }

        private int totalRowNum = 0;

        private void ultraGridExcelExporter1_InitializeColumn(object sender, InitializeColumnEventArgs e)
        {
            if(e.Column.Key == "Departure_Date")
                e.ExcelFormatStr = "[$-409]mmmm d, yyyy;@";
        }

        
    }
}

Open in new window

0
CipherIS
Asked:
CipherIS
1 Solution
 
anarki_jimbelCommented:
Yes, this code is from the Infragistics web site.

I believe you just have no some component.

I'd suggest you discuss the problem on some of Infragistics forums: http://www.infragistics.com/community/forums/ .
Or http://www.infragistics.com/community/forums/178.aspx.

As far as I remember no Infragistics questions were answered here. Most people just don't know about it at all. I used it years ago, now haven't it installed on my computer and unfortunately can't look at the problem more closely.
0
 
CipherISAuthor Commented:
Yes, you are correct.  I needed to add a component.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now