向Excel插入矩形問題

向Excel插入矩形圖形就四行代碼。而且真正有用的就兩句

  static public void InsertRectToExcelOnTemp( string phyFileTar, int sheetIndex, float x, float y, float Width, float Height, string caption,int m )
        {
            Excel.ApplicationClass excelApp = new Excel.ApplicationClass();

            //打開模板文件,得到WorkBook對象
            Excel.Workbooks workBooks = excelApp.Workbooks;
            Excel.Workbook workBook = workBooks._Open( phyFileTar, Missing.Value, Missing.Value, Missing.Value, Missing.Value
                , Missing.Value, Missing.Value, Missing.Value, Missing.Value
                , Missing.Value, Missing.Value, Missing.Value, Missing.Value );
            Excel.Worksheet workSheet = ( Excel.Worksheet )workBook.Sheets[sheetIndex];
            try
            {
                Shape mysheap = workSheet.Shapes.AddShape( Microsoft.Office.Core.MsoAutoShapeType.msoShapeRectangle, x, y, Width, Height );//矩形的左邊距,上邊距,寬和高
                mysheap.TextFrame.Characters( 1, caption.Length ).Text = caption;//設置矩形的值
                mysheap.TextFrame.HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter;//水平居中
                mysheap.TextFrame.VerticalAlignment = Excel.XlVAlign.xlVAlignCenter;//垂直居中
                mysheap.TextFrame.Characters( 1, caption.Length ).Font.Name = "宋體";
                mysheap.TextFrame.Characters( 1, caption.Length ).Font.FontStyle = "常規";
                mysheap.Fill.ForeColor.SchemeColor = m;
                workBook.Save();
            }
            catch ( Exception Er )
            {
                throw new Exception( "調用EXCEL程序出現錯誤!" + Er.Message );
            }
            finally{
                if( workBook != null ) workBook.Close( false, Missing.Value, Missing.Value );
                if( workBooks != null ) workBooks.Close();
                if( excelApp != null ) excelApp.Quit();
            }

        }
       

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章