在excel表格中输入文字时,我们常常需要调整文字对齐方式或者对文字进行换行。本文将介绍如何通过编程的方式设置文字对齐方式,改变文字方向以及对文字进行换行。
c#
//创建workbook对象
workbook wookbook = new workbook();
worksheet sheet = wookbook.worksheets[0];
//添加说明文字
sheet.range["b1"].text = "文字对齐方式";
sheet.range["d1"].text = "文字方向";
sheet.range["f1"].text = "文字换行";
sheet.range["b1:f1"].style.font.isbold = true;
//左对齐
sheet.range["b3"].text = "左对齐";
sheet.range["b3"].style.horizontalalignment = horizontalaligntype.left;
//水平居中
sheet.range["b4"].text = "水平居中";
sheet.range["b4"].style.horizontalalignment = horizontalaligntype.center;
//右对齐
sheet.range["b5"].text = "右对齐";
sheet.range["b5"].style.horizontalalignment = horizontalaligntype.right;
//居上
sheet.range["b7"].text = "居上";
sheet.range["b7"].style.verticalalignment = verticalaligntype.top;
//居中
sheet.range["b8"].text = "垂直居中";
sheet.range["b8"].style.verticalalignment = verticalaligntype.center;
//居下
sheet.range["b9"].text = "居下";
sheet.range["b9"].style.verticalalignment = verticalaligntype.bottom;
//分散对齐并居中
sheet.range["b10"].text = "水平分散对齐 垂直居中";
sheet.range["b10"].style.horizontalalignment = horizontalaligntype.distributed;
sheet.range["b10"].style.verticalalignment = verticalaligntype.center;
//逆时针旋转45°
sheet.range["d7"].text = "旋转45°";
sheet.range["d7"].style.rotation = 45;
//逆时针旋转90°
sheet.range["d8"].text = "旋转90°";
sheet.range["d8"].style.rotation = 90;
//顺时针旋转45°
sheet.range["d9"].text = "旋转-45°";
sheet.range["d9"].style.rotation = 135;
//顺时针旋转90°
sheet.range["d10"].text = "旋转-90°";
sheet.range["d10"].style.rotation = 180;
//添加‘\n’进行文字换行
sheet.range["f9"].text = "这是\n手动\n换行";
//自动换行
sheet.range["f10"].text = "这是自动换行(额外测试文字)";
sheet.range["f10"].style.wraptext = true;
//设置列宽、行高
sheet.columns[1].columnwidth = 15;
sheet.columns[3].columnwidth = 15;
sheet.columns[5].columnwidth = 15;
sheet.range["b3:b5"].rowheight = 15;
sheet.range["b7:b10"].rowheight = 50;
//保存文档
wookbook.savetofile("textalignment.xlsx", fileformat.version2013);
vb.net
'创建workbook对象
dim wookbook as new workbook()
dim sheet as worksheet = wookbook.worksheets(0)
'添加说明文字
sheet.range("b1").text = "文字对齐方式"
sheet.range("d1").text = "文字方向"
sheet.range("f1").text = "文字换行"
sheet.range("b1:f1").style.font.isbold = true
'左对齐
sheet.range("b3").text = "左对齐"
sheet.range("b3").style.horizontalalignment = horizontalaligntype.left
'水平居中
sheet.range("b4").text = "水平居中"
sheet.range("b4").style.horizontalalignment = horizontalaligntype.center
'右对齐
sheet.range("b5").text = "右对齐"
sheet.range("b5").style.horizontalalignment = horizontalaligntype.right
'居上
sheet.range("b7").text = "居上"
sheet.range("b7").style.verticalalignment = verticalaligntype.top
'居中
sheet.range("b8").text = "垂直居中"
sheet.range("b8").style.verticalalignment = verticalaligntype.center
'居下
sheet.range("b9").text = "居下"
sheet.range("b9").style.verticalalignment = verticalaligntype.bottom
'分散对齐并居中
sheet.range("b10").text = "水平分散对齐 垂直居中"
sheet.range("b10").style.horizontalalignment = horizontalaligntype.distributed
sheet.range("b10").style.verticalalignment = verticalaligntype.center
'逆时针旋转45°
sheet.range("d7").text = "旋转45°"
sheet.range("d7").style.rotation = 45
'逆时针旋转90°
sheet.range("d8").text = "旋转90°"
sheet.range("d8").style.rotation = 90
'顺时针旋转45°
sheet.range("d9").text = "旋转-45°"
sheet.range("d9").style.rotation = 135
'顺时针旋转90°
sheet.range("d10").text = "旋转-90°"
sheet.range("d10").style.rotation = 180
'添加‘\n’进行文字换行
sheet.range("f9").text = "这是" & vblf & "手动" & vblf & "换行"
'自动换行
sheet.range("f10").text = "这是自动换行(额外测试文字)"
sheet.range("f10").style.wraptext = true
'设置列宽、行高
sheet.columns(1).columnwidth = 15
sheet.columns(3).columnwidth = 15
sheet.columns(5).columnwidth = 15
sheet.range("b3:b5").rowheight = 15
sheet.range("b7:b10").rowheight = 50
'保存文档
wookbook.savetofile("textalignment.xlsx", fileformat.version2013)