该文将介绍如何使用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
效果图: