如何在 Excel 中設置單元格數據格式

如何使用 IronXL 在 C# 中設定儲存格資料格式。

This article was translated from English: Does it need improvement?
Translated
View the article in English

IronXL.Excel 可透過在儲存格或範圍上設定 FormatString 屬性,以內建的資料格式 (如貨幣、百分比、日期和自訂數字格式) 來格式化 Excel 儲存格,而無需 Interop 相依性。

快速入門:在一行中將內建格式應用於單一儲存格

本範例展示如何使用 IronXL.Excel 的 API 建立新的 Excel 工作簿,並將內建格式套用至特定的儲存格。 不需要 Interop 相依性 - 只要設定 FormatString 並儲存即可。

Nuget Icon立即開始使用 NuGet 建立 PDF 檔案:

  1. 使用 NuGet 套件管理器安裝 IronXL

    PM > Install-Package IronXL.Excel

  2. 複製並運行這段程式碼。

    IronXL.WorkBook book = IronXL.WorkBook.Create(); book.DefaultWorkSheet["B2"].FormatString = IronXL.Formatting.BuiltinFormats.Currency2;
  3. 部署到您的生產環境進行測試

    立即開始在您的專案中使用 IronXL,免費試用!
    arrow pointer


如何在 Excel 中設定儲存格資料格式?

可以透過單元格和單元格區域存取FormatString屬性。 因此,可以為單一儲存格、列、行和任何選定範圍設定資料格式。 當您需要在不使用 Interop 的情況下使用 C# 處理 Excel 檔案時,IronXL.Excel 的這種靈活性尤其強大。

資料格式化對於在試算表中清楚呈現資訊至關重要。 無論是建立有貨幣值的財務報告、有精確小數位的科學數據,或是以日期為基礎的分析,正確的格式都能確保您的資料既可讀又專業。 IronXL.Excel 透過直接的 API 提供對 Excel 格式化引擎的直接存取,簡化了這個過程。

:path=/static-assets/excel/content-code-examples/how-to/set-cell-data-format.cs
using IronXL;
using IronXL.Formatting;
using System;
using System.Linq;

// Create a new workbook
WorkBook workBook = WorkBook.Create();
WorkSheet workSheet = workBook.DefaultWorkSheet;

// Set the data format to 12300.00%
workSheet["A1"].Value = 123;
workSheet["A1"].FormatString = BuiltinFormats.Percent2;

// Set the data format to 123.0000
workSheet["A2"].Value = 123;
workSheet["A2"].FormatString = "0.0000";

// Set data display format to range
DateTime dateValue = new DateTime(2020, 1, 1, 12, 12, 12);
workSheet["A3"].Value = dateValue;
workSheet["A4"].Value = new DateTime(2022, 3, 3, 10, 10, 10);
workSheet["A5"].Value = new DateTime(2021, 2, 2, 11, 11, 11);

IronXL.Range range = workSheet["A3:A5"];

// Set the data format to 1/1/2020 12:12:12
range.FormatString = "MM/dd/yy h:mm:ss";

workBook.SaveAs("dataFormats.xlsx");
Imports IronXL
Imports IronXL.Formatting
Imports System
Imports System.Linq

' Create a new workbook
Private workBook As WorkBook = WorkBook.Create()
Private workSheet As WorkSheet = workBook.DefaultWorkSheet

' Set the data format to 12300.00%
Private workSheet("A1").Value = 123
Private workSheet("A1").FormatString = BuiltinFormats.Percent2

' Set the data format to 123.0000
Private workSheet("A2").Value = 123
Private workSheet("A2").FormatString = "0.0000"

' Set data display format to range
Private dateValue As New DateTime(2020, 1, 1, 12, 12, 12)
Private workSheet("A3").Value = dateValue
Private workSheet("A4").Value = New DateTime(2022, 3, 3, 10, 10, 10)
Private workSheet("A5").Value = New DateTime(2021, 2, 2, 11, 11, 11)

Private range As IronXL.Range = workSheet("A3:A5")

' Set the data format to 1/1/2020 12:12:12
range.FormatString = "MM/dd/yy h:mm:ss"

workBook.SaveAs("dataFormats.xlsx")
$vbLabelText   $csharpLabel
顯示格式化儲存格的 Excel 試算表:百分比、數字和日期/時間值的不同顯示格式

如何在不轉換的情況下將儲存格值設定為字串?

