通用的自动提交From表单

没事写了个通用的:

 

   只需要改变Execl里面的内容就行!

 

源码:

Form1.resx:

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.IO; using System.Collections; using System.Data.OleDb; namespace automatic { public partial class Form1 : Form { private ArrayList arrText; private int falg = 0;//第一次0 开始前1 开始后2 public Form1() { InitializeComponent(); } /// <summary> /// 选择文件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnSelect_Click(object sender, EventArgs e) { this.openFile.ShowDialog(); } private void openFile_FileOk(object sender, CancelEventArgs e) { this.txtPath.Text = this.openFile.FileName.ToString(); } private void btnStart_Click(object sender, EventArgs e) { falg = 1; this.webBro.Navigate(this.txtRegAdd.Text); } private void webBro_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) { if (falg != 0) { foreach (DataRow dr in ExcelToDataTable(this.excelTable.Text, this.txtPath.Text).Rows) { //类型是text的 if (dr[2].ToString() == "text") { this.webBro.Document.All[dr[0].ToString()].SetAttribute("value", dr[1].ToString()); } //类型是select的 if (dr[2].ToString() == "select") { //this.webBro.Document.All[dr[0].ToString()].SetAttribute("value", dr[1].ToString()); for (int i = 0; i < this.webBro.Document.All[dr[0].ToString()].Children.Count; i++) { if (this.webBro.Document.All[dr[0].ToString()].Children[i].GetAttribute("text").ToString() == dr[1].ToString()) { this.webBro.Document.All[dr[0].ToString()].Children[i].SetAttribute("selected", "true"); } } } //类型是checkbox的 if (dr[2].ToString() == "checkbox") { this.webBro.Document.All[dr[0].ToString()].SetAttribute("checked", "true"); } //类型是radio的 if (dr[2].ToString() == "radio") { for (int i = 0; i < this.webBro.Document.GetElementsByTagName("Input").GetElementsByName(dr[0].ToString()).Count; i++) { if (this.webBro.Document.GetElementsByTagName("Input").GetElementsByName(dr[0].ToString())[i].GetAttribute("value").ToString() == dr[1].ToString()) { this.webBro.Document.GetElementsByTagName("Input").GetElementsByName(dr[0].ToString())[i].SetAttribute("checked", "true"); } } } if (dr[2].ToString() == "submit") { if (falg == 1) { this.webBro.Document.All[dr[0].ToString()].InvokeMember("click"); falg++; } } } } } //Excel(注释:A是Name B是value值 C是类型) /// <summary> /// 查询出来Excel里面的值返回一个DataTable /// </summary> /// <param name="tableName">Excel的Table 例如:Sheet1</param> /// <param name="path">Excel的路径</param> /// <returns></returns> public DataTable ExcelToDataTable(string tableName, string path) { string connString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + path + ";Extended Properties=/"Excel 8.0;HDR=NO;IMEX=1/""; ; OleDbConnection connection = new OleDbConnection(connString); string sql_select_commands = "Select * from [" + tableName + "$]"; OleDbDataAdapter adp = new OleDbDataAdapter(sql_select_commands, connection); DataSet ds = new DataSet(); try { adp.Fill(ds, tableName); return ds.Tables[0]; } catch { return null; } } } }

 

 

Form1.Designer.cs:

 

namespace automatic { partial class Form1 { /// <summary> /// 必需的设计器变量。 /// </summary> private System.ComponentModel.IContainer components = null; /// <summary> /// 清理所有正在使用的资源。 /// </summary> /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param> protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Windows 窗体设计器生成的代码 /// <summary> /// 设计器支持所需的方法 - 不要 /// 使用代码编辑器修改此方法的内容。 /// </summary> private void InitializeComponent() { this.txtPath = new System.Windows.Forms.TextBox(); this.btnStart = new System.Windows.Forms.Button(); this.openFile = new System.Windows.Forms.OpenFileDialog(); this.btnSelect = new System.Windows.Forms.Button(); this.label1 = new System.Windows.Forms.Label(); this.txtRegAdd = new System.Windows.Forms.TextBox(); this.label2 = new System.Windows.Forms.Label(); this.webBro = new System.Windows.Forms.WebBrowser(); this.label3 = new System.Windows.Forms.Label(); this.excelTable = new System.Windows.Forms.TextBox(); this.SuspendLayout(); // // txtPath // this.txtPath.Location = new System.Drawing.Point(104, 54); this.txtPath.Name = "txtPath"; this.txtPath.ReadOnly = true; this.txtPath.Size = new System.Drawing.Size(257, 21); this.txtPath.TabIndex = 4; this.txtPath.Text = "C://Documents and Settings//Administrator//桌面//1.xls"; // // btnStart // this.btnStart.Location = new System.Drawing.Point(474, 52); this.btnStart.Name = "btnStart"; this.btnStart.Size = new System.Drawing.Size(75, 23); this.btnStart.TabIndex = 2; this.btnStart.Text = "开始"; this.btnStart.UseVisualStyleBackColor = true; this.btnStart.Click += new System.EventHandler(this.btnStart_Click); // // openFile // this.openFile.FileName = "openFileDialog1"; this.openFile.FileOk += new System.ComponentModel.CancelEventHandler(this.openFile_FileOk); // // btnSelect // this.btnSelect.Location = new System.Drawing.Point(381, 52); this.btnSelect.Name = "btnSelect"; this.btnSelect.Size = new System.Drawing.Size(75, 23); this.btnSelect.TabIndex = 3; this.btnSelect.Text = "选择文件"; this.btnSelect.UseVisualStyleBackColor = true; this.btnSelect.Click += new System.EventHandler(this.btnSelect_Click); // // label1 // this.label1.AutoSize = true; this.label1.Location = new System.Drawing.Point(18, 12); this.label1.Name = "label1"; this.label1.Size = new System.Drawing.Size(53, 12); this.label1.TabIndex = 6; this.label1.Text = "网页地址"; // // txtRegAdd // this.txtRegAdd.Location = new System.Drawing.Point(104, 9); this.txtRegAdd.Name = "txtRegAdd"; this.txtRegAdd.Size = new System.Drawing.Size(257, 21); this.txtRegAdd.TabIndex = 5; this.txtRegAdd.Text = "http://www.jmweb.com/business/index.asp"; // // label2 // this.label2.AutoSize = true; this.label2.Location = new System.Drawing.Point(18, 57); this.label2.Name = "label2"; this.label2.Size = new System.Drawing.Size(53, 12); this.label2.TabIndex = 7; this.label2.Text = "文件地址"; // // webBro // this.webBro.Location = new System.Drawing.Point(20, 81); this.webBro.MinimumSize = new System.Drawing.Size(20, 20); this.webBro.Name = "webBro"; this.webBro.Size = new System.Drawing.Size(626, 364); this.webBro.TabIndex = 8; this.webBro.Url = new System.Uri("", System.UriKind.Relative); this.webBro.DocumentCompleted += new System.Windows.Forms.WebBrowserDocumentCompletedEventHandler(this.webBro_DocumentCompleted); // // label3 // this.label3.AutoSize = true; this.label3.Location = new System.Drawing.Point(379, 12); this.label3.Name = "label3"; this.label3.Size = new System.Drawing.Size(77, 12); this.label3.TabIndex = 10; this.label3.Text = "Excel的Table"; // // excelTable // this.excelTable.Location = new System.Drawing.Point(474, 9); this.excelTable.Name = "excelTable"; this.excelTable.Size = new System.Drawing.Size(109, 21); this.excelTable.TabIndex = 9; this.excelTable.Text = "Sheet1"; // // Form1 // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(663, 481); this.Controls.Add(this.label3); this.Controls.Add(this.excelTable); this.Controls.Add(this.webBro); this.Controls.Add(this.label2); this.Controls.Add(this.label1); this.Controls.Add(this.txtRegAdd); this.Controls.Add(this.btnSelect); this.Controls.Add(this.btnStart); this.Controls.Add(this.txtPath); this.Name = "Form1"; this.Text = "Form1"; this.ResumeLayout(false); this.PerformLayout(); } #endregion private System.Windows.Forms.TextBox txtPath; private System.Windows.Forms.Button btnStart; private System.Windows.Forms.OpenFileDialog openFile; private System.Windows.Forms.Button btnSelect; private System.Windows.Forms.Label label1; private System.Windows.Forms.TextBox txtRegAdd; private System.Windows.Forms.Label label2; private System.Windows.Forms.WebBrowser webBro; private System.Windows.Forms.Label label3; private System.Windows.Forms.TextBox excelTable; } }

 

 

简单说明:

 

 百度收索

1.新建一个Execl

  在A列写入wd(wd是百度首页的输入框name ,你可以查看源码找到)
  B列写入你要收索的内容
  C列写入类型(就是input里面的type类型)text;
  以此类推
  最后写入submit
  a列是提交按钮的名字b列为空 c列是类型submit

这样就可以

   只要是页面表单都可以这样用

2.运行程序

   在Excel的Table里面写入Excel(例如:Sheet1)
  选择刚才新建的Excel  点击开始  OK

 

 

 

 

效果如下:

 

通用的自动提交From表单_第1张图片

你可能感兴趣的:(String,object,Excel,input,dataset,Components)