⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 form1.cs

📁 C#写的导入Excel表到数据库
💻 CS
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.OleDb;
using System.Data .SqlClient;
namespace InExcel
{
	/// <summary>
	/// Form1 的摘要说明。
	/// </summary>
	public class Form1 : System.Windows.Forms.Form
	{
		private System.Windows.Forms.TextBox textBox1;
		private System.Windows.Forms.ComboBox comboBox1;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.Button button1;
		private System.Windows.Forms.Button button2;
		private System.Windows.Forms.OpenFileDialog openFileDialog1;
		private System.Windows.Forms.DataGrid dataGrid1;

		private string strConnString;
		private SqlConnection connConnection;
		private System.Windows.Forms.TextBox textBox2;

		/// <summary>
		/// 必需的设计器变量。
		/// </summary>
		private System.ComponentModel.Container components = null;

		public Form1()
		{
			//
			// Windows 窗体设计器支持所必需的
			//
			InitializeComponent();

			//
			// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
			//
		}

		/// <summary>
		/// 清理所有正在使用的资源。
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows 窗体设计器生成的代码
		/// <summary>
		/// 设计器支持所需的方法 - 不要使用代码编辑器修改
		/// 此方法的内容。
		/// </summary>
		private void InitializeComponent()
		{
			this.textBox1 = new System.Windows.Forms.TextBox();
			this.comboBox1 = new System.Windows.Forms.ComboBox();
			this.label1 = new System.Windows.Forms.Label();
			this.label2 = new System.Windows.Forms.Label();
			this.button1 = new System.Windows.Forms.Button();
			this.button2 = new System.Windows.Forms.Button();
			this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
			this.dataGrid1 = new System.Windows.Forms.DataGrid();
			this.textBox2 = new System.Windows.Forms.TextBox();
			((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
			this.SuspendLayout();
			// 
			// textBox1
			// 
			this.textBox1.Location = new System.Drawing.Point(16, 32);
			this.textBox1.Name = "textBox1";
			this.textBox1.TabIndex = 0;
			this.textBox1.Text = "";
			// 
			// comboBox1
			// 
			this.comboBox1.Location = new System.Drawing.Point(214, 32);
			this.comboBox1.Name = "comboBox1";
			this.comboBox1.Size = new System.Drawing.Size(121, 20);
			this.comboBox1.TabIndex = 1;
			this.comboBox1.Enter += new System.EventHandler(this.comboBox1_Enter);
			// 
			// label1
			// 
			this.label1.AutoSize = true;
			this.label1.Location = new System.Drawing.Point(216, 8);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(66, 17);
			this.label1.TabIndex = 2;
			this.label1.Text = "选择工作簿";
			// 
			// label2
			// 
			this.label2.AutoSize = true;
			this.label2.Location = new System.Drawing.Point(16, 8);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(85, 17);
			this.label2.TabIndex = 3;
			this.label2.Text = "选择Excel文件";
			// 
			// button1
			// 
			this.button1.Location = new System.Drawing.Point(133, 32);
			this.button1.Name = "button1";
			this.button1.Size = new System.Drawing.Size(64, 24);
			this.button1.TabIndex = 4;
			this.button1.Text = "选择";
			this.button1.Click += new System.EventHandler(this.button1_Click);
			// 
			// button2
			// 
			this.button2.Location = new System.Drawing.Point(352, 32);
			this.button2.Name = "button2";
			this.button2.Size = new System.Drawing.Size(72, 24);
			this.button2.TabIndex = 5;
			this.button2.Text = "导入";
			this.button2.Click += new System.EventHandler(this.button2_Click);
			// 
			// dataGrid1
			// 
			this.dataGrid1.DataMember = "";
			this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
			this.dataGrid1.Location = new System.Drawing.Point(16, 64);
			this.dataGrid1.Name = "dataGrid1";
			this.dataGrid1.Size = new System.Drawing.Size(408, 200);
			this.dataGrid1.TabIndex = 6;
			// 
			// textBox2
			// 
			this.textBox2.Location = new System.Drawing.Point(16, 272);
			this.textBox2.Name = "textBox2";
			this.textBox2.Size = new System.Drawing.Size(400, 21);
			this.textBox2.TabIndex = 8;
			this.textBox2.Text = "textBox2";
			// 
			// Form1
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
			this.ClientSize = new System.Drawing.Size(432, 310);
			this.Controls.Add(this.textBox2);
			this.Controls.Add(this.dataGrid1);
			this.Controls.Add(this.button2);
			this.Controls.Add(this.button1);
			this.Controls.Add(this.label2);
			this.Controls.Add(this.label1);
			this.Controls.Add(this.comboBox1);
			this.Controls.Add(this.textBox1);
			this.Name = "Form1";
			this.Text = "Form1";
			((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// 应用程序的主入口点。
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new Form1());
		}

		private void button2_Click(object sender, System.EventArgs e)
		{
			string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + this.textBox1.Text + ";" + "Extended Properties=Excel 8.0;";
			OleDbConnection conn = new OleDbConnection(strConn);
			conn.Open();
			string strExcel = "";
			OleDbDataAdapter myCommand = null;
			DataSet ds = null;
			strExcel = "select * from [" + this.comboBox1.Text + "]";
			myCommand = new OleDbDataAdapter(strExcel, strConn);
			ds = new DataSet();
			myCommand.Fill(ds, "table1");

			this.dataGrid1.DataSource = ds.Tables[0];

			string U8_id,part_id,T_name_new,sql;
			double T_weight_new,T_weight;
			int TT_TL;
			
			for(int i=0;i<ds.Tables[0].Rows.Count;i++)
			{
				U8_id=ds.Tables[0].Rows[i][0].ToString()==" "?"":ds.Tables[0].Rows[i][0].ToString();
				part_id=ds.Tables[0].Rows[i][1].ToString();
				T_name_new=ds.Tables[0].Rows[i][5].ToString()==" "?"":ds.Tables[0].Rows[i][5].ToString();
				T_weight=double.Parse(ds.Tables[0].Rows[i][19].ToString()==""?"0":ds.Tables[0].Rows[i][19].ToString());
				T_weight_new=double.Parse(ds.Tables[0].Rows[i][20].ToString()==""?"0":ds.Tables[0].Rows[i][20].ToString());
				TT_TL=int.Parse(ds.Tables[0].Rows[i][3].ToString()==""?"0":ds.Tables[0].Rows[i][3].ToString());
				sql="update apart set U8_id='"+U8_id+"', TT_TL="+TT_TL+", T_name_new='"+T_name_new+"',T_weight="+T_weight+", T_weight_new="+T_weight_new+" where Part_id="+part_id;
				this.textBox2.Text=sql;
				update(sql);
			}
			conn.Close();
		}

		public void Open()
		{
			strConnString = "database =xgltnew;server=.;pwd =mdsoft;uid =sa";     
			try
			{
				connConnection = new SqlConnection(strConnString);
				connConnection.Open();
			}
			catch ( Exception e )
			{
				MessageBox.Show ( "连接数据库发生错误为:" + e.ToString ( ) , "错误!" ) ;
			}

		}

		public void update(string sql)
		{
			this.Open();
			DataSet myDataSet = new DataSet();
			try
			{
				SqlCommand cmd = new SqlCommand(sql, connConnection);
				cmd.CommandTimeout = 30;

				int i=cmd.ExecuteNonQuery();
			}
			catch (Exception e)
			{
				MessageBox.Show("连接数据库发生错误为:" + e.ToString(), "错误!");
			}
			this.dateClose();
		}

		public void dateClose()
		{
			connConnection.Close();
		}

		private void button1_Click(object sender, System.EventArgs e)
		{
			openFileDialog1.Filter = "*.xls|*.xls";
			if (this.openFileDialog1.ShowDialog() == DialogResult.OK)
			{
				this.textBox1.Text = openFileDialog1.FileName;
			}	
		}

		private void comboBox1_Enter(object sender, System.EventArgs e)
		{
			this.comboBox1.Items.Clear();


			OleDbConnection objConn = null;
			DataTable dt = null;

			try
			{
				string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + this.textBox1.Text + ";" + "Extended Properties=Excel 8.0;";

				objConn = new OleDbConnection(strConn);
				objConn.Open();
				dt = objConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);

				if (dt != null)
				{
					string[] sheetName = new string[dt.Rows.Count];
					int i = 0;
					foreach (DataRow row in dt.Rows)
					{
						sheetName[i] = row["TABLE_NAME"].ToString();
						this.comboBox1.Items.Add(sheetName[i].ToString());

						i++;
					}
				}
			}

			catch
			{
				MessageBox.Show("错误");

			}
		}
	}
}

⌨️ 快捷键说明

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