Click here to Skip to main content
15,888,454 members
Articles / Desktop Programming / Windows Forms

How to export GridView to Excel, render cells, add WordArt, and fill a custom template

Rate me:
Please Sign up or sign in to vote.
4.82/5 (49 votes)
9 Nov 20063 min read 544.5K   11.2K   228  
Export Gridview to Excel, render cells, add WordArt, and fill a custom template.
namespace GridviewToExcel
{
    partial class Form1
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (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.dg1 = new System.Windows.Forms.DataGridView();
            this.progressBar1 = new System.Windows.Forms.ProgressBar();
            this.btn2Excel = new System.Windows.Forms.Button();
            this.btnUseTemplate = new System.Windows.Forms.Button();
            this.label1 = new System.Windows.Forms.Label();
            this.saveFileDialog1 = new System.Windows.Forms.SaveFileDialog();
            ((System.ComponentModel.ISupportInitialize)(this.dg1)).BeginInit();
            this.SuspendLayout();
            // 
            // dg1
            // 
            this.dg1.AllowUserToAddRows = false;
            this.dg1.AllowUserToDeleteRows = false;
            this.dg1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
            this.dg1.Location = new System.Drawing.Point(11, 10);
            this.dg1.Name = "dg1";
            this.dg1.ReadOnly = true;
            this.dg1.RowTemplate.Height = 15;
            this.dg1.Size = new System.Drawing.Size(713, 260);
            this.dg1.TabIndex = 0;
            // 
            // progressBar1
            // 
            this.progressBar1.Location = new System.Drawing.Point(10, 325);
            this.progressBar1.Name = "progressBar1";
            this.progressBar1.Size = new System.Drawing.Size(714, 21);
            this.progressBar1.TabIndex = 1;
            // 
            // btn2Excel
            // 
            this.btn2Excel.BackColor = System.Drawing.Color.WhiteSmoke;
            this.btn2Excel.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
            this.btn2Excel.Location = new System.Drawing.Point(10, 361);
            this.btn2Excel.Name = "btn2Excel";
            this.btn2Excel.Size = new System.Drawing.Size(237, 35);
            this.btn2Excel.TabIndex = 2;
            this.btn2Excel.Text = "Export to Excel";
            this.btn2Excel.UseVisualStyleBackColor = false;
            this.btn2Excel.Click += new System.EventHandler(this.btn2Excel_Click);
            // 
            // btnUseTemplate
            // 
            this.btnUseTemplate.BackColor = System.Drawing.Color.WhiteSmoke;
            this.btnUseTemplate.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
            this.btnUseTemplate.Location = new System.Drawing.Point(284, 361);
            this.btnUseTemplate.Name = "btnUseTemplate";
            this.btnUseTemplate.Size = new System.Drawing.Size(237, 35);
            this.btnUseTemplate.TabIndex = 3;
            this.btnUseTemplate.Text = "Use Excel template file";
            this.btnUseTemplate.UseVisualStyleBackColor = false;
            this.btnUseTemplate.Click += new System.EventHandler(this.btnUseTemplate_Click);
            // 
            // label1
            // 
            this.label1.AutoSize = true;
            this.label1.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.label1.Location = new System.Drawing.Point(11, 308);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(56, 13);
            this.label1.TabIndex = 4;
            this.label1.Text = "Progress";
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(732, 449);
            this.Controls.Add(this.label1);
            this.Controls.Add(this.btnUseTemplate);
            this.Controls.Add(this.btn2Excel);
            this.Controls.Add(this.progressBar1);
            this.Controls.Add(this.dg1);
            this.Name = "Form1";
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            this.Text = "Form1";
            this.Load += new System.EventHandler(this.Form1_Load);
            ((System.ComponentModel.ISupportInitialize)(this.dg1)).EndInit();
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.DataGridView dg1;
        private System.Windows.Forms.ProgressBar progressBar1;
        private System.Windows.Forms.Button btn2Excel;
        private System.Windows.Forms.Button btnUseTemplate;
        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.SaveFileDialog saveFileDialog1;
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Software Developer (Senior)
United Kingdom United Kingdom
I currently hold the following qualifications (amongst others, I also studied Music Technology and Electronics, for my sins)

- MSc (Passed with distinctions), in Information Technology for E-Commerce
- BSc Hons (1st class) in Computer Science & Artificial Intelligence

Both of these at Sussex University UK.

Award(s)

I am lucky enough to have won a few awards for Zany Crazy code articles over the years

  • Microsoft C# MVP 2016
  • Codeproject MVP 2016
  • Microsoft C# MVP 2015
  • Codeproject MVP 2015
  • Microsoft C# MVP 2014
  • Codeproject MVP 2014
  • Microsoft C# MVP 2013
  • Codeproject MVP 2013
  • Microsoft C# MVP 2012
  • Codeproject MVP 2012
  • Microsoft C# MVP 2011
  • Codeproject MVP 2011
  • Microsoft C# MVP 2010
  • Codeproject MVP 2010
  • Microsoft C# MVP 2009
  • Codeproject MVP 2009
  • Microsoft C# MVP 2008
  • Codeproject MVP 2008
  • And numerous codeproject awards which you can see over at my blog

Comments and Discussions