关于程序的一些想法:
单位的全部生日名单保存在excel里,本来我想用config配置文件管理,直接调出。但实际操作的时候出现了问题:操作config需要知道key才可以获得value,换句话说我在知道今天谁生日之前要先知道寿星的名字,这是不符合逻辑的。
最后考虑到用sql语言操作excel表格来完成,但是日期并不好操作,上网搜找到一个脑洞巨大的方法:dayofyear,简直神器,接下来就简单了,在excel新建一列dayofyear,用oledb连接excel,再添加一个timer控件定时刷新sql语言找出寿星,显示在datagridview里。
遇到的问题点:
本来的excel驱动版本
MsWord.Document wordDoc = wordApp.Documents.Add(@"E:\治安要情模板.doc");
vs报错外部表不是预期的格式,问题出在excel2010版不被JET引擎支持(03-07),所以要改为
string strconn="Provider=Microsoft.ACE.OLEDB.12.0;Data Source="+filepath+";Extended Properties='Excel 8.0;HDR=YES;IMEX=1'" ;
就可以顺利进入了
让程序不在下方任务栏显示:
this.ShowInTaskbar = false;
程序要在通知栏显示一个icon,双击弹出,再双击隐藏:
在form设计界面添加一个notifyicon控件并设置图标(必须)
添加notifyicon方法:
private void notifyIcon1_DoubleClick(object sender, EventArgs e)
{
if (this.WindowState == FormWindowState.Normal)
{
this.WindowState = FormWindowState.Minimized;
this.Hide();
}
else if (this.WindowState == FormWindowState.Minimized)
{
this.Show();
this.WindowState = FormWindowState.Normal;
this.Activate();
}
}
开机自启动:
1把exe文件添加到开始菜单启动文件夹内
2注册表启动
// 添加到 当前登陆用户的 注册表启动项
RegistryKey RKey = Registry.CurrentUser.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run");
RKey.SetValue("AppName", @"C:\AppName.exe");
// 添加到 所有用户的 注册表启动项
RegistryKey RKey = Registry.LocalMachine.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run");
RKey.SetValue("AppName", @"C:\AppName.exe");
完整代码
using System;
using System.Data;
using System.Windows.Forms;
using System.Data.OleDb;//引用oledb
namespace birthdaylist
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
timer1.Enabled = true;
}
private void button1_Click(object sender, EventArgs e)
{
string filepath = "E:\\生日慰问一览表(放在e盘根目录下).xls";
string strconn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filepath + ";Extended Properties='Excel 8.0;HDR=YES;IMEX=1'";
//oledb打开excel的操作
OleDbConnection oleconn = new OleDbConnection(strconn);
oleconn.Open();
string sql = textBox1.Text;
OleDbDataAdapter oledaexcel = new OleDbDataAdapter(sql, oleconn);
DataSet oledsexcel = new DataSet();//新建dataset对象
oledaexcel.Fill(oledsexcel, "Sheet1");//映射到ds里
oleconn.Close();
this.dataGridView1.DataSource = oledsexcel.Tables[0];//dgv数据源来自ds.table0
}
private void textBox1_KeyDown(object sender, KeyEventArgs e)//回车等于查询按钮
{
if (e.KeyCode == Keys.Enter)
button1_Click(sender,e);
}
private void timer1_Tick(object sender, EventArgs e)
{
label1.Text = DateTime.Now.DayOfYear.ToString();
int today = DateTime.Now.DayOfYear;
timer1.Interval = 3600 * 1000;//周期1小时一次
//string tmp = DateTime.Now.ToShortTimeString();
if (DateTime.Now.Hour.ToString() == "8")//8点钟提示
{
int yesterday = today - 1;
textBox1.Text = "select * from [Sheet1$] where xh='" + yesterday + "' ";
button1_Click(sender, e);
}
}
private void button2_Click(object sender, EventArgs e)//停止启动timer
{
if (timer1.Enabled == true)
timer1.Enabled = false;
else
timer1.Enabled = true;
}
private void Form1_Load(object sender, EventArgs e)
{
this.ShowInTaskbar = false;
}
private void notifyIcon1_DoubleClick(object sender, EventArgs e)
{
if (this.WindowState == FormWindowState.Normal)
{
this.WindowState = FormWindowState.Minimized;
this.Hide();
}
else if (this.WindowState == FormWindowState.Minimized)
{
this.Show();
this.WindowState = FormWindowState.Normal;
this.Activate();
}
}
}
}