【教程】Spire.XLS 教程:在C#中將形狀插入Excel工做表

Spire.XLS 支持最多添加186種現成形狀,如三角形,箭頭和標註到Excel工做表。 本文演示如何使用Spire.XLS 和C#將形狀插入Excel工做表,並使用顏色和圖片填充形狀。spa

詳細步驟:對象

Step 1: 實例化一個Workbook對象並獲取第一個工做表。blog

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: 添加心形並用漸變色填充形狀。get

//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: 使用默認顏色添加箭頭形狀。string

IPrstGeomShape arrow = sheet.PrstGeomShapes.AddPrstGeomShape(10, 2, 100, 100, PrstGeomShapeType.CurvedRightArrow);

Step 5: 添加雲形,並用自定義圖片填充形狀。it

//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: 保存文件。io

workbook.SaveToFile("AddShapes.xlsx", ExcelVersion.Version2013);

截圖:class

圖片1

完整代碼:object

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

慧都控件網

相關文章
相關標籤/搜索