C# & VB.NET 在Excel中通过两点绘制直线形状

天涯浪子 提交于 2020-12-03 01:27:53

本文介绍通过C#及VB.NET程序在Excel中绘制直线形状的方法,使用的控件是Spire.XLS for .NET提供的形状类来绘制。绘制直线形状时,可通过2种方式,一种是通过IPrstGeomShape类提供的形状集合来添加预设形状,支持线条形状(以及其他多种形状类型),即

IPrstGeomShape Line = sheet.PrstGeomShapes.AddPrstGeomShape(int row, int column, int width, int height, PrstGeomShapeType.Line);

另一种是通过XlsLineShape类来绘制直线形状。本文着重介绍第二种。
注意:这里使用的Spire版本是hotfix 10.11.7,可通过官网下载或者Nuget下载,下载后将Spire.XLS.dll文件添加引用到VS程序,如下引用效果:
C# & VB.NET 在Excel中通过两点绘制直线形状
C#


using Spire.Xls;
using Spire.Xls.Core.Spreadsheet.Shapes;
using System.Drawing;

namespace AddLineThroung2Points
{
    class Program
    {
        static void Main(string[] args)
        {
            //新建workbook工作簿,并获取第一个工作表(创建的工作簿默认已包含三个工作表)
            Workbook workbook = new Workbook();
            Worksheet worksheet = workbook.Worksheets[0];

            //通过相对位置添加线条
            XlsLineShape line1 = worksheet.TypedLines.AddLine() as XlsLineShape;
            //设置线条起点位置
            line1.LeftColumn = 2;
            line1.LeftColumnOffset = 2;
            line1.TopRow = 5;
            line1.TopRowOffset = 10;

            //设置线条终点位置
            line1.RightColumn = 6;
            line1.RightColumnOffset = 10;
            line1.BottomRow = 5;
            line1.BottomRowOffset = 10;

            //设置线条颜色、宽度、名称
            line1.Color = Color.Red;
            line1.Weight = 5;
            line1.SetName("相对位置线条");

            //通过绝对位置添加线条,单位为像素
            XlsLineShape line2 = worksheet.TypedLines.AddLine() as XlsLineShape;

            //设置线条起点和终点位置
            line2.StartPoint = new Point(140, 12);
            line2.EndPoint = new Point(280,120);

            //设置线条颜色、宽度、名称
            line2.Color = Color.Blue;
            line2.Weight = 5;
            line2.SetName("绝对位置线条");

            //保存文档
            workbook.SaveToFile("Addlines.xlsx", ExcelVersion.Version2013);
            System.Diagnostics.Process.Start("Addlines.xlsx");
        }
    }
}

直线形状绘制效果图:
C# & VB.NET 在Excel中通过两点绘制直线形状

VB.NET

Imports Spire.Xls
Imports Spire.Xls.Core.Spreadsheet.Shapes
Imports System.Drawing

Namespace AddLineThroung2Points
    Class Program
        Private Shared Sub Main(args As String())
            '新建workbook工作簿,并获取第一个工作表(创建的工作簿默认已包含三个工作表)
            Dim workbook As New Workbook()
            Dim worksheet As Worksheet = workbook.Worksheets(0)

            '通过相对位置添加直线线条
            Dim line1 As XlsLineShape = TryCast(worksheet.TypedLines.AddLine(), XlsLineShape)
            '设置线条起点位置
            line1.LeftColumn = 2
            line1.LeftColumnOffset = 2
            line1.TopRow = 5
            line1.TopRowOffset = 10

            '设置线条终点位置
            line1.RightColumn = 6
            line1.RightColumnOffset = 10
            line1.BottomRow = 5
            line1.BottomRowOffset = 10

            '设置线条颜色、宽度、名称
            line1.Color = Color.Red
            line1.Weight = 5
            line1.SetName("相对位置线条")

            '通过绝对位置添加线条,单位为像素
            Dim line2 As XlsLineShape = TryCast(worksheet.TypedLines.AddLine(), XlsLineShape)

            '设置线条起点和终点位置
            line2.StartPoint = New Point(140, 12)
            line2.EndPoint = New Point(280, 120)

            '设置线条颜色、宽度、名称
            line2.Color = Color.Blue
            line2.Weight = 5
            line2.SetName("绝对位置线条")

            '保存文档
            workbook.SaveToFile("Addlines.xlsx", ExcelVersion.Version2013)
            System.Diagnostics.Process.Start("Addlines.xlsx")
        End Sub
    End Class
End Namespace

< 完 >

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!