VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > VB.net教程 >
  • 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  
复制代码

 

 出处:https://www.cnblogs.com/Yesi/p/7569580.html

相关教程