Click here to Skip to main content
13,191,400 members (75,975 online)
Click here to Skip to main content

Stats

315.2K views
8.5K downloads
108 bookmarked
Posted 15 Apr 2007
MIT

DataTable to Excel

, 15 Apr 2007
A quick and easy way to format and export a DataTable to an Excel spreadsheet
namespace ExcelTest
{
	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.components = new System.ComponentModel.Container();
			this.dataGridView1 = new System.Windows.Forms.DataGridView();
			this.statsBindingSource = new System.Windows.Forms.BindingSource(this.components);
			this.button1 = new System.Windows.Forms.Button();
			this.textBox1 = new System.Windows.Forms.TextBox();
			this.usernameDataGridViewTextBoxColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
			this.logincountDataGridViewTextBoxColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
			this.favoritecolorDataGridViewTextBoxColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
			this.dataBindingSource = new System.Windows.Forms.BindingSource(this.components);
			this.data = new ExcelTest.Data();
			((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.statsBindingSource)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.dataBindingSource)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.data)).BeginInit();
			this.SuspendLayout();
			// 
			// dataGridView1
			// 
			this.dataGridView1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
						| System.Windows.Forms.AnchorStyles.Left)
						| System.Windows.Forms.AnchorStyles.Right)));
			this.dataGridView1.AutoGenerateColumns = false;
			this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
			this.dataGridView1.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
            this.usernameDataGridViewTextBoxColumn,
            this.logincountDataGridViewTextBoxColumn,
            this.favoritecolorDataGridViewTextBoxColumn});
			this.dataGridView1.DataSource = this.statsBindingSource;
			this.dataGridView1.Location = new System.Drawing.Point(12, 12);
			this.dataGridView1.Name = "dataGridView1";
			this.dataGridView1.Size = new System.Drawing.Size(405, 277);
			this.dataGridView1.TabIndex = 0;
			// 
			// statsBindingSource
			// 
			this.statsBindingSource.DataMember = "Stats";
			this.statsBindingSource.DataSource = this.dataBindingSource;
			// 
			// button1
			// 
			this.button1.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
			this.button1.Location = new System.Drawing.Point(342, 295);
			this.button1.Name = "button1";
			this.button1.Size = new System.Drawing.Size(75, 23);
			this.button1.TabIndex = 1;
			this.button1.Text = "Write Excel";
			this.button1.UseVisualStyleBackColor = true;
			this.button1.Click += new System.EventHandler(this.button1_Click);
			// 
			// textBox1
			// 
			this.textBox1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)
						| System.Windows.Forms.AnchorStyles.Right)));
			this.textBox1.Location = new System.Drawing.Point(12, 297);
			this.textBox1.Name = "textBox1";
			this.textBox1.Size = new System.Drawing.Size(324, 20);
			this.textBox1.TabIndex = 2;
			// 
			// usernameDataGridViewTextBoxColumn
			// 
			this.usernameDataGridViewTextBoxColumn.DataPropertyName = "username";
			this.usernameDataGridViewTextBoxColumn.HeaderText = "username";
			this.usernameDataGridViewTextBoxColumn.Name = "usernameDataGridViewTextBoxColumn";
			// 
			// logincountDataGridViewTextBoxColumn
			// 
			this.logincountDataGridViewTextBoxColumn.DataPropertyName = "login_count";
			this.logincountDataGridViewTextBoxColumn.HeaderText = "login_count";
			this.logincountDataGridViewTextBoxColumn.Name = "logincountDataGridViewTextBoxColumn";
			// 
			// favoritecolorDataGridViewTextBoxColumn
			// 
			this.favoritecolorDataGridViewTextBoxColumn.DataPropertyName = "favorite_color";
			this.favoritecolorDataGridViewTextBoxColumn.HeaderText = "favorite_color";
			this.favoritecolorDataGridViewTextBoxColumn.Name = "favoritecolorDataGridViewTextBoxColumn";
			// 
			// dataBindingSource
			// 
			this.dataBindingSource.DataSource = this.data;
			this.dataBindingSource.Position = 0;
			// 
			// data
			// 
			this.data.DataSetName = "Data";
			this.data.SchemaSerializationMode = System.Data.SchemaSerializationMode.IncludeSchema;
			// 
			// Form1
			// 
			this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
			this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
			this.ClientSize = new System.Drawing.Size(429, 330);
			this.Controls.Add(this.textBox1);
			this.Controls.Add(this.button1);
			this.Controls.Add(this.dataGridView1);
			this.Name = "Form1";
			this.Text = "Form1";
			((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.statsBindingSource)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.dataBindingSource)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.data)).EndInit();
			this.ResumeLayout(false);
			this.PerformLayout();

		}

		#endregion

		private System.Windows.Forms.DataGridView dataGridView1;
		private System.Windows.Forms.DataGridViewTextBoxColumn usernameDataGridViewTextBoxColumn;
		private System.Windows.Forms.DataGridViewTextBoxColumn logincountDataGridViewTextBoxColumn;
		private System.Windows.Forms.DataGridViewTextBoxColumn favoritecolorDataGridViewTextBoxColumn;
		private System.Windows.Forms.BindingSource statsBindingSource;
		private System.Windows.Forms.BindingSource dataBindingSource;
		private Data data;
		private System.Windows.Forms.Button button1;
		private System.Windows.Forms.TextBox textBox1;
	}
}

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, along with any associated source code and files, is licensed under The MIT License

Share

About the Author

Jacob Klint
Software Developer
United States United States
No Biography provided

You may also be interested in...

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.171017.1 | Last Updated 15 Apr 2007
Article Copyright 2007 by Jacob Klint
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid