Solved

C# Infragistics UltraGrid - Export to Excel

Posted on 2016-07-27
2
75 Views
Last Modified: 2016-07-28
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
Comment
Question by:CipherIS
2 Comments
 
LVL 29

Accepted Solution

by:
anarki_jimbel earned 500 total points
ID: 41731957
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
 
LVL 1

Author Comment

by:CipherIS
ID: 41733071
Yes, you are correct.  I needed to add a component.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now