博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C#/VB.NET设置Excel表格背景色
阅读量:6158 次
发布时间:2019-06-21

本文共 3445 字,大约阅读时间需要 11 分钟。

在查看很多有复杂的数据的表格时,为了能够快速地找到所需要的数据组时,往往需要对该数据组进行分类,一个简单快速的方法就是对数据组所在的单元格填充背景颜色,这样就使得我们在阅读文件时能够直观的看到数据分类,既美观又实用。那对于开发者而言,我这里提供一个简单快速的方法,即用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.DrawingImports Spire.XlsNamespace 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 ClassEnd Namespace

 

 

 

转载地址:http://rasfa.baihongyu.com/

你可能感兴趣的文章
CF 888E Maximum Subsequence——折半搜索
查看>>
欧几里德算法(辗转相除法)
查看>>
面试题1-----SVM和LR的异同
查看>>
MFC控件的SubclassDlgItem
查看>>
如何避免历史回退到登录页面
查看>>
《图解HTTP》1~53Page Web网络基础 HTTP协议 HTTP报文内的HTTP信息
查看>>
unix环境高级编程-高级IO(2)
查看>>
树莓派是如何免疫 Meltdown 和 Spectre 漏洞的
查看>>
雅虎瓦片地图切片问题
查看>>
HTML 邮件链接,超链接发邮件
查看>>
HDU 5524:Subtrees
查看>>
手机端userAgent
查看>>
pip安装Mysql-python报错EnvironmentError: mysql_config not found
查看>>
http协议组成(请求状态码)
查看>>
怎样成为一个高手观后感
查看>>
[转]VC预处理指令与宏定义的妙用
查看>>
MySql操作
查看>>
python 解析 XML文件
查看>>
MySQL 文件导入出错
查看>>
java相关
查看>>