form1.cs

来自「用于文件传输的Web服务客户端」· CS 代码 · 共 132 行

CS
132
字号
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data.OleDb;
using System.Data;
namespace E3.Client
{
	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	public class Form1 : System.Windows.Forms.Form
	{
		private System.Windows.Forms.DataGrid dataGrid1;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public Form1()
		{
			//
			// 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 );
		}
		private void TestImportExcel()
		{
			//			String sConnectionString = "Provider=SQLOLEDB;Data Source=10.7.1.30;Initial Catalog=Data0527;User ID=sa;Password=daniel;";

			// Create connection object by using the preceding connection string.
			
			String sConnectionString =@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=E:\测试\JgxxData\JGGX_11_20040331.xls;Extended Properties=Excel 8.0;";
			
			OleDbConnection objConn = new OleDbConnection(sConnectionString);

			// Open connection with the database.
			objConn.Open();

			// The code to follow uses a SQL SELECT command to display the data from the worksheet.

			//			// Create new OleDbCommand to return data from worksheet.
			//			string StrSql="select * from "	+ "OpenDataSource('Microsoft.Jet.OLEDB.4.0'" +
			//					@",'Excel 8.0;HDR=YES;DATABASE=E:\测试\JgxxData\JGGX_11_20040331.xls')...[Sheet1$]"; 

			string StrSql="select * from DataTable2";
			OleDbCommand objCmdSelect =new OleDbCommand(StrSql, objConn);

			// Create new OleDbDataAdapter that is used to build a DataSet
			// based on the preceding SQL SELECT statement.
			OleDbDataAdapter objAdapter1 = new OleDbDataAdapter();

			// Pass the Select command to the adapter.
			objAdapter1.SelectCommand = objCmdSelect;

			// Create new DataSet to hold information from the worksheet.
			DataSet objDataset1 = new DataSet();

			// Fill the DataSet with the information from the worksheet.
			objAdapter1.Fill(objDataset1, "XLData");

			// Bind data to DataGrid control.
			//			DataGrid1.DataSource = objDataset1.Tables[0].DefaultView;
			//			DataGrid1.DataBind();

			// Clean up objects.
			objConn.Close();
			dataGrid1.DataSource=objDataset1;

		}
		
		#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.dataGrid1 = new System.Windows.Forms.DataGrid();
			((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
			this.SuspendLayout();
			// 
			// dataGrid1
			// 
			this.dataGrid1.DataMember = "";
			this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
			this.dataGrid1.Location = new System.Drawing.Point(32, 32);
			this.dataGrid1.Name = "dataGrid1";
			this.dataGrid1.Size = new System.Drawing.Size(424, 272);
			this.dataGrid1.TabIndex = 0;
			// 
			// Form1
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(464, 326);
			this.Controls.Add(this.dataGrid1);
			this.Name = "Form1";
			this.Text = "Form1";
			this.Load += new System.EventHandler(this.Form1_Load);
			((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
			this.ResumeLayout(false);

		}
		#endregion

		private void Form1_Load(object sender, System.EventArgs e)
		{
			this.TestImportExcel();
		}
	}
}

⌨️ 快捷键说明

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