Solved

C# Infragistics UltraGrid - Export to Excel

Posted on 2016-07-27
2
52 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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
There have always been a lot of questions related to when Crystal Reports evaluates report components (such as formulas, summaries, cross-tabs, charts, to name a few examples). Crystal Reports uses a two-pass reporting process to provide greater …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

758 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

22 Experts available now in Live!

Get 1:1 Help Now