该文将介绍如何使用Spire.XLS 在.NET应用程序中通过设置线条起点和终点,然后以这两个点的位置添加线条到Excel工作表。线条起点和终点的位置可以为相对位置或绝对位置。详细代码:
C#
using Spire.Xls;
using Spire.Xls.Core.Spreadsheet.Shapes;
using System.Drawing;
namespace AddLinestoExcel
{
class Program
{
static void Main(string[] args)
{
//新建workbook并获取第一个工作表
Workbook workbook = new Workbook();
Worksheet worksheet = workbook.Worksheets[0];
//通过相对位置添加线条
XlsLineShape line = worksheet.TypedLines.AddLine() as XlsLineShape;
//设置线条起点位置
line.LeftColumn = 2;
line.LeftColumnOffset = 2;
line.TopRow = 5;
line.TopRowOffset = 10;
//设置线条终点位置
line.RightColumn = 10;
line.RightColumnOffset = 10;
line.BottomRow =5;
line.BottomRowOffset = 10;
//设置线条颜色
line.Color = Color.Red;
//通过绝对位置添加线条,单位为像素
XlsLineShape line1 = worksheet.TypedLines.AddLine() as XlsLineShape;
//设置线条起点和终点位置
line1.StartPoint = new Point(20, 30);
line1.EndPoint = new Point(200, 30);
//设置线条颜色
line1.Color = Color.Blue;
workbook.SaveToFile("Addlines.xlsx", ExcelVersion.Version2013);
workbook.Dispose();
}
}
}
VB.NET
Imports Spire.Xls
Imports Spire.Xls.Core.Spreadsheet.Shapes
Imports System.Drawing
Namespace AddLinestoExcel
Class Program
Private Shared Sub Main(ByVal args() As String)
'新建workbook并获取第一个工作表
Dim workbook As Workbook = New Workbook
Dim worksheet As Worksheet = workbook.Worksheets(0)
'通过相对位置添加线条
Dim line As XlsLineShape = CType(worksheet.TypedLines.AddLine,XlsLineShape)
'设置线条起点位置
line.LeftColumn = 2
line.LeftColumnOffset = 2
line.TopRow = 5
line.TopRowOffset = 10
'设置线条终点位置
line.RightColumn = 10
line.RightColumnOffset = 10
line.BottomRow = 5
line.BottomRowOffset = 10
'设置线条颜色
line.Color = Color.Red
//通过绝对位置添加线条,单位为像素
Dim line1 As XlsLineShape = CType(worksheet.TypedLines.AddLine,XlsLineShape)
'设置线条起点和终点位置
line1.StartPoint = New Point(20, 30)
line1.EndPoint = New Point(200, 30)
'设置线条颜色
line1.Color = Color.Blue
workbook.SaveToFile("Addlines.xlsx", ExcelVersion.Version2013)
workbook.Dispose
End Sub
End Class
End Namespace
效果图: