公司内部改革,对考勤方面做出调整,要求实现办公自动化,在OA进行上下班考勤;作为程序员,突发奇想如何实现自动化考勤应用?
需求如下:
- 可设置考勤地址、用户信息、上下班时间;
- 根据设置的上下班时间,定时实现自动考勤。
具体实现步骤如下:
- 窗体界面添加考勤地址、用户信息、上班下班时间、WebBrowser控件;
- 将WebBrowser地址更改为考勤地址
- 判断WebBrowser加载完毕后,输入用户、密码信息,点登录进入系统
- 采用线程阻塞形式,预估登入进入系统时间,再进入考勤菜单进行考勤
代码实现如下:
public partial class Form1 : Form { /// <summary> /// 计数器判断网页是否加载完毕 /// </summary> int i = 0; //登入计时器 int j = 0; //上班计时器 int m = 0; public Form1() { InitializeComponent(); txtUrl.Enabled = false; txtUser.Enabled = false; txtPwd.Enabled = false; tdtstartTime.Enabled = false; tdtendTime.Enabled = false; wm = GetWork(); webBrowser.Url = wm.Url; webBrowser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBw_DocumentCompleted); webBrowser.Navigated += new WebBrowserNavigatedEventHandler(webBrowser_Navigated); } /// <summary> /// 用计数器判断界面是否加载完成 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> void webBrowser_Navigated(object sender, WebBrowserNavigatedEventArgs e) { i++; } void webBw_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) { i--; if (0 == i) { if (j < 1) { Lognin(); j++; } if (m < 1 && j > 0) { HtmlDocument htmlDoc = webBrowser.Document; HtmlElement txtMenuCodeQuick = htmlDoc.All["MenuCodeQuick"]; if (txtMenuCodeQuick != null) { txtMenuCodeQuick.Focus(); txtMenuCodeQuick.SetAttribute("value", "HR43B"); SendKeys.SendWait("{ENTER}"); m++; } } } } /// <summary> /// 登录 /// </summary> void Lognin() { HtmlDocument htmlDoc = webBrowser.Document; HtmlElement txtUser = htmlDoc.All["txtUserCodeL"]; txtUser.Focus(); if (txtUser != null) { txtUser.SetAttribute("value", wm.User); } SendKeys.SendWait(Keys.Tab.ToString()); HtmlElement txtPassword = htmlDoc.GetElementById("txtPasswordL"); if (txtPassword != null) { txtPassword.SetAttribute("value", wm.Password); } Thread.Sleep(1000); HtmlElement btnDl = htmlDoc.GetElementById("ctl00$ctl00$c1$CB$btnLogin"); btnDl.InvokeMember("click"); Thread.Sleep(1000); } private Workmanger GetWork() { Uri u = new Uri(txtUrl.Text); wm.Url = u; wm.User = txtUser.Text; wm.Password = txtPwd.Text; wm.StratTime = tdtstartTime.Time; wm.EndTime = tdtendTime.Time; return wm; } //根据指定url地址打开页面 void OpenIE(Uri url) { webBrowser.Url = url; } private Workmanger wm = new Workmanger(); void EndWork() { HtmlDocument htmlDoc = webBrowser.Document; HtmlElement rtbText = htmlDoc.All["ctl00_ctl00_c1_H1_WsdBasicToolBar1"]; StringBuilder sb = new StringBuilder(); foreach (HtmlElement item in htmlDoc.All) { sb.AppendLine(item.Id); } File.WriteAllText(@"d:html.txt", sb.ToString(), Encoding.UTF8); } } /// <summary> /// 考勤系统类 /// </summary> public class Workmanger { /// <summary> /// 地址 /// </summary> public Uri Url { get; set; } /// <summary> /// 用户名 /// </summary> public string User { get; set; } /// <summary> /// 密码 /// </summary> public string Password { get; set; } /// <summary> /// 上班时间 /// </summary> public DateTime? StratTime { get; set; } /// <summary> /// 下班时间 /// </summary> public DateTime? EndTime { get; set; } }