Read the Excel task list and displayed on the Outlook calendar

A few days ago, the company made a scheduled task, time is not fixed, but the requirements on time, in order to give yourself a reminder, and to look back at the previous technology, made a special Demo.

Read Excel is not to say, the code is very simple, but to support older versions of Excel and the version of Excel.

code show as below:

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;
        }
    }

Call Example: DataTable dtExl = new Calendar.ExcelConn (@ "d: \ Desktop \ Temp \ net production assault team registration form (1) .xlsx") ExcelToDS () Tables [0];.

Adding a calendar on OutLook, in fact, it is to add an appointment.

New Appointment is calling OutLook of ApplicationClass.

The main code is as follows:

                ApplicationClass oApp = new Microsoft.Office.Interop.Outlook.ApplicationClass();

                // meeting date is a 
                AppointmentItem oItem = (AppointmentItem) oApp.CreateItem (OlItemType.olAppointmentItem);
                oItem.MeetingStatus = OlMeetingStatus.olMeeting;
                oItem.Subject = " Production assault " ;
                oItem.Body = " Content " ;
                oItem.Location = " place " ;
                 // start time  
                oItem.Start = DateTime.Now.AddDays ( 1 );
                 // End Time 
                oItem.End = DateTime.Now.AddDays ( 1 ) .AddHours ( 4 );
                 // remind provided 
                oItem.ReminderSet = to true ;
                oItem.ReminderMinutesBeforeStart = 5;

                // whether all-day event 
                oItem.AllDayEvent = false ;

                oItem.BusyStatus = OlBusyStatus.olBusy;

                // index from the beginning, rather than from 0
                 // sender account information 
                var ACC = oApp.Session.Accounts;
                oItem.SendUsingAccount = oApp.Session.Accounts[1];

                // add people Required 
                Recipient Force = oItem.Recipients.Add ( " [email protected] " );
                force = oItem.Recipients.Add("[email protected]");
                force.Type = ( int ) OlMeetingRecipientType.olRequired;
                 /// / add optional person 
                // Recipient oItem.Recipients.Add opt = ( "[email protected]");
                 // opt.Type = (int) OlMeetingRecipientType.olOptional; 
                /// / session initiator added 
                // Recipient meetingSender = oItem.Recipients.Add ( "[email protected]");
                 // meetingSender.Type = (int) OlMeetingRecipientType.olOrganizer;

                oItem.Recipients.ResolveAll();
                oItem.Send();

Excel only read, but also made an appointment, call screening and the following is the data in Excel, I believe not beat you, do not say.

So far, this simple Demo be done, hope to be useful.

Reproduced in: https: //www.cnblogs.com/ushou/p/3358961.html

Guess you like

Origin blog.csdn.net/weixin_33920401/article/details/93162897