C# 添加箭头线条形状到Excel

在Excel中,可以添加不同类型的形状,包括线条形状、矩形形状、基本形状、箭头总汇、公式形状、流程图、标注形状、星于旗帜形状等等。在“C# 操作Excel形状”这篇文章中,介绍了在Excel中添加、读...

在Excel中,可以添加不同类型的形状,包括线条形状、矩形形状、基本形状、箭头总汇、公式形状、流程图、标注形状、星于旗帜形状等等。在“C# 操作Excel形状”这篇文章中,介绍了在Excel中添加、读取、隐藏、删除形状的方法。本篇文章将介绍除上述文章中涉及到的形状外,带箭头的线条形状的添加方法。通过这些线条形状应用到表格中可起到特定指示、连接或转换作用。下面将通过C#代码示例演示。

 

使用工具: Spire.XLS for .NET 9.9.0

 

文件获取及dll引用:

方法1通过官网下载文件包。下载后,解压文件。将bin文件夹中的Spire.Xls.dll文件添加引用到C# 程序。如下图:


 

方法2可通过Nuget下载。

 

C#代码示例(供参考)

using Spire.Xls;
using System.Drawing;

namespace AddLines_XLS
{
    class Program
    {
        static void Main(string[] args)
        {
            //创建workbook类的实例,并获取第一张工作表
            Workbook workbook = new Workbook();
            Worksheet sheet = workbook.Worksheets[0];

            //添加水平方向带箭头的线条,并设置线条颜色           
            var line1 = sheet.TypedLines.AddLine();
            line1.Top = 10;
            line1.Left = 20;
            line1.Width = 100;
            line1.Height = 0;
            line1.Color = Color.Blue;
            line1.BeginArrowHeadStyle = ShapeArrowStyleType.LineArrow;          
            var line11 = sheet.TypedLines.AddLine();
            line11.Top = 40;
            line11.Left = 20;
            line11.Width = 100;
            line11.Height = 0;
            line11.Color = Color.Blue;            
            line11.EndArrowHeadStyle = ShapeArrowStyleType.LineArrow;

            var line111 = sheet.TypedLines.AddLine();
            line111.Top = 70;
            line111.Left = 20;
            line111.Width = 100;
            line111.Height = 0;
            line111.Color = Color.Blue;
            line111.BeginArrowHeadStyle = ShapeArrowStyleType.LineArrow;
            line111.EndArrowHeadStyle = ShapeArrowStyleType.LineArrow;

            //插入倾斜的带箭头的线条,并设置线条颜色
            var line_1 = sheet.TypedLines.AddLine();
            line_1.Top = 10;
            line_1.Left = 140;
            line_1.Width = 60;
            line_1.Height = 50;
            line_1.Color = Color.Red;
            line_1.BeginArrowHeadStyle = ShapeArrowStyleType.LineNoArrow;
            line_1.EndArrowHeadStyle = ShapeArrowStyleType.LineArrow;

            var line_2 = sheet.TypedLines.AddLine();
            line_2.Top = 10;
            line_2.Left = 180;
            line_2.Width = 60;
            line_2.Height = 50;
            line_2.Color = Color.Red;
            line_2.BeginArrowHeadStyle = ShapeArrowStyleType.LineArrow;
            line_2.EndArrowHeadStyle = ShapeArrowStyleType.LineNoArrow;

            var line_3 = sheet.TypedLines.AddLine();
            line_3.Top = 10;
            line_3.Left = 220;
            line_3.Width = 60;
            line_3.Height = 50;
            line_3.Color = Color.Red;
            line_3.BeginArrowHeadStyle = ShapeArrowStyleType.LineArrow;
            line_3.EndArrowHeadStyle = ShapeArrowStyleType.LineArrow;

            //插入肘形箭头连接符
            Spire.Xls.Core.Spreadsheet.Shapes.XlsLineShape line3 = sheet.TypedLines.AddLine() as Spire.Xls.Core.Spreadsheet.Shapes.XlsLineShape;
            line3.LineShapeType = LineShapeType.ElbowLine;
            line3.Width = 30;
            line3.Height = 50;
            line3.EndArrowHeadStyle = ShapeArrowStyleType.LineArrow;
            line3.Top = 100;
            line3.Left = 50;

            Spire.Xls.Core.Spreadsheet.Shapes.XlsLineShape line2 = sheet.TypedLines.AddLine() as Spire.Xls.Core.Spreadsheet.Shapes.XlsLineShape;
            line2.LineShapeType = LineShapeType.ElbowLine;            
            line2.Top = 100;
            line2.Left = 100;
            line2.Width = 50;
            line2.Height = 50;
            line2.EndArrowHeadStyle = ShapeArrowStyleType.LineArrow;
            line2.BeginArrowHeadStyle = ShapeArrowStyleType.LineArrow;
            

            //插入曲线箭头连接符
            line3 = sheet.TypedLines.AddLine() as Spire.Xls.Core.Spreadsheet.Shapes.XlsLineShape;
            line3.LineShapeType = LineShapeType.CurveLine;
            line3.Top = 100;
            line3.Left = 170;
            line3.Width = 30;
            line3.Height = 50;
            line3.EndArrowHeadStyle = ShapeArrowStyleType.LineArrowOpen;
            
            line2 = sheet.TypedLines.AddLine() as Spire.Xls.Core.Spreadsheet.Shapes.XlsLineShape;
            line2.LineShapeType = LineShapeType.CurveLine;           
            line2.Top = 100;
            line2.Left = 230;
            line2.Width = 30;
            line2.Height = 50;
            line2.EndArrowHeadStyle = ShapeArrowStyleType.LineArrowOpen;
            line2.BeginArrowHeadStyle = ShapeArrowStyleType.LineArrowOpen;
            

            //保存文档
            workbook.SaveToFile("AddLines.xlsx", ExcelVersion.Version2013);
          }
    }
}

箭头线条形状添加效果:


 

(本文完)

转载请注明出处!

  • 发表于 2019-09-06 18:20
  • 阅读 ( 62 )
  • 分类:网络文章

条评论

请先 登录 后评论
不写代码的码农
小编

篇文章

作家榜 »

  1. 小编 文章
返回顶部
部分文章转自于网络,若有侵权请联系我们删除