在查看很多有復(fù)雜的數(shù)據(jù)的表格時(shí),為了能夠快速地找到所需要的數(shù)據(jù)組時(shí),往往需要對(duì)該數(shù)據(jù)組進(jìn)行分類,一個(gè)簡(jiǎn)單快速的方法就是對(duì)數(shù)據(jù)組所在的單元格填充背景顏色,這樣就使得我們?cè)陂喿x文件時(shí)能夠直觀的看到數(shù)據(jù)分類,既美觀又實(shí)用。那對(duì)于開(kāi)發(fā)者而言,我這里提供一個(gè)簡(jiǎn)單快速的方法,即用C#給單元格設(shè)置背景顏色。下面是我的操作方法,供參考。我這里使用了一個(gè)叫Spire.XLS for . NET的免費(fèi)版組件,該產(chǎn)品由E-iceblue公司發(fā)布,具體詳情可上官網(wǎng)了解(https://www.e-iceblue.cn/Introduce/Spire-XLS-NET.html),不廢話,下面我演示一下我的操作。
讓客戶滿意是我們工作的目標(biāo),不斷超越客戶的期望值來(lái)自于我們對(duì)這個(gè)行業(yè)的熱愛(ài)。我們立志把好的技術(shù)通過(guò)有效、簡(jiǎn)單的方式提供給客戶,將通過(guò)不懈努力成為客戶在信息化領(lǐng)域值得信任、有價(jià)值的長(zhǎng)期合作伙伴,公司提供的服務(wù)項(xiàng)目有:域名與空間、虛擬主機(jī)、營(yíng)銷軟件、網(wǎng)站建設(shè)、坡頭網(wǎng)站維護(hù)、網(wǎng)站推廣。原文件如圖:
步驟:
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
創(chuàng)新互聯(lián)www.cdcxhl.cn,專業(yè)提供香港、美國(guó)云服務(wù)器,動(dòng)態(tài)BGP最優(yōu)骨干路由自動(dòng)選擇,持續(xù)穩(wěn)定高效的網(wǎng)絡(luò)助力業(yè)務(wù)部署。公司持有工信部辦法的idc、isp許可證, 機(jī)房獨(dú)有T級(jí)流量清洗系統(tǒng)配攻擊溯源,準(zhǔn)確進(jìn)行流量調(diào)度,確保服務(wù)器高可用性。佳節(jié)活動(dòng)現(xiàn)已開(kāi)啟,新人活動(dòng)云服務(wù)器買多久送多久。