在 IronXL 中設定值時,請使用StringValue而不是Value以便直接將確切值賦給儲存格,而無需自動轉換。 這就像在 Excel 中的儲存格值前加上撇號一樣。 在處理產品代碼、電話號碼或其他應保留為文字的資料時,此技術尤其有用。 如需更進階的字串操作,請參閱如何在 .NET 中 寫入 Excel 值

:path=/static-assets/excel/content-code-examples/how-to/write-excel-net-assign-stringvalue.cs
// Assign value as string
workSheet["A1"].StringValue = "4402-12";
' Assign value as string
workSheet("A1").StringValue = "4402-12"
$vbLabelText   $csharpLabel

如何在 IronXL 中使用內建格式?

IronXL 函式庫提供了各種預先定義的格式字串,可以透過IronXL.Formatting.BuiltinFormats類別存取這些字串,用於格式化 Excel 儲存格。 這樣您就可以自訂資料在 Excel 表格中的顯示方式。 這些內建格式涵蓋最常見的商業情境,並與所有 Excel 版本相容。

程式化地建立試算表時,套用一致的格式對於維持整個文件的專業標準至關重要。 BuiltinFormats 類別提供超過 30 種預配置的格式模式,與 Excel 的標準格式選項相匹配。

:path=/static-assets/excel/content-code-examples/how-to/set-cell-data-format-builtin-formats.cs
using IronXL;
using IronXL.Formatting;

// Create a new workbook
WorkBook workBook = WorkBook.Create();
WorkSheet workSheet = workBook.DefaultWorkSheet;

// Use builtin formats
workSheet["A1"].Value = 123;
workSheet["A1"].FormatString = BuiltinFormats.Accounting0;

workBook.SaveAs("builtinDataFormats.xlsx");
Imports IronXL
Imports IronXL.Formatting

' Create a new workbook
Private workBook As WorkBook = WorkBook.Create()
Private workSheet As WorkSheet = workBook.DefaultWorkSheet

' Use builtin formats
Private workSheet("A1").Value = 123
Private workSheet("A1").FormatString = BuiltinFormats.Accounting0

workBook.SaveAs("builtinDataFormats.xlsx")
$vbLabelText   $csharpLabel

有哪些內建資料格式可用?

對於Duration類型,輸入值以天為單位。 例如,1 天等於 24 小時,1.05 天等於 25 小時 12 分鐘。 這在 使用 Excel 資料涉及時間追蹤或專案管理時特別有用。 以下是可用的格式類型:

顯示內建數字格式的 Excel 試算表,包含各種資料類型的輸入值與格式化輸出

What Does Each Format Type Do?

  • 一般:預設格式顯示無特定格式的數字。
  • Duration1:顯示以分秒為單位的持續時間,如 "mm:ss"。
  • Duration2:顯示以時、分、秒為單位的持續時間,如 "[h]:mm:ss"。
  • Duration3:顯示以分、秒和毫秒為單位的持續時間,如 "mm:ss.0"。
  • 會計0:不含小數點的會計格式,括號表示負數,如 "#,##0_);(#,##0)"。
  • Accounting0Red:不含小數點的會計格式,紅色代表負數,如 "#,##0_);Red"。
  • Accounting2:有兩個小數點的會計格式,括號表示負數,如 "#,##0.00;(#,##0.00)"。
  • Accounting2Red:有兩個小數點的會計格式,紅色代表負數,如 "#,##0.00_);Red"。
  • Time1: 12 小時格式,AM/PM 如 "h:mm AM/PM"。
  • Time2:12 小時格式,包含秒和 AM/PM,如 "h:mm:ss AM/PM"。
  • Time3:不含秒的 24 小時格式,如 "h:mm"。
  • Time4:24 小時格式,秒如 "h:mm:ss"。
  • ShortDate:短日期格式,如 "m/d/yy"。
  • ShortDateAndTime:簡短的日期和時間格式,如 "m/d/yy h:mm"。
  • LongDate1:長日期,包含月、日、年,如 "d-mm-yy"。
  • LongDate2:帶日和縮寫月的長日期,如 "d-mmm"。
  • LongDate3:長日期,包含縮寫的月份和年份,如 "mmm-yy"。
  • Fraction1:只有一位數的小數值,如 "# ?/?" 。
  • Fraction2:包含兩位數的小數值,如 "# ??/??" 。
  • Scientific1:小數點後一位數的科學記號,如 "##0.0E+0"。
  • Scientific2:帶有兩位小數的科學記號,如 "0.00E+00"。
  • 百分比:不含小數點的百分比,如 "0%"。
  • Percent2:有兩個小數點的百分比,如 "0.00%"。
  • Currency0:不含小數點的貨幣,括號表示負數,如"$#,##0_);($#,##0)"。
  • Currency0Red:不含小數點的貨幣,紅色代表負數,如"$#,##0_);Red"。
  • Currency2:有兩個小數點的貨幣,括號表示負數,如"$#,##0.00;($#,##0.00)"。
  • Currency2Red:有兩個小數位的貨幣,紅色代表負數,如 "$#,##0.00_);Red"。
  • Thousands0: 以千為分隔符的數字,不含小數點,如 "#,##0"。
  • Thousands2:帶有千位分隔符的數字,兩個小數點,如 "#,##0.00"。
  • Number0:不含小數點的數字,如 "0"。
  • Number2:帶有兩位小數的數字,如 "0.00"。
  • 文字:純文字格式,如 "@"。

