您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
在查看很多有复杂的数据的表格时,为了能够快速地找到所需要的数据组时,往往需要对该数据组进行分类,一个简单快速的方法就是对数据组所在的单元格填充背景颜色,这样就使得我们在阅读文件时能够直观的看到数据分类,既美观又实用。那对于开发者而言,我这里提供一个简单快速的方法,即用C#给单元格设置背景颜色。下面是我的操作方法,供参考。我这里使用了一个叫Spire.XLS for . NET的免费版组件,该产品由E-iceblue公司发布,具体详情可上官网了解(https://www.e-iceblue.cn/Introduce/Spire-XLS-NET.html),不废话,下面我演示一下我的操作。
原文件如图:
步骤:
1、添加命名空间
Using Spire.Xls; Using System.Drawing;
2、初始化工作簿并加载文档
Workbook workbook =new Workbook(); workbook.LoadFromFile(@"C:\Users\Administrator\Desktop\test.xlsx",ExcelVersion.Version97to2003); Worksheet worksheet =workbook.Worksheets[0];Worksheet worksheet = workbook.Worksheets[0];
3、设置单元格背景颜色
worksheet.Range["A1:C2"].Style.Color= Color.LightSeaGreen; worksheet.Range["A3:C4"].Style.Color= Color.LightYellow; worksheet.Range["A5:C19"].Style.Color= Color.SpringGreen; worksheet.Range["A20:C21"].Style.Color = Color.DeepSkyBlue; worksheet.Range["A22:C23"].Style.Color = Color.Yellow
4、保存并预览文件
workbook.SaveToFile("GradesRank.xls",ExcelVersion.Version97to2003; System.Diagnostics.Process.Start(workbook.FileName);
完成以上步骤后文档背景颜色添加成功,如下图所示:
完整代码:
C#
using System.Drawing; using Spire.Xls; namespace background_color { class Program { static void Main(string[] args) { Workbook workbook = new Workbook(); workbook.LoadFromFile(@"C:\Users\Administrator\Desktop\test.xlsx", ExcelVersion.Version97to2003); Worksheet worksheet = workbook.Worksheets[0]; //set the backgroundcolor of Range["A1:C2"] worksheet.Range["A1:C2"].Style.Color = Color.LightSeaGreen; //set the backgroundcolor of Range["A3:C4"] worksheet.Range["A3:C4"].Style.Color = Color.LightYellow; //set the backgroundcolor of Range["A5:C19"] worksheet.Range["A5:C19"].Style.Color = Color.SpringGreen; //set the backgroundcolor of Range["A20:C21"] worksheet.Range["A20:C21"].Style.Color = Color.DeepSkyBlue; //set the backgroundcolor of Range["A22:C23"] worksheet.Range["A22:C23"].Style.Color = Color.Yellow; //save and launch the project workbook.SaveToFile("Sample.xls", ExcelVersion.Version97to2003); System.Diagnostics.Process.Start(workbook.FileName); } } }
VB.NET
Imports System.Drawing Imports Spire.Xls Namespace background_color Class Program Private Shared Sub Main(ByVal args() As String) Dim workbook As Workbook = New Workbook workbook.LoadFromFile("C:\Users\Administrator\Desktop\test.xlsx", ExcelVersion.Version97to2003) Dim worksheet As Worksheet = workbook.Worksheets(0) 'set the backgroundcolor of Range["A1:C2"] worksheet.Range("A1:C2").Style.Color = Color.LightSeaGreen 'set the backgroundcolor of Range["A3:C4"] worksheet.Range("A3:C4").Style.Color = Color.LightYellow 'set the backgroundcolor of Range["A5:C19"] worksheet.Range("A5:C19").Style.Color = Color.SpringGreen 'set the backgroundcolor of Range["A20:C21"] worksheet.Range("A20:C21").Style.Color = Color.DeepSkyBlue 'set the backgroundcolor of Range["A22:C23"] worksheet.Range("A22:C23").Style.Color = Color.Yellow 'save and launch the project workbook.SaveToFile("Sample.xls", ExcelVersion.Version97to2003) System.Diagnostics.Process.Start(workbook.FileName) End Sub End Class End Namespace
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。