在microsoft excel中,我们可以通过设置单元格格式来设置excel的字体,实现对excel 字体,字形,字号,下划线,颜色及删除线,上下标等特殊效果的设置。spire.xls提供excelfont 对象让程序员可以很简单的完成对excel单元格里的字体进行设置。
本文将详细描述如何使用spire.xls设置excel字体和设置excel上标和下标。
c#
{
//实例化一个workbook并获取第一个工作表
workbook workbook = new workbook();
worksheet sheet = workbook.worksheets[0];
//创建字体
excelfont font1 = workbook.createfont();
//设置字体,字形,大小,颜色
font1.fontname = "宋体";
font1.isbold = true;
font1.size = 10;
font1.knowncolor = excelcolors.blue;
//为a1单元格写入数据并设置字体
richtext richtext = sheet.range["a1"].richtext;
richtext.text = "excel字体设置";
richtext.setfont(0, richtext.text.toarray().length - 1, font1);
//设置excel上标
excelfont font2 = workbook.createfont();
font2.issuperscript = true;
richtext richtext2 = sheet.range["a2"].richtext;
richtext2.text = "a2 b2=c2";
richtext2.setfont(1, 1, font2);
richtext2.setfont(4, 4, font2);
richtext2.setfont(7, 7, font2);
//设置excel下标
excelfont font3 = workbook.createfont();
font3.issubscript = true;
richtext richtext3 = sheet.range["a3"].richtext;
richtext3.text = "h2o";
richtext3.setfont(1, 1, font3);
//保存文档
workbook.savetofile("result.xlsx",fileformat.version2010);
}
vb.net
'实例化一个workbook并获取第一个工作表
dim workbook as new workbook()
dim sheet as worksheet = workbook.worksheets(0)
'创建字体
dim font1 as excelfont = workbook.createfont()
'设置字体,字形,大小,颜色
font1.fontname = "宋体"
font1.isbold = true
font1.size = 10
font1.knowncolor = excelcolors.blue
'为a1单元格写入数据并设置字体
dim richtext as richtext = sheet.range("a1").richtext
richtext.text = "excel字体设置"
richtext.setfont(0, richtext.text.toarray().length - 1, font1)
'设置excel上标
dim font2 as excelfont = workbook.createfont()
font2.issuperscript = true
dim richtext2 as richtext = sheet.range("a2").richtext
richtext2.text = "a2 b2=c2"
richtext2.setfont(1, 1, font2)
richtext2.setfont(4, 4, font2)
richtext2.setfont(7, 7, font2)
'设置excel下标
dim font3 as excelfont = workbook.createfont()
font3.issubscript = true
dim richtext3 as richtext = sheet.range("a3").richtext
richtext3.text = "h2o"
richtext3.setfont(1, 1, font3)
'保存文档
workbook.savetofile("result.xlsx", fileformat.version2010)