C#/VB.NET設置Excel表格背景色


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

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM