读取Excel任务列表并显示在Outlook日历上
前几天,公司发了一个任务安排,时间不固定,但要求准时到,为了给自己加一个提醒,也为了回顾一下以前的技术,特做了一个Demo。
读取Excel就不多说了,代码很简单,但支持老版本Excel和的版本Excel。
代码如下:
public class ExcelConn { private string FilePath; private string m_filePath = string.Empty; private OleDbConnection conn; public ExcelConn(string filePath) { this.FilePath = filePath; string fileType = System.IO.Path.GetExtension(filePath); if (fileType == ".xls") { conn = new OleDbConnection("Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source ='" + this.FilePath + "';Extended Properties=Excel 8.0;"); } else { conn = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source ='" + this.FilePath + "';Extended Properties=\"Excel 12.0;HDR=YES;IMEX=1\""); } if (conn.State != System.Data.ConnectionState.Open) { conn.Open(); } } private DataTable GetData(OleDbCommand cmd) { try { if (cmd.Connection != null) { using (DataSet ds = new DataSet()) { using (OleDbDataAdapter da = new OleDbDataAdapter()) { da.SelectCommand = cmd; da.Fill(ds); return ds.Tables[0]; } } } else { using (OleDbTransaction trans = conn.BeginTransaction(IsolationLevel.ReadUncommitted)) { try { cmd.Transaction = trans; using (DataSet ds = new DataSet()) { using (OleDbDataAdapter da = new OleDbDataAdapter()) { da.SelectCommand = cmd; da.SelectCommand.Connection = conn; da.Fill(ds); return ds.Tables[0]; } } } finally { trans.Commit(); } } } } finally { } } public DataSet GetDataSet(string sql) { try { OleDbTransaction trans = conn.BeginTransaction(IsolationLevel.ReadUncommitted); try { using (OleDbCommand cmd = conn.CreateCommand()) { cmd.Transaction = trans; cmd.CommandType = CommandType.Text; cmd.CommandText = sql; using (DataSet ds = new DataSet()) { using (OleDbDataAdapter da = new OleDbDataAdapter()) { da.SelectCommand = cmd; da.SelectCommand.Connection = conn; da.Fill(ds); return ds; } } } } finally { trans.Commit(); } } finally { } } public void ExecuteNonQuery(string sql) { OleDbTransaction trans = conn.BeginTransaction(IsolationLevel.ReadUncommitted); OleDbCommand cmd = new OleDbCommand(sql); cmd.Connection = conn; cmd.Transaction = trans; cmd.ExecuteNonQuery(); trans.Commit(); } public object ExecuteScalar(OleDbCommand cmd) { try { using (OleDbTransaction trans = conn.BeginTransaction(IsolationLevel.ReadUncommitted)) { cmd.Connection = conn; cmd.Transaction = trans; object res = cmd.ExecuteScalar(); trans.Commit(); return res; } } finally { } } public void Close() { conn.Close(); } public DataSet ExcelToDS() { OleDbDataAdapter myCommand; DataTable schemaTable = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { }); DataSet ds = new DataSet(); try { int i = 0; { DataRow dr = schemaTable.Rows[i]; string strExcel = "select * from [" + dr["TABLE_NAME"].ToString().Trim() + "]"; myCommand = new OleDbDataAdapter(strExcel, conn); myCommand.Fill(ds); } } catch { } finally { conn.Close(); } return ds; } }
调用示例:DataTable dtExl = new Calendar.ExcelConn(@"d:\桌面\Temp\net开发团队生产突击报名表(1).xlsx").ExcelToDS().Tables[0];
在OutLook上添加日历,其实就是添加一个约会。
新建约会也就是调用OutLook的ApplicationClass。
主要代码如下:
ApplicationClass oApp = new Microsoft.Office.Interop.Outlook.ApplicationClass(); //会议是约会的一种 AppointmentItem oItem = (AppointmentItem)oApp.CreateItem(OlItemType.olAppointmentItem); oItem.MeetingStatus = OlMeetingStatus.olMeeting; oItem.Subject = "生产突击"; oItem.Body = "内容"; oItem.Location = "地点"; //开始时间 oItem.Start = DateTime.Now.AddDays(1); //结束时间 oItem.End = DateTime.Now.AddDays(1).AddHours(4); //提醒设置 oItem.ReminderSet = true; oItem.ReminderMinutesBeforeStart = 5; //是否全天事件 oItem.AllDayEvent = false; oItem.BusyStatus = OlBusyStatus.olBusy; //索引从1开始,而不是从0 //发件人的帐号信息 var acc = oApp.Session.Accounts; oItem.SendUsingAccount = oApp.Session.Accounts[1]; //添加必选人 Recipient force = oItem.Recipients.Add("wufei@china.com"); force = oItem.Recipients.Add("zaijun@china.com"); force.Type = (int)OlMeetingRecipientType.olRequired; ////添加可选人 //Recipient opt = oItem.Recipients.Add("mailuser3@p.mailserver.com"); //opt.Type = (int)OlMeetingRecipientType.olOptional; ////添加会议发起者 //Recipient meetingSender = oItem.Recipients.Add("mailuser1@mailserver.com"); //meetingSender.Type = (int)OlMeetingRecipientType.olOrganizer; oItem.Recipients.ResolveAll(); oItem.Send();
既读取了Excel,又能发约会,下面就是Excel中数据的筛选和调用了,相信难不倒各位,就不多说了。
至此,这个简单的Demo算是完成了,希望对大家有用。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· [AI/GPT/综述] AI Agent的设计模式综述