【教程】Spire.XLS 教程:在C#中将形状插入Excel工作表

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

截图:

图片1

完整代码:

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

慧都控件网

转载于:https://my.oschina.net/u/3006003/blog/1550379

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值