Excel 中的資料格式化和數字格式化可控制單元格中的數字、日期、時間和其他資料的顯示方式,以提高可讀性並確保資料的準確性。 透過資料格式,您可以將資訊以特定格式(例如百分比或貨幣)呈現。 同樣,數字格式允許您自訂小數位數和顯示選項。

如何在不同範圍套用多種格式?

在實際應用中,您經常需要將不同的格式套用到試算表的各個部分。 本範例示範格式化具有多種資料類型的財務報告。當 以 C# 從資料庫或其他資料來源匯出至 Excel 時,此方法尤其有用:

/* :path=/static-assets/excel/content-code-examples/how-to/set-cell-data-format-financial-report.cs */
using IronXL;
using IronXL.Formatting;
using System;

// Create a financial report with multiple formats
WorkBook workBook = WorkBook.Create();
WorkSheet workSheet = workBook.DefaultWorkSheet;

// Headers
workSheet["A1"].Value = "Financial Report Q4 2023";
workSheet["A1"].Style.Font.Bold = true;

// Revenue section with currency formatting
workSheet["A3"].Value = "Revenue";
workSheet["B3"].Value = 2500000;
workSheet["B3"].FormatString = BuiltinFormats.Currency0;

// Growth percentage
workSheet["A4"].Value = "YoY Growth";
workSheet["B4"].Value = 0.157;
workSheet["B4"].FormatString = BuiltinFormats.Percent2;

// Date formatting
workSheet["A6"].Value = "Report Date";
workSheet["B6"].Value = DateTime.Now;
workSheet["B6"].FormatString = BuiltinFormats.LongDate1;

// Apply accounting format to expense columns
IronXL.Range expenseRange = workSheet["B8:B12"];
expenseRange.FormatString = BuiltinFormats.Accounting2Red;

// Save the formatted report
workBook.SaveAs("FinancialReport_Q4_2023.xlsx");
/* :path=/static-assets/excel/content-code-examples/how-to/set-cell-data-format-financial-report.cs */
using IronXL;
using IronXL.Formatting;
using System;

// Create a financial report with multiple formats
WorkBook workBook = WorkBook.Create();
WorkSheet workSheet = workBook.DefaultWorkSheet;

// Headers
workSheet["A1"].Value = "Financial Report Q4 2023";
workSheet["A1"].Style.Font.Bold = true;

// Revenue section with currency formatting
workSheet["A3"].Value = "Revenue";
workSheet["B3"].Value = 2500000;
workSheet["B3"].FormatString = BuiltinFormats.Currency0;

// Growth percentage
workSheet["A4"].Value = "YoY Growth";
workSheet["B4"].Value = 0.157;
workSheet["B4"].FormatString = BuiltinFormats.Percent2;

// Date formatting
workSheet["A6"].Value = "Report Date";
workSheet["B6"].Value = DateTime.Now;
workSheet["B6"].FormatString = BuiltinFormats.LongDate1;

// Apply accounting format to expense columns
IronXL.Range expenseRange = workSheet["B8:B12"];
expenseRange.FormatString = BuiltinFormats.Accounting2Red;

// Save the formatted report
workBook.SaveAs("FinancialReport_Q4_2023.xlsx");
Imports IronXL
Imports IronXL.Formatting
Imports System

' Create a financial report with multiple formats
Dim workBook As WorkBook = WorkBook.Create()
Dim workSheet As WorkSheet = workBook.DefaultWorkSheet

' Headers
workSheet("A1").Value = "Financial Report Q4 2023"
workSheet("A1").Style.Font.Bold = True

' Revenue section with currency formatting
workSheet("A3").Value = "Revenue"
workSheet("B3").Value = 2500000
workSheet("B3").FormatString = BuiltinFormats.Currency0

' Growth percentage
workSheet("A4").Value = "YoY Growth"
workSheet("B4").Value = 0.157
workSheet("B4").FormatString = BuiltinFormats.Percent2

' Date formatting
workSheet("A6").Value = "Report Date"
workSheet("B6").Value = DateTime.Now
workSheet("B6").FormatString = BuiltinFormats.LongDate1

' Apply accounting format to expense columns
Dim expenseRange As IronXL.Range = workSheet("B8:B12")
expenseRange.FormatString = BuiltinFormats.Accounting2Red

' Save the formatted report
workBook.SaveAs("FinancialReport_Q4_2023.xlsx")
$vbLabelText   $csharpLabel

本範例展示了不同格式類型如何結合來建立專業的財務文件。 對於更進階的格式化選項,包括 條件格式化,您可以進一步延伸這些概念。

在處理大型資料集或以程式化方式建立報表時,格式的一致性變得非常重要。 IronXL.Excel 的格式化系統可與其他功能無縫整合,例如 math 函數編輯公式,讓您可以建立完全自動化的報表解決方案。

對於從 Office Interop 轉移過來或追求更佳效能的開發人員而言,IronXL 的格式化系統提供了顯著的優勢。 與傳統方法不同,您可以有效率地格式化成千上萬的單元格,而無需 COM interop 開銷。 若要瞭解有關 Excel 自動化最佳實務的更多資訊,請瀏覽我們全面的 Excel 文件

常見問題解答

在不使用 Interop 的情況下,如何在 C# 中格式化 Excel 單元格?

IronXL.Excel 提供了一種簡單的方式來格式化 Excel 單元格,而無需 Interop 相依性。您可以在儲存格或範圍上使用 FormatString 屬性來套用內建格式,例如貨幣、百分比、日期和自訂數字格式。只需使用 IronXL 建立或開啟工作簿,並將 FormatString 屬性設定為所需格式即可。

將貨幣格式化套用至儲存格的最快方法是什麼?

使用 IronXL,您只需一行代碼即可套用貨幣格式。建立工作簿後,只需使用:book.DefaultWorkSheet["B2"].FormatString = IronXL.Formatting.BuiltinFormats.Currency2; 即可在儲存格 B2 套用小數點後 2 位的內建貨幣格式。

我可以一次格式化多個儲存格嗎?

是的,IronXL 允許您透過 FormatString 屬性來格式化個別儲存格、整列、行或任何選取的範圍。這種靈活性可讓您輕鬆地在 Excel 試算表的多個儲存格中套用一致的格式,而無需逐一遍歷每個儲存格。

設定儲存格值時,如何防止自動資料轉換?

IronXL 提供 StringValue 屬性,可將儲存格值設定為字串,而無需自動轉換。與其使用 Value 屬性,不如使用 StringValue 來為儲存格指定精確的值。其作用類似於在 Excel 中的儲存格值前放置撇號,對於產品代碼、電話號碼或其他應保留為文字的資料特別有用。

有哪些內建格式選項?

IronXL 透過 IronXL.Formatting.BuiltinFormats 類別提供全面的預定義格式字串。其中包括各種貨幣格式、日期和時間格式、百分比顯示、科學記數法和自訂數字格式,讓您可以自訂 Excel 試算表中的資料顯示方式。

我需要安裝 Microsoft Excel 才能使用儲存格格式化功能嗎?

不,IronXL 獨立運作,不需要 Microsoft Excel 或 Interop 的依賴。該函式庫包含自己的格式化引擎,可直接操作 Excel 檔案,因此非常適合伺服器環境或未安裝 Excel 的系統。

Curtis Chau
技術撰稿人

Curtis Chau 擁有電腦科學學士學位(卡爾頓大學),專長於前端開發,精通 Node.js、TypeScript、JavaScript 和 React。Curtis 對製作直覺且美觀的使用者介面充滿熱情,他喜歡使用現代化的架構,並製作結構良好且視覺上吸引人的手冊。

除了開發之外,Curtis 對物聯網 (IoT) 也有濃厚的興趣,他喜歡探索整合硬體與軟體的創新方式。在空閒時間,他喜歡玩遊戲和建立 Discord bots,將他對技術的熱愛與創意結合。

準備好開始了嗎?
Nuget 下載 1,846,091 | 版本: 2026.2 剛剛發布