前面我们介绍了如何使用Spire.XLS 插入文本或图片文本框到Excel工作表,从Spire.XLS V. 9.3.10开始,Spire.XLS新添加了设置文本框中内容页边距的属性。通过该属性,我们可以设置文本框中内容的位置。该文将详细介绍如何使用C#设置Excel 工作表中文本框的内容位置。
C#
{
     
     //加载文档
     Workbook workbook = new Workbook();
     workbook.LoadFromFile("Sample.xlsx", ExcelVersion.Version2010);
     //获取第一个worksheet
     Worksheet sheet = workbook.Worksheets[0];
     //添加文本框并设置其位置,大小
     XlsTextBoxShape textbox = sheet.TextBoxes.AddTextBox(4, 2, 100, 300) as XlsTextBoxShape;
     
     //设置文本框文字和对齐方式
     textbox.Text = "添加文本框并设置内容页边距";
     textbox.HAlignment = CommentHAlignType.Center;
     textbox.VAlignment = CommentVAlignType.Center;
     //设置文本内容上下左右页边距 
     textbox.InnerLeftMargin = 1;
     textbox.InnerRightMargin = 3;
     textbox.InnerTopMargin = 1;
     textbox.InnerBottomMargin = 1;
     
     //保存文档
     workbook.SaveToFile("Result.xlsx", ExcelVersion.Version2010);        
     
 }VB.NET
'加载文档 Dim workbook As New Workbook() workbook.LoadFromFile("Sample.xlsx",ExcelVersion.Version2010) '获取第一个worksheet
Dim sheet As Worksheet = workbook.Worksheets(0)
'添加文本框并设置其位置,大小 Dim textbox As XlsTextBoxShape = TryCast(sheet.TextBoxes.AddTextBox(4,2,100,300),XlsTextBoxShape) '设置文本框文字和对齐方式
textbox.Text = "添加文本框并设置内容页边距"
textbox.HAlignment = CommentHAlignType.Center
textbox.VAlignment = CommentVAlignType.Center
'设置文本内容上下左右页边距 textbox.InnerLeftMargin = 1 textbox.InnerRightMargin = 3 textbox.InnerTopMargin = 1 textbox.InnerBottomMargin = 1 '保存文档
workbook.SaveToFile("Result.xlsx", ExcelVersion.Version2010)效果图:

 



 
					



