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)