跳至页脚内容
使用 IRONXL

如何在 Excel 中突出显示每隔一行

Microsoft Excel 提供了一个内置功能,可以根据用户的需要突出显示行。 要在 Excel 中突出显示行,可以使用"突出显示单元格"操作。 Excel 中的突出显示行功能是一种简单的方法,可以让文档看起来更有趣。 此工具对于显示数据点行之间的差异非常有用。 但是,在 Microsoft Excel 中突出显示奇数行可能会很棘手,对于初学者来说并不容易。 然而,您可以很容易地为交替行着色。 我们将逐步解释。

让我们开始吧:

  1. 选择您要格式化的行。
  2. 从主菜单中点击主页,然后点击格式化为表
  3. 从表对话框中选择具有交替行着色的表样式。
  4. 现在,如果要将着色从行更改为列,选择表格并从表样式选项组中点击设计。然后,从下拉菜单中取消选择条纹行框,并选择条纹列框。

如何突出显示 Excel 中的每隔一行,图 1:导航到 Excel 中的条纹列功能 导航到 Excel 中的条纹列功能

如果您想保留表格布局,但不需要其功能,您可以将其转换为数据范围。 如果您正在添加颜色行/列并具有条纹,则不会自动复制。 然而,您可以通过使用格式刷复制具有交替格式的行或列来重新创建格式。

使用条件格式应用条纹行或列

您还可以使用条件格式规则对特定行或列应用不同的格式。 以下是我们可以做到的方法:

  • 在工作表上,执行以下操作之一:
  • 如果要对某些单元格应用格式,选择单元格或行的范围。 如果您想将格式应用于整个文档,请按 Ctrl+A 选择整个文档。
  • 转到 主页 > 条件格式 > 新规则。 这将打开格式规则对话框。

如何突出显示 Excel 中的每隔一行,图 2:为条件格式设置新的规则 为条件格式设置新的规则

  • "选择规则类型"部分,选择"使用公式来确定格式化的单元格"选项。
  • 在"编辑规则说明"部分中编写以下公式,为交替行应用颜色。
=MOD(ROW(),2)=0

要为交替列应用颜色,请输入此公式:=MOD(COLUMN(),2)=0

这些公式确定行或列是偶数还是奇数,并相应地应用所需的格式。

如何突出显示 Excel 中的每隔一行,图 3:新的格式规则对话框 新的格式规则对话框

  1. 点击格式
  2. 格式化单元格框中,点击填充
  3. 选择一种颜色并点击确定
  4. 您可以在样本下预览您的选择,并点击确定或选择另一种颜色。

通过实施以下步骤,您将看到交替行突出显示。

IronXL:C# Excel 库

IronXL 是一个 .NET Excel 库,为开发人员提供了一组使用 Excel 的工具。 该库可用于读取、写入和修改 Excel 文件和工作表。 它还可以用于在不同文件格式之间转换,例如 XML、JSON、HTML 和 CSV。IronXL 库是用 C# 编程语言开发的,并且是开源的,这意味着开发人员可以自由地在他们的应用程序中使用它。 IronXL Excel 库可以用于任何需要访问 Microsoft Office Excel 的 .NET 项目。 开发人员可以在他们的项目中使用 IronXL,而无需在他们的计算机上安装 Office 或从 Microsoft 设置开发人员许可证。

IronXL Excel 库提供了以下功能:

  • 广泛的功能集,包括数据处理、数据导出、数据导入和从其他来源导入。
  • 支持所有最新版本的 Microsoft Excel。
  • 支持 Excel 最流行的文件格式(.xlsx)。
  • 支持单元格格式化,例如文本对齐、字体大小、颜色边框等。
  • 控制 Excel 工作簿中单元格显示方式的能力(例如网格线)。

我们可以使用 IronXL 进行条件格式设置。 要使用 IronXL,您必须在您的 C# 项目中安装 IronXL 库。 安装库后,您必须添加 IronXL 命名空间。 在程序文件顶部编写以下代码行:

using IronXL;
using IronXL.Formatting;
using IronXL.Formatting.Enums;
using IronXL.Styles;
using IronXL;
using IronXL.Formatting;
using IronXL.Formatting.Enums;
using IronXL.Styles;
Imports IronXL
Imports IronXL.Formatting
Imports IronXL.Formatting.Enums
Imports IronXL.Styles
$vbLabelText   $csharpLabel

