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

在查看不少有複雜的數據的表格時,爲了可以快速地找到所須要的數據組時,每每須要對該數據組進行分類,一個簡單快速的方法就是對數據組所在的單元格填充背景顏色,這樣就使得咱們在閱讀文件時可以直觀的看到數據分類,既美觀又實用。那對於開發者而言,我這裏提供一個簡單快速的方法,即用C#給單元格設置背景顏色。下面是個人操做方法,供參考。我這裏使用了一個叫Spire.XLS for . NET的免費版組件,該產品由E-iceblue公司發佈,具體詳情可上官網瞭解(https://www.e-iceblue.cn/Introduce/Spire-XLS-NET.html),不廢話,下面我演示一下個人操做。html

原文件如圖:spa

 

步驟:code

一、 添加命名空間htm

Using Spire.Xls;
Using System.Drawing;

二、 初始化工做簿並加載文檔blog

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);

完成以上步驟後文檔背景顏色添加成功,以下圖所示:string

 

完整代碼:產品

C#io

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  
相關文章
相關標籤/搜索