使用IRONXL

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

已更新 2024年1月7日
分享:

Microsoft Excel 提供內建功能,根據使用者的需求突出顯示行。在 Excel 中,可以使用突出顯示儲存格動作來突出顯示行。Excel 中的突出顯示行功能是一種簡單的方法,使文件更具可看性。此工具對於顯示數據點之間的差異非常有用。但是,在 Microsoft Excel 中突出顯示奇數行可能會比較棘手,對初學者來說並不那麼容易。然而,為交替行著色對你來說可能會變得非常簡單。我們將逐步解釋。

讓我們開始:

  1. 選取你想要格式化的行。

  2. 從主選單中點擊 首頁,然後點擊 格式化為表格

  3. 從表格對話框中選擇具有交替行著色的表格樣式。

  4. 現在,如果你想將著色從行更改為列,選擇表格,然後從表格樣式選項組中點擊設計。然後,從下拉選單中取消選取 斑駁行 框並選取 斑駁列 框。

如何在 Excel 中高亮顯示每隔一行,圖 1:導航至 Excel 中的帶狀列功能

導航到 Excel 中的帶狀列功能

如果你想保留表格佈局,但不需要它的功能,你可以將其轉換為數據範圍。如果你在添加顏色行/列並有帶狀格式,這不會自動複製。然而,你可以使用格式刷通過複製具有替代格式的行或列重新創建格式。

使用條件格式設置帶狀行或列

您還可以使用條件格式規則對特定行或列應用不同的格式。步驟如下:

  • 在工作表上,執行以下操作之一:
  • 如果您想對一些單元格應用格式,請選擇單元格或行範圍。如果您想將格式應用到整個文件,請按Ctrl+A選擇整個文件。
  • 前往 開始 > 條件格式 > 新規則。這將打開格式設置規則對話框。

如何在 Excel 中突出顯示每隔一行,圖 2:設置一個新規則以進行條件格式化

設定條件格式的新規則

  • “選擇規則類型”部分,選擇“使用公式來決定要設定格式的儲存格”選項。
  • 在“編輯規則描述”部分中,輸入以下公式以將顏色應用於交替的行。
=MOD(ROW(),2)=0

若要將顏色應用於交替列,請輸入此公式:=MOD(列(),2)=0

這些公式決定一行或一列是偶數還是奇數,並確定我們想要應用的顏色。

如何在 Excel 中突出顯示每隔一行,圖 3:新格式規則對話框

新的格式規則對話框

  1. 點擊格式
  2. 儲存格格式框中,點擊填滿
  3. 選擇一個顏色,然後點擊確定
  4. 您可以在範例下預覽您的選擇,然後點擊確定或選擇另一個顏色。

通過實施以下步驟,您將看到交替的行被高亮顯示。

IronXL: C# Excel Library

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
VB   C#

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

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")
VB   C#

我們可以使用定義條件格式規則 建立條件格式規則() 功能。這非常容易 編輯字體樣式和邊框樣式 使用 IronXL。您可以 格式化行 使用 row 函數。在參數中,您將給出想要應用的行號和格式。您可以選擇每 n 行突出顯示。您可以在 中查看更多詳細信息。 我們關於條件格式支持的代碼示例.

IronXL 在開發階段是免費的。但是,在開發階段會有水印。您可以在生產中免費嘗試。您可以啟動 IronXL 免費試用 在沒有任何付款或信用卡資訊的情況下。之後,您可以購買它。根據您的需求,有不同的價格方案可供選擇,請訪問這個 授權頁面 更多資訊。

< 上一頁
如何在 Excel 中移动行
下一個 >
在 C# 中讀取 CSV 文件:教程

準備開始了嗎? 版本: 2024.10 剛剛發布

免費 NuGet 下載 總下載次數: 1,023,839 查看許可證 >