前面我们介绍了如何使用spire.xls ,从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)
效果图: