form1.cs

来自「Sams Teach Yourself C# Web Programming i」· CS 代码 · 共 130 行

CS
130
字号
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Reflection;

namespace Automate_Excel
{
	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	public class clsMain : System.Windows.Forms.Form
	{
		private System.Windows.Forms.Button btnAutomateExcel;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

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

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

		/// <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.btnAutomateExcel = new System.Windows.Forms.Button();
			this.SuspendLayout();
			// 
			// btnAutomateExcel
			// 
			this.btnAutomateExcel.Location = new System.Drawing.Point(96, 128);
			this.btnAutomateExcel.Name = "btnAutomateExcel";
			this.btnAutomateExcel.Size = new System.Drawing.Size(104, 23);
			this.btnAutomateExcel.TabIndex = 0;
			this.btnAutomateExcel.Text = "Automate Excel";
			this.btnAutomateExcel.Click += new System.EventHandler(this.btnAutomateExcel_Click);
			// 
			// clsMain
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(292, 266);
			this.Controls.AddRange(new System.Windows.Forms.Control[] {
																		  this.btnAutomateExcel});
			this.Name = "clsMain";
			this.Text = "Automate Excel";
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new clsMain());
		}

		private void btnAutomateExcel_Click(object sender, System.EventArgs e)
		{
			
			// Instantiate Excel and start a new workbook.
			Excel.Application objExcel = new Excel.Application();
			
			objExcel.Visible = true;

			//start a new workbook and a worksheet.
			Excel._Workbook objBook = objExcel.Workbooks.Add(Missing.Value);
			Excel._Worksheet objSheet =    (Excel._Worksheet)objBook.Worksheets.get_Item(1);
					
			Excel.Range range;

			range = objSheet.get_Range("A1", Missing.Value);

			//This code works with Excel 9. In order for it to work
			//with Excel XP, you'll need to modify the code as explained
			//in the text.
			range.Value ="75";

			range = objSheet.get_Range("B1", Missing.Value);
			range.Value= "125" ;
		
			range = objSheet.get_Range("C1", Missing.Value);
			range.Value = "255";
			range = objSheet.get_Range("D1", Missing.Value);
			range.Value = "295";

			range = objSheet.get_Range("E1", Missing.Value);
			range.Value = "=SUM(RC[-4]:RC[-1])";
			
			range = objSheet.get_Range("A1", "E1");
			range.Font.Bold=true;
			
			objExcel=null;

		}
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?