spire.xls支持插入文本框到excel工作表中,并且插入文本框之后还可以对它进行美化。比如,设置文本框的字体,背景色和去除文本框边框,也可以在文本框中插入图片让它更美观。该文将介绍如果使用spire.xls插入文本框和删除文本框。
插入文本框
c#
//加载excel文档
workbook workbook = new workbook();
workbook.loadfromfile("sample.xlsx");
//获取第一个工作表
worksheet sheet = workbook.worksheets[0];
//插入第一个文本框并设置文本框位置和大小
itextbox textbox = sheet.textboxes.addtextbox(3, 2, 50, 196);
//设置文本框文本和对齐方式
textbox.text = "插入excel文本框";
textbox.halignment = commenthaligntype.center;
textbox.valignment = commentvaligntype.center;
//设置文本框字体,颜色,大小
excelfont font = workbook.createfont();
font.fontname = "arial";
font.size = 12;
font.isbold = true;
font.color = color.white;
(new richtext(textbox.richtext)).setfont(0, textbox.text.length - 1, font);
//设置文本框背景色
xlstextboxshape shape = sheet.textboxes[0] as xlstextboxshape;
shape.fill.filltype = shapefilltype.solidcolor;
shape.fill.foreknowncolor = excelcolors.bluegray;
//插入第二个文本框并设置文本框位置和大小
itextboxshape shape2 = sheet.textboxes.addtextbox(10, 3, 200, 200);
//加载图片并用图片填充文本框
shape2.fill.custompicture("logo.png");
shape2.fill.filltype = shapefilltype.picture;
//获取第二个文本框
xlstextboxshape shape3 = sheet.textboxes[1] as xlstextboxshape;
//设置文本框边框为0
shape3.line.weight = 0;
//保存文档
workbook.savetofile("textbox.xlsx", excelversion.version2013);
vb.net
'加载excel文档
dim workbook as new workbook()
workbook.loadfromfile("sample.xlsx")
'获取第一个工作表
dim sheet as worksheet = workbook.worksheets(0)
'插入第一个文本框并设置文本框位置和大小
dim textbox as itextbox = sheet.textboxes.addtextbox(3, 2, 50, 196)
'设置文本框文本和对齐方式
textbox.text = "插入excel文本框"
textbox.halignment = commenthaligntype.center
textbox.valignment = commentvaligntype.center
'设置文本框字体,颜色,大小
dim font as excelfont = workbook.createfont()
font.fontname = "arial"
font.size = 12
font.isbold = true
font.color = color.white
(new richtext(textbox.richtext)).setfont(0, textbox.text.length - 1, font)
'设置文本框背景色
dim shape as xlstextboxshape = trycast(sheet.textboxes(0), xlstextboxshape)
shape.fill.filltype = shapefilltype.solidcolor
shape.fill.foreknowncolor = excelcolors.bluegray
'插入第二个文本框并设置文本框位置和大小
dim shape2 as itextboxshape = sheet.textboxes.addtextbox(10, 3, 200, 200)
'加载图片并用图片填充文本框
shape2.fill.custompicture("logo.png")
shape2.fill.filltype = shapefilltype.picture
'获取第二个文本框
dim shape3 as xlstextboxshape = trycast(sheet.textboxes(1), xlstextboxshape)
'设置文本框边框为0
shape3.line.weight = 0
'保存文档
workbook.savetofile("textbox.xlsx", excelversion.version2013)
删除文本框
c#
//加载excel文档
workbook workbook = new workbook();
workbook.loadfromfile("textbox.xlsx");
//获取第一个工作表
worksheet sheet = workbook.worksheets[0];
//获取第二个文本框
xlstextboxshape textboxshape = sheet.textboxes[1] as xlstextboxshape;
//删除文本框
textboxshape.remove();
//保存文档
workbook.savetofile("deletetextbox.xlsx", excelversion.version2013);
vb.net
'加载excel文档
dim workbook as new workbook()
workbook.loadfromfile("textbox.xlsx")
'获取第一个工作表
dim sheet as worksheet = workbook.worksheets(0)
'获取第二个文本框
dim textboxshape as xlstextboxshape = trycast(sheet.textboxes(1), xlstextboxshape)
'删除文本框
textboxshape.remove()
'保存文档
workbook.savetofile("deletetextbox.xlsx", excelversion.version2013)