原創|使用教程|編輯:王香|2017-10-13 15:23:33.000|閱讀 705 次
概述:Spire.XLS 是一個專業的Excel控件,有 .NET、WPF和Silverlight 版本,通過Spire.XLS無需安裝微軟Excel,也能擁有Excel的全套功能,本文介紹了如何通過Spire.XLS 在C#中將形狀插入Excel工作表。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
Spire.XLS 支持最多添加186種現成形狀,如三角形,箭頭和標注到Excel工作表。 本文演示如何使用Spire.XLS 和C#將形狀插入Excel工作表,并使用顏色和圖片填充形狀。
詳細步驟:
Step 1: 實例化一個Workbook對象并獲取第一個工作表。
Workbook workbook = new Workbook(); Worksheet sheet = workbook.Worksheets[0];
Step 2: 添加三角形,并填充純色的形狀。
//Add a triangle shape IPrstGeomShape triangle = sheet.PrstGeomShapes.AddPrstGeomShape(2, 2, 100, 100, PrstGeomShapeType.Triangle); //Fill the triangle with solid color triangle.Fill.ForeColor = Color.Yellow; triangle.Fill.FillType = ShapeFillType.SolidColor;
Step 3: 添加心形并用漸變色填充形狀。
//Add a heart shape IPrstGeomShape heart = sheet.PrstGeomShapes.AddPrstGeomShape(2, 5, 100, 100, PrstGeomShapeType.Heart); //Fill the heart with gradient color heart.Fill.ForeColor = Color.Red; heart.Fill.FillType = ShapeFillType.Gradient;
Step 4: 使用默認顏色添加箭頭形狀。
IPrstGeomShape arrow = sheet.PrstGeomShapes.AddPrstGeomShape(10, 2, 100, 100, PrstGeomShapeType.CurvedRightArrow);
Step 5: 添加云形,并用自定義圖片填充形狀。
//Add a cloud shape IPrstGeomShape cloud = sheet.PrstGeomShapes.AddPrstGeomShape(10, 5, 100, 100, PrstGeomShapeType.Cloud); //Fill the cloud with picture cloud.Fill.CustomPicture(Image.FromFile("Hydrangeas.jpg"), "Hydrangeas.jpg"); cloud.Fill.FillType = ShapeFillType.Picture;
Step 6: 保存文件。
workbook.SaveToFile("AddShapes.xlsx", ExcelVersion.Version2013);
截圖:
完整代碼:
using System.Drawing; using Spire.Xls; using Spire.Xls.Core; namespace Add_shapes_to_Excel { class Program { static void Main(string[] args) { //Instantiate a workbook object Workbook workbook = new Workbook(); //Get the first worksheet Worksheet sheet = workbook.Worksheets[0]; //Add a triangle shape IPrstGeomShape triangle = sheet.PrstGeomShapes.AddPrstGeomShape(2, 2, 100, 100, PrstGeomShapeType.Triangle); //Fill the triangle with solid color triangle.Fill.ForeColor = Color.Yellow; triangle.Fill.FillType = ShapeFillType.SolidColor; //Add a heart shape IPrstGeomShape heart = sheet.PrstGeomShapes.AddPrstGeomShape(2, 5, 100, 100, PrstGeomShapeType.Heart); //Fill the heart with gradient color heart.Fill.ForeColor = Color.Red; heart.Fill.FillType = ShapeFillType.Gradient; //Add an arrow shape with default color IPrstGeomShape arrow = sheet.PrstGeomShapes.AddPrstGeomShape(10, 2, 100, 100, PrstGeomShapeType.CurvedRightArrow); //Add a cloud shape IPrstGeomShape cloud = sheet.PrstGeomShapes.AddPrstGeomShape(10, 5, 100, 100, PrstGeomShapeType.Cloud); //Fill the cloud with custom picture cloud.Fill.CustomPicture(Image.FromFile("Hydrangeas.jpg"), "Hydrangeas.jpg"); cloud.Fill.FillType = ShapeFillType.Picture; //Save the file workbook.SaveToFile("AddShapes.xlsx", ExcelVersion.Version2013); } } }
慧都控件網
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn