C# 如何设置Excel上、下标

2025-04-21 15:55:26

此条经验将介绍如何通过C#编程来设置Excel表格数据的上标、下标。

工具/原料

Free Spire.XLS for .NET 8.3(社区版)

Visual Studio

Dll引用

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

C# 如何设置Excel上、下标

C#代码示例(供参考)

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"); } }}

C# 如何设置Excel上、下标
声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