当前位置: 移动技术网 > IT编程>开发语言>.net > C# 应用Excel条件格式(一)

C# 应用Excel条件格式(一)

2018年08月17日  | 移动技术网IT编程  | 我要评论

南宁公交,底漆,猎隼大队

excel中的条件格式功能是个十分强大且方便的功能,通过对使用条件格式功能可以在很大程度上改进表格的设计和可读性,用户可以指定单个或者多个单元格区域应用一种或者多种格式,如此一来,也在大大提高了表格的可操作性。下面将介绍在c#编程中如何来设置并应用excel条件格式。

示例要点概述:

1. 基于单元格值应用条件格式

2. 基于自定义公式应用条件格式

3. 应用数据条条件类型格式

4. 删除条件格式

  4.1 删除指定数据范围中的条件格式

  4.2 删除全部条件格式

使用工具

 

示例代码(供参考)

测试文档如下:

【示例 1 】应用条件格式

using spire.xls;
using system.drawing;

namespace conditionalformatting_xls
{
    class program
    {
        static void main(string[] args)
        {
            //实例化workbook对象并加载文档
            workbook wb = new workbook();
            wb.loadfromfile("sample.xlsx");

            //获取第一个工作表
            worksheet sheet = wb.worksheets[0];

            //获取数据范围
            cellrange range = sheet.range["a2:h27"];

            //在所选范围添加条件格式1
            conditionalformatwrapper format1 = range.conditionalformats.addcondition();

            //条件格式类型1基于单元格值
            format1.formattype = conditionalformattype.cellvalue;
            //将数值在60到90之间的单元格进行字体加粗,并设置字体颜色为橙色
            format1.firstformula = "60";
            format1.secondformula = "90";
            format1.operator = comparisonoperatortype.between;
            format1.fontcolor = color.orange;
            //format1.backcolor = color.orange;

            //添加条件格式2
            conditionalformatwrapper format2 = range.conditionalformats.addcondition();
            format2.formattype = conditionalformattype.cellvalue;
            format2.firstformula = "60";
            format2.operator = comparisonoperatortype.less;
            format2.fontcolor = color.red;
            //format2.backcolor = color.red;
            format2.isbold = true;
            //添加边框格式(边框颜色、边框类型)到条件格式2
            format2.leftbordercolor = color.red;
            format2.rightbordercolor = color.darkblue;
            format2.topbordercolor = color.deepskyblue;
            format2.bottombordercolor = color.deepskyblue;
            format2.leftborderstyle = linestyletype.medium;
            format2.rightborderstyle = linestyletype.thick;
            format2.topborderstyle = linestyletype.double;
            format2.bottomborderstyle = linestyletype.double;

            //条件格式3的类型为自定义公式
            conditionalformatwrapper format3 = range.conditionalformats.addcondition();
            format3.formattype = conditionalformattype.formula;

            //自定义公式将低于60的单元格所在的行填充背景色
            format3.firstformula = "=or($c2<60,$d2<60,$e2<60,$f2<60,$g2<60,$h2<60)";
            format3.backcolor = color.gray;

            //保存并打开文档
            wb.savetofile("result.xlsx", excelversion.version2013);
            system.diagnostics.process.start("result.xlsx");
        }
    }
}

调试运行程序,生成文档,如下:

【示例2】应用数据条类型的条件格式

using spire.xls;
using system.drawing;

namespace conditionalformatting_xls
{
    class program
    {
        static void main(string[] args)
        {
            //实例化workbook对象并加载文档
            workbook wb = new workbook();
            wb.loadfromfile("sample.xlsx");

            //获取第2个工作表
            worksheet sheet = wb.worksheets[1];

            //获取数据范围
            cellrange range = sheet.range["b2:d7"];

            //添加条件类型4为data bars
            conditionalformatwrapper format4 = sheet.allocatedrange.conditionalformats.addcondition();
            format4.formattype = conditionalformattype.databar;
            format4.databar.barcolor = color.forestgreen;

            //保存并打开文档
            wb.savetofile("result1.xlsx", excelversion.version2013);
            system.diagnostics.process.start("result1.xlsx");  
        }
    }
}

测试结果:

【示例3】删除条件格式

using spire.xls;

namespace removeconditionalformat_xls
{
    class program
    {
        static void main(string[] args)
        {
            //实例化workbook类,加载测试文档
            workbook workbook = new workbook();
            workbook.loadfromfile("test.xlsx");

            //获取第一个工作表
            worksheet sheet = workbook.worksheets[0];
            //删除指定区域的条件格式
            //sheet.range["a5:h5"].conditionalformats.remove();

            //删除表格中的所有条件格式
            sheet.allocatedrange.conditionalformats.remove();

            //保存并打开文档
            workbook.savetofile("result1.xlsx", excelversion.version2010);
            system.diagnostics.process.start("result1.xlsx");
        }
    }
}

 删除效果

1. 删除指定数据范围的条件格式

2. 删除全部条件格式

本次关于“c# 应用条件格式到excel”的示例方法介绍到此。

如需转载,请注明出处。

 

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网