C#/VB.NET 对Excel单元格填充背景色

阅读更多

我们知道,在处理很多表格时会遇到各种各样纷繁复杂的数据,数据较多时,如果是在没有进行数据分类的情况下,想要快速地辨别出不同类别的数据可能会花费不少时间,而且难免也会出错。在众多分类数据的方法中,对数据表格中的单元格进行颜色填充可以说是比较简单粗暴的了,同时,也可以美化表格,让人看数据时也没那么枯燥乏味。

那么,在C#语言环境中,如何来通过代码步骤实现对表哥单元格的颜色填充呢,下面的例这个操作方法给出了答案!文章转载自http://www.cnblogs.com/Yesi/p/7569580.html ,有兴趣可以自行查看详细内容。

 

说明:操作中使用到了组件Spire.XLS for .NET,写入代码前,需要先下载安装,并添加引用dll文件

 

操作前:


C#/VB.NET 对Excel单元格填充背景色_第1张图片
 

操作后:

C#/VB.NET 对Excel单元格填充背景色_第2张图片
 

 

   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 

 

 

  • C#/VB.NET 对Excel单元格填充背景色_第3张图片
  • 大小: 21.2 KB
  • C#/VB.NET 对Excel单元格填充背景色_第4张图片
  • 大小: 22.9 KB
  • 查看图片附件

你可能感兴趣的:(C#,VB.NET,Excel,背景颜色填充)