重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
在查看很多有复杂的数据的表格时,为了能够快速地找到所需要的数据组时,往往需要对该数据组进行分类,一个简单快速的方法就是对数据组所在的单元格填充背景颜色,这样就使得我们在阅读文件时能够直观的看到数据分类,既美观又实用。那对于开发者而言,我这里提供一个简单快速的方法,即用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