IRONSOFTWAREHOME
Excel 數字格式
from ironxl import *

# Load an existing WorkSheet
workbook = WorkBook.Load("sample.xls")
worksheet = workbook.WorkSheets[0]

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

selected_range = worksheet["A3:A5"]

# The cell(A3) value will look like 1/1/2020 12:12:12 PM
selected_range.FormatString = "MM/dd/yy h:mm:ss"

# Save changes with applied number formats
workbook.SaveAs("numberFormats.xls")
pip install ironxl
Excel 數字格式

Excel 數字格式

使用 FormatString property 來控制如何在任何 Excel 的 CellRange 中顯示數值,適用於 IronXL for Python。 使用 worksheet["A2"] 選擇指定地址的 Range。 要存取個別的 Cell,可以使用 First() method。 然而,FormatString property 可以直接設定在 CellRange 上,所以常常可以省略 First()。 可以使用相同的方法套用其他數字格式,如下方程式範例所示。

在程式範例中,WorkBook.Load method 開啟現有的 Excel 檔案。worksheet["A2"] syntax 選擇特定的儲存格,並將 FormatString property 設為 "0.00" 以顯示兩位小數。 最後,SaveAs method 將修改過的工作簿儲存為新檔案。

學習使用 Python 設定 Excel 儲存格資料格式。

準備好開始了嗎?

版本:2026.9剛剛發布

Key in blue circle

立即免費取得 30 天試用金鑰

Your trial license will be sent to your email address

無任何限制。100% 解鎖。無需信用卡。

bullet_checked無需信用卡或建立帳號無任何限制。100% 解鎖。無需信用卡。
  • Logo Aetna
  • Logo NASA
  • Logo GE
  • Logo Porsche
  • Logo USDA
  • Logo Qatar
Join Millions of Engineers who’ve tried IronPDF
預訂您的免費 即時示範
Booking Badge

獲得數百萬工程師的信賴世界各地

Iron Software 的客戶標誌
獲取無需承諾的諮詢
填寫以下表單或發電子郵件至sales@ironsoftware.com
您的資訊將永遠 保持機密。
獲得數百萬工程師的信賴世界各地
Iron Software 的客戶標誌
立即獲取您的30天試用金鑰
無需信用卡或帳戶建立