VSTO 加载TASKPANE

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using Excel = Microsoft.Office.Interop.Excel;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;

namespace ExcelAddInDemo
{
    using Microsoft.Office.Tools;
    using System.Windows.Forms;
    using Excel = Microsoft.Office.Interop.Excel;
    using Microsoft;
    public partial class ThisAddIn
    {
        public CustomTaskPane _MyCustomTaskPane = null;

        private void ThisAddIn_Startup(object sender, System.EventArgs e)
        {
           

     /*       UCTaskPane2 taskPane = new UCTaskPane2();
            _MyCustomTaskPane = this.CustomTaskPanes.Add(taskPane, "我的任务面板");
            _MyCustomTaskPane.Width = 30;//height有问题,此处width ==height
            _MyCustomTaskPane.Visible = true;
            _MyCustomTaskPane.DockPosition = Microsoft.Office.Core.MsoCTPDockPosition.msoCTPDockPositionTop;
*/
            UCPaneLeft panLeft = new UCPaneLeft();
            _MyCustomTaskPane = this.CustomTaskPanes.Add(panLeft, "OceanTaskPane");
            _MyCustomTaskPane.Width = 200;
            _MyCustomTaskPane.Visible = true;
            _MyCustomTaskPane.DockPosition = Microsoft.Office.Core.MsoCTPDockPosition.msoCTPDockPositionRight;

           /* UCTaskPane2 panRight = new UCTaskPane2();
            _MyCustomTaskPane = this.CustomTaskPanes.Add(panRight, "用户列表");
            _MyCustomTaskPane.Width = 200;
            _MyCustomTaskPane.Visible = true;
            _MyCustomTaskPane.DockPosition = Microsoft.Office.Core.MsoCTPDockPosition.msoCTPDockPositionRight;
            */
           /* UCLog panLog = new UCLog();
            _MyCustomTaskPane = this.CustomTaskPanes.Add(panLog, "日志列表");
            _MyCustomTaskPane.Width = 60;
            _MyCustomTaskPane.Visible = true;
            _MyCustomTaskPane.DockPosition = Microsoft.Office.Core.MsoCTPDockPosition.msoCTPDockPositionBottom;
            */
            //Hook into the workbook open event
            //This is because Office doesn't always have a document ready when this method is run 
            this.Application.WorkbookActivate += Application_WorkbookActivate;
            //test 
            //this.Application.SheetSelectionChange += Application_SheetSelectionChange;
        }

        void Application_SheetSelectionChange(object Sh, Excel.Range Target)
        {
            if (this.Application != null)
            {
                this.Application.Caption = this.Application.ActiveCell.Address.ToString();//$A$1
                                                                                          //+ this.Application.ActiveCell.AddressLocal.ToString();//$A$1
                                                                                          //this.Application.ActiveCell.Formula = "=sum(1+2)";

            }
        }

        void Application_WorkbookActivate(Excel.Workbook Wb)
        {
            //using Microsoft.Office.Tools.Excel 和 using Microsoft.Office.Interop.Excel 都有worksheet等,容易混淆
            //string path = this.Application.ActiveWorkbook.FullName;
            Excel._Worksheet ws = (Excel._Worksheet)this.Application.ActiveWorkbook.ActiveSheet;
            ws.Cells[2, 2] = "ID2";
            //如何设置只读等有待研究
            int r = 2, c = 2;
            //((Excel.Range)ws.Cells[r, c]).NumberFormat = format;
            ((Excel.Range)ws.Cells[r, c]).Value2 = "ID";
            ((Excel.Range)ws.Cells[r, c]).Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Red);
            //((Excel.Range)ws.Cells[r, c]).Style.Name = "Normal";
            ((Excel.Range)ws.Cells[r, c]).Style.Font.Bold = true;

            #region format
            ((Microsoft.Office.Interop.Excel.Range)ws.get_Range("A2", "E10")).Font.Bold = true;
            ((Microsoft.Office.Interop.Excel.Range)ws.get_Range("A2", "E10")).Font.Italic = true;
            ((Microsoft.Office.Interop.Excel.Range)ws.get_Range("A2", "E10")).Font.Color = System.Drawing.Color.FromArgb(96, 32, 0).ToArgb();
            ((Microsoft.Office.Interop.Excel.Range)ws.get_Range("A2", "E10")).Font.Name = "Calibri";
            ((Microsoft.Office.Interop.Excel.Range)ws.get_Range("A2", "E10")).Font.Size = 15;

            //border
            Excel.Range range = ((Microsoft.Office.Interop.Excel.Range)ws.get_Range("B2", "E3"));
            Excel.Borders border = range.Borders;
            border[Excel.XlBordersIndex.xlEdgeBottom].LineStyle = Excel.XlLineStyle.xlContinuous;
            border.Weight = 2d;
            border[Excel.XlBordersIndex.xlEdgeTop].LineStyle = Excel.XlLineStyle.xlContinuous;
            border[Excel.XlBordersIndex.xlEdgeLeft].LineStyle = Excel.XlLineStyle.xlContinuous;
            border[Excel.XlBordersIndex.xlEdgeRight].LineStyle = Excel.XlLineStyle.xlContinuous;
            #endregion
            ws.Cells[2, 4] = "First";
            ws.Cells[3, 2] = "Last";
            ws.Cells[3, 4] = "Email";
        }

        //****************************************************

        //****************************************************
        private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
        {
        }

        #region VSTO 生成的代码

        /// <summary>
        /// 设计器支持所需的方法 - 不要
        /// 使用代码编辑器修改此方法的内容。
        /// </summary>
        private void InternalStartup()
        {
            this.Startup += new System.EventHandler(ThisAddIn_Startup);
            this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
        }

        #endregion
    }
}

猜你喜欢

转载自blog.csdn.net/time_forgotten/article/details/89362153