C# 如何设置Excel上、下标
1、下载安装Spire.xls后,注意添加引用Spire.xls.dll到程序设计中,dll文件可在安装路径下的Bin文件夹中获取。

1、using Spire.Xls;
using System.Drawing;
namespace SubscriptAndSuperscript_XLS
{
class Program
{
static void Main(string[] args)
{
//创建工作簿,获取第一个工作表
Workbook workbook = new Workbook();
Worksheet sheet = workbook.Worksheets[0];
//添加文本内容到指定单元格
sheet.Range["B2"].Text = "This is an example of Subscript:";
sheet.Range["D2"].Text = "This is an example of Superscript:";
//添加字符串到指定单元格
CellRange range = sheet.Range["B3"];
range.RichText.Text = "R100-0.06";
ExcelFont font = workbook.CreateFont();//创建字体
font.IsSubscript = true;//设置字体下标的属性
font.Color = Color.Green;//设置下标字体的颜色
range.RichText.SetFont(4, 8, font);//指定作为下标的字符串
//添加字符串到指定单元格
range = sheet.Range["D3"];
range.RichText.Text = "a2 + b2 = c2";
font = workbook.CreateFont();
font.IsSuperscript = true;//设置字体的上标属性
range.RichText.SetFont(1, 1, font);//指定作为上标的字符串
range.RichText.SetFont(6, 6, font);
range.RichText.SetFont(11, 11, font);
sheet.AllocatedRange.AutoFitColumns();//设置自适应列宽
//保存并打开文档
workbook.SaveToFile("result.xls");
System.Diagnostics.Process.Start("result.xls");
}
}
}
