在查看很多有複雜的數據的表格時,為了能夠快速地找到所需要的數據組時,往往需要對該數據組進行分類,一個簡單快速的方法就是對數據組所在的單元格填充背景顏色,這樣就使得我們在閱讀文件時能夠直觀的看到數據分類,既美觀又實用。那對於開發者而言,我這裡提供一個簡單快速的方法,即用C#給單元格設置背景顏色。下麵是 ...
在查看很多有複雜的數據的表格時,為了能夠快速地找到所需要的數據組時,往往需要對該數據組進行分類,一個簡單快速的方法就是對數據組所在的單元格填充背景顏色,這樣就使得我們在閱讀文件時能夠直觀的看到數據分類,既美觀又實用。那對於開發者而言,我這裡提供一個簡單快速的方法,即用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