前幾天,公司發了一個任務安排,時間不固定,但要求準時到,爲了給本身加一個提醒,也爲了回顧一下之前的技術,特作了一個Demo。sql
讀取Excel就很少說了,代碼很簡單,但支持老版本Excel和的版本Excel。ide
代碼以下:ui
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];this
在OutLook上添加日曆,其實就是添加一個約會。spa
新建約會也就是調用OutLook的ApplicationClass。code
主要代碼以下:server
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中數據的篩選和調用了,相信難不倒各位,就很少說了。blog
至此,這個簡單的Demo算是完成了,但願對你們有用。索引