向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();
}
}