在查看很多有復(fù)雜的數(shù)據(jù)的表格時,為了能夠快速地找到所需要的數(shù)據(jù)組時,往往需要對該數(shù)據(jù)組進行分類,一個簡單快速的方法就是對數(shù)據(jù)組所在的單元格填充背景顏色,這樣就使得我們在閱讀文件時能夠直觀的看到數(shù)據(jù)分類,既美觀又實用。那對于開發(fā)者而言,我這里提供一個簡單快速的方法,即用C#給單元格設(shè)置背景顏色。下面是我的操作方法,供參考。我這里使用了一個叫Spire.XLS for . NET的免費版組件,該產(chǎn)品由E-iceblue公司發(fā)布,具體詳情可上官網(wǎng)了解(https://www.e-iceblue.cn/Introduce/Spire-XLS-NET.html),不廢話,下面我演示一下我的操作。
成都創(chuàng)新互聯(lián)公司專業(yè)為企業(yè)提供平湖網(wǎng)站建設(shè)、平湖做網(wǎng)站、平湖網(wǎng)站設(shè)計、平湖網(wǎng)站制作等企業(yè)網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計與制作、平湖企業(yè)網(wǎng)站模板建站服務(wù),十載平湖做網(wǎng)站經(jīng)驗,不只是建網(wǎng)站,更提供有價值的思路和整體網(wǎng)絡(luò)服務(wù)。
原文件如圖:
步驟:
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、設(shè)置單元格背景顏色
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、保存并預(yù)覽文件
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