上下标常见于数学公式、化学表达式等科学公式中。本文将介绍如何使用spire.xls在excel中插入上标和下标。
c#
//创建workbook实例
workbook workbook = new workbook();
//获取第一张工作表
worksheet sheet = workbook.worksheets[0];
//向单元格“a2”中写入数学公式
cellrange range = sheet.range["a2"];
range.richtext.text = "a2 b2=c2";
//设置上标
excelfont font = workbook.createfont();
font.issuperscript = true;
font.color = color.red;
range.richtext.setfont(1, 1, font);
range.richtext.setfont(4, 4, font);
range.richtext.setfont(7, 7, font);
//向单元格“a4”中写入化学方程式
range = sheet.range["a4"];
range.richtext.text = "2na 2h2o=2naoh h2";
//设置下标
font = workbook.createfont();
font.issubscript = true;
font.color = color.forestgreen;
range.richtext.setfont(6, 6, font);
range.richtext.setfont(16, 16, font);
//自适应列宽
sheet.range.autofitcolumns();
//保存文档
workbook.savetofile("output.xlsx", excelversion.version2013);
vb.net
'创建workbook实例
dim workbook as new workbook()
'获取第一张工作表
dim sheet as worksheet = workbook.worksheets(0)
'向单元格“a2”中写入数学公式
dim range as cellrange = sheet.range("a2")
range.richtext.text = "a2 b2=c2"
'设置上标
dim font as excelfont = workbook.createfont()
font.issuperscript = true
font.color = color.red
range.richtext.setfont(1, 1, font)
range.richtext.setfont(4, 4, font)
range.richtext.setfont(7, 7, font)
'向单元格“a4”中写入化学方程式
range = sheet.range("a4")
range.richtext.text = "2na 2h2o=2naoh h2"
'设置下标
font = workbook.createfont()
font.issubscript = true
font.color = color.forestgreen
range.richtext.setfont(6, 6, font)
range.richtext.setfont(16, 16, font)
'自适应列宽
sheet.range.autofitcolumns()
'保存文档
workbook.savetofile("output.xlsx", excelversion.version2013)
效果图: