C#操做PPT表格

1.激活組件windows

     AxFramerControl改控件的dll本身再網上百度下下載這裏很少講ide

        /// <summary>
        /// 檢測是否註冊控件
        /// </summary>
        public static void CheckDsoFramerExists()
        {
            string path = @"c:\windows\system32\dsoframer.ocx";
            string filepath = Application.StartupPath;
            if (!File.Exists(path))
            {
                File.Copy(filepath + "\\dsoframer.ocx", path);
            }
            ProcessStartInfo psi = new ProcessStartInfo("regsvr32", "/s " + path);
            Process.Start(psi);
        }字體

2.開始操做寫入表格blog

裏面包含建立單元格  單元格複製 單元格字體居中  合併單元格等操做get

        /// <summary>
        /// 建立人員分佈表
        /// </summary>
        /// <param name="obj"></param>
        public static void CreateTempPersonInfoByCom(object obj)
        {
            PPT.Presentation ppt = obj as PPT.Presentation;
            if (ppt == null) { return; }
            //清楚ppt現有內容
            int slides = ppt.Slides.Count;
            for (int i = 0; i < slides; i++)
            {
                ppt.Slides[1].Delete(); ;
            }
            ppt.Slides.Add(1, PPT.PpSlideLayout.ppLayoutTable);
            foreach (PPT.Slide item in ppt.Slides)
            {
                item.Shapes.Range(1).Delete();
                item.Shapes.Range(1).Delete();
                item.Shapes.AddTable(7, 9, 10, 10, 700, 300);
                PPT.Table table = item.Shapes.Range(1).Table;
                if (table != null)
                {
                    table.Cell(1, 1).Shape.TextFrame.TextRange.Text = "類別";
                    table.Cell(1, 2).Shape.TextFrame.TextRange.Text = "數量";
                    table.Cell(1, 3).Shape.TextFrame.TextRange.Text = "獨立操做人員數量";
                    table.Cell(1, 4).Shape.TextFrame.TextRange.Text = "工齡分佈";
                    table.Cell(1, 8).Shape.TextFrame.TextRange.Text = "每日人員變更狀況";
                    table.Cell(1, 9).Shape.TextFrame.TextRange.Text = "備註";
                    table.Cell(2, 4).Shape.TextFrame.TextRange.Text = "入職五年以上";
                    table.Cell(2, 5).Shape.TextFrame.TextRange.Text = "入職3-5年";
                    table.Cell(2, 6).Shape.TextFrame.TextRange.Text = "入職1-3年";
                    table.Cell(2, 7).Shape.TextFrame.TextRange.Text = "入職1年如下";
                    table.Cell(3, 1).Shape.TextFrame.TextRange.Text = "正式";
                    table.Cell(4, 1).Shape.TextFrame.TextRange.Text = "試用";
                    table.Cell(5, 1).Shape.TextFrame.TextRange.Text = "實習";
                    table.Cell(6, 1).Shape.TextFrame.TextRange.Text = "勞務";
                    table.Cell(7, 1).Shape.TextFrame.TextRange.Text = "合計";
                    //循環設置單元格樣式
                    SetTableStyle(table);
                    //設置合併操做
                    table.Cell(1, 1).Merge(table.Cell(2, 1));
                    table.Cell(1, 2).Merge(table.Cell(2, 2));
                    table.Cell(1, 3).Merge(table.Cell(2, 3));
                    table.Cell(1, 8).Merge(table.Cell(2, 8));
                    table.Cell(1, 9).Merge(table.Cell(2, 9));
                    table.Cell(1, 4).Merge(table.Cell(1, 7));
                }
            }
           
        }string

//設置樣式it

        /// <summary>         io

/// 設置table樣式        table

/// </summary>        百度

/// <param name="table"></param>      

   private static void SetTableStyle(PPT.Table table)       

  {         

    if (table == null) { return; }

            //循環設置單元格樣式         

    for (int i = 0; i < table.Rows.Count; i++)       

      {              

   for (int j = 0; j < table.Columns.Count; j++)        

         {                    

        table.Cell(i + 1, j + 1).Shape.TextFrame.TextRange.Font.Size = 14;             

        table.Cell(i + 1, j + 1).Shape.TextFrame.HorizontalAnchor = MsoHorizontalAnchor.msoAnchorCenter;  

         table.Cell(i + 1, j + 1).Shape.TextFrame.VerticalAnchor = MsoVerticalAnchor.msoAnchorMiddle;             

        }       

      }

        }

 

 3.效果圖以下

相關文章
相關標籤/搜索