之后,在您的主要函数中添加以下代码行:

// Load the Excel workbook
WorkBook workbook = WorkBook.Load("test.xlsx");

// Access the default worksheet
WorkSheet sheet = workbook.DefaultWorkSheet;

// Create a specific conditional formatting rule for values less than 8
ConditionalFormattingRule rule = sheet.ConditionalFormatting.CreateConditionalFormattingRule(ComparisonOperator.LessThan, "8");

// Set different style options for the rule
rule.FontFormatting.IsBold = true;
rule.FontFormatting.FontColor = "#123456";
rule.BorderFormatting.RightBorderColor = "#ffffff";
rule.BorderFormatting.RightBorderType = BorderType.Thick;
rule.PatternFormatting.BackgroundColor = "#54bdd9";
rule.PatternFormatting.FillPattern = FillPattern.Diamonds;

// Add the formatting rule to a specific region in the worksheet
sheet.ConditionalFormatting.AddConditionalFormatting("A3:A8", rule);

// Create another conditional formatting rule for values between 7 and 10
ConditionalFormattingRule rule1 = sheet.ConditionalFormatting.CreateConditionalFormattingRule(ComparisonOperator.Between, "7", "10");

// Set additional style options for the new rule
rule1.FontFormatting.IsItalic = true;
rule1.FontFormatting.UnderlineType = FontUnderlineType.Single;

// Add the second formatting rule to another region in the worksheet
sheet.ConditionalFormatting.AddConditionalFormatting("A3:A9", rule1);

// Save the updated workbook
workbook.SaveAs("ApplyConditionalFormatting.xlsx");
// Load the Excel workbook
WorkBook workbook = WorkBook.Load("test.xlsx");

// Access the default worksheet
WorkSheet sheet = workbook.DefaultWorkSheet;

// Create a specific conditional formatting rule for values less than 8
ConditionalFormattingRule rule = sheet.ConditionalFormatting.CreateConditionalFormattingRule(ComparisonOperator.LessThan, "8");

// Set different style options for the rule
rule.FontFormatting.IsBold = true;
rule.FontFormatting.FontColor = "#123456";
rule.BorderFormatting.RightBorderColor = "#ffffff";
rule.BorderFormatting.RightBorderType = BorderType.Thick;
rule.PatternFormatting.BackgroundColor = "#54bdd9";
rule.PatternFormatting.FillPattern = FillPattern.Diamonds;

// Add the formatting rule to a specific region in the worksheet
sheet.ConditionalFormatting.AddConditionalFormatting("A3:A8", rule);

// Create another conditional formatting rule for values between 7 and 10
ConditionalFormattingRule rule1 = sheet.ConditionalFormatting.CreateConditionalFormattingRule(ComparisonOperator.Between, "7", "10");

// Set additional style options for the new rule
rule1.FontFormatting.IsItalic = true;
rule1.FontFormatting.UnderlineType = FontUnderlineType.Single;

// Add the second formatting rule to another region in the worksheet
sheet.ConditionalFormatting.AddConditionalFormatting("A3:A9", rule1);

// Save the updated workbook
workbook.SaveAs("ApplyConditionalFormatting.xlsx");
' Load the Excel workbook
Dim workbook As WorkBook = WorkBook.Load("test.xlsx")

' Access the default worksheet
Dim sheet As WorkSheet = workbook.DefaultWorkSheet

' Create a specific conditional formatting rule for values less than 8
Dim rule As ConditionalFormattingRule = sheet.ConditionalFormatting.CreateConditionalFormattingRule(ComparisonOperator.LessThan, "8")

' Set different style options for the rule
rule.FontFormatting.IsBold = True
rule.FontFormatting.FontColor = "#123456"
rule.BorderFormatting.RightBorderColor = "#ffffff"
rule.BorderFormatting.RightBorderType = BorderType.Thick
rule.PatternFormatting.BackgroundColor = "#54bdd9"
rule.PatternFormatting.FillPattern = FillPattern.Diamonds

' Add the formatting rule to a specific region in the worksheet
sheet.ConditionalFormatting.AddConditionalFormatting("A3:A8", rule)

' Create another conditional formatting rule for values between 7 and 10
Dim rule1 As ConditionalFormattingRule = sheet.ConditionalFormatting.CreateConditionalFormattingRule(ComparisonOperator.Between, "7", "10")

' Set additional style options for the new rule
rule1.FontFormatting.IsItalic = True
rule1.FontFormatting.UnderlineType = FontUnderlineType.Single

' Add the second formatting rule to another region in the worksheet
sheet.ConditionalFormatting.AddConditionalFormatting("A3:A9", rule1)

' Save the updated workbook
workbook.SaveAs("ApplyConditionalFormatting.xlsx")
$vbLabelText   $csharpLabel

我们可以使用CreateConditionalFormattingRule()函数定义条件格式规则。 使用 IronXL 很容易编辑字体样式和边框样式。 可以通过使用行函数格式化行。 同时,在参数中,您将给出行号和要应用的格式。 您可以选择突出显示每第 n 行。 您可以在我们的条件格式支持代码示例中查看更多详细信息。

IronXL 可供开发免费使用。 不过,在开发阶段会有一个水印。 您可以在生产中免费试用。 您可以在不需要任何付款或卡信息的情况下激活IronXL 免费试用。 之后,您可以购买它。 有不同的定价计划可供选择,您可以根据您的需要选择,访问此许可页面以获取更多信息。

常见问题解答

如何使用 Excel 内置功能突出显示每隔一行?

您可以使用 Excel 的“格式化为表格”功能来突出显示每隔一行。只需选择要格式化的单元格范围,转到“主页”,单击“格式化为表格”,然后选择具有交替行着色的表格样式。

如何应用条件格式以突出显示 Excel 中的交替行?

要使用条件格式突出显示交替行,选择您的单元格,导航到“主页” > “条件格式” > “新建规则”,并输入公式=MOD(ROW(),2)=0。这将把格式应用于每隔一行。

使用 .NET Excel 库进行行高亮有什么好处?

使用像 IronXL 这样的 .NET Excel 库可以让开发人员以编程方式应用条件格式,包括突出显示每隔一行,而无需手动与 Excel 互动。这可以显著提高生产力并自动化重复任务。

如何在 C# 中自动化 Excel 文件操作?

IronXL 是一个 .NET 库,专为以编程方式读取、写入和修改 Excel 文件而设计。它允许自动化任务,例如突出显示行、应用条件格式和在不同 Excel 文件格式之间转换。

我可以在没有 Microsoft Office 的情况下自动化突出显示 Excel 中的交替行吗?

是的,使用 IronXL,您可以在没有安装 Microsoft Office 的情况下自动化突出显示 Excel 中的交替行的过程。IronXL 提供以编程方式应用条件格式的方法。

如何将 Excel 表格转换回范围,同时保持格式?

您可以通过选择表格,转到“表格设计”,选择“转换为范围”来将 Excel 表格转换回范围。为了保持格式,您可能需要使用格式刷工具再次手动应用。

是否有经济有效的方式使用 .NET Excel 库?

IronXL 提供一个用于开发的免费版本,其中包含水印。对于生产用途,有各种定价计划可供选择,为在 .NET 应用程序中自动化 Excel 任务提供具有成本效益的解决方案。

如何使用 .NET 库以编程方式应用条件格式?

使用 IronXL,您可以通过加载工作簿,访问所需的工作表,创建条件格式规则,设置必要的样式选项,并保存更新后的工作簿来应用条件格式。

哪些 Excel 文件格式由 .NET Excel 库支持?

IronXL 支持各种 Excel 文件格式,包括 .xlsx,并允许在 XML、JSON、HTML 和 CSV 等格式之间转换,使其适应不同的应用需求。

Curtis Chau
技术作家

Curtis Chau 拥有卡尔顿大学的计算机科学学士学位,专注于前端开发,精通 Node.js、TypeScript、JavaScript 和 React。他热衷于打造直观且美观的用户界面,喜欢使用现代框架并创建结构良好、视觉吸引力强的手册。

除了开发之外,Curtis 对物联网 (IoT) 有浓厚的兴趣,探索将硬件和软件集成的新方法。在空闲时间,他喜欢玩游戏和构建 Discord 机器人,将他对技术的热爱与创造力相结合。