使用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或從微軟取得開發者授權。

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

之後,在您的主函式中添加以下程式碼:

WorkBook workbook = WorkBook.Load("test.xlsx");
WorkSheet sheet = workbook.DefaultWorkSheet;

//Create a specific conditional formatting rule.
ConditionalFormattingRule rule = sheet.ConditionalFormatting.CreateConditionalFormattingRule(ComparisonOperator.LessThan, "8");
//Set different style options.
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 formatting with the specified region.
sheet.ConditionalFormatting.AddConditionalFormatting("A3:A8", rule);

ConditionalFormattingRule rule1 = sheet.ConditionalFormatting.CreateConditionalFormattingRule(ComparisonOperator.Between, "7", "10");
rule1.FontFormatting.IsItalic = true;
rule1.FontFormatting.UnderlineType = FontUnderlineType.Single;
sheet.ConditionalFormatting.AddConditionalFormatting("A3:A9", rule1);

workbook.SaveAs("ApplyConditionalFormatting.xlsx");
WorkBook workbook = WorkBook.Load("test.xlsx");
WorkSheet sheet = workbook.DefaultWorkSheet;

//Create a specific conditional formatting rule.
ConditionalFormattingRule rule = sheet.ConditionalFormatting.CreateConditionalFormattingRule(ComparisonOperator.LessThan, "8");
//Set different style options.
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 formatting with the specified region.
sheet.ConditionalFormatting.AddConditionalFormatting("A3:A8", rule);

ConditionalFormattingRule rule1 = sheet.ConditionalFormatting.CreateConditionalFormattingRule(ComparisonOperator.Between, "7", "10");
rule1.FontFormatting.IsItalic = true;
rule1.FontFormatting.UnderlineType = FontUnderlineType.Single;
sheet.ConditionalFormatting.AddConditionalFormatting("A3:A9", rule1);

workbook.SaveAs("ApplyConditionalFormatting.xlsx");
Dim workbook As WorkBook = WorkBook.Load("test.xlsx")
Dim sheet As WorkSheet = workbook.DefaultWorkSheet

'Create a specific conditional formatting rule.
Dim rule As ConditionalFormattingRule = sheet.ConditionalFormatting.CreateConditionalFormattingRule(ComparisonOperator.LessThan, "8")
'Set different style options.
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 formatting with the specified region.
sheet.ConditionalFormatting.AddConditionalFormatting("A3:A8", rule)

Dim rule1 As ConditionalFormattingRule = sheet.ConditionalFormatting.CreateConditionalFormattingRule(ComparisonOperator.Between, "7", "10")
rule1.FontFormatting.IsItalic = True
rule1.FontFormatting.UnderlineType = FontUnderlineType.Single
sheet.ConditionalFormatting.AddConditionalFormatting("A3:A9", rule1)

workbook.SaveAs("ApplyConditionalFormatting.xlsx")
$vbLabelText   $csharpLabel

我們可以使用CreateConditionalFormattingRule()函數來定義條件格式規則。 使用IronXL很容易編輯字型樣式和邊框樣式。 您可以使用 row 函數來格式化行。 而在參數中,您將提供要應用的行號和格式。 您可以選擇突出顯示每第 n 行。 您可以在我們的有關條件格式支持的程式碼範例中查看更多詳細資訊。

IronXL 在開發階段是免費的。 然而,在開發階段會有浮水印。 您可以在生產環境中免費試用。 您可以在不需要任何支付或卡片信息的情況下啟用IronXL 免費試用版。 之後,您可以購買它。 您可以根據您的需求選擇不同的價格方案,請訪問此授權頁面以獲取更多信息。

里根普恩
軟體工程師
Regan 畢業於雷丁大學,擁有電子工程學士學位。在加入 Iron Software 之前,他的工作角色讓他專注於單一任務;而他在 Iron Software 工作中最喜歡的是他所能承擔的工作範圍,無論是增加銷售價值、技術支持、產品開發或市場營銷。他喜歡了解開發人員如何使用 Iron Software 庫,並利用這些知識不斷改進文檔和開發產品。
< 上一頁
如何在 Excel 中移动行
下一個 >
在 C# 中讀取 CSV 文件:教程