セルの形式を変更

可能な使用シナリオ

特定のデータを強調表示したい場合、セルのスタイルを変更できます。

Excelのセルのフォーマットを変更する方法

Excelの単一のセルのフォーマットを変更するには、次の手順に従います。

  1. Excelを開き、セルのフォーマットを変更したいワークブックを開きます。

  2. フォーマットを変更したいセルを見つけます。

  3. セルを右クリックして、コンテキストメニューから"セルの書式設定"を選択します。または、セルを選択し、Excelリボンのホームタブに移動し、“セル"グループの"書式"ドロップダウンをクリックし、「セルの書式設定」を選択することもできます。

  4. 「セルの書式設定」ダイアログボックスが表示されます。ここでは、選択したセルに適用するさまざまな書式オプションを選択できます。たとえば、フォントスタイル、フォントサイズ、フォントの色、数値形式、罫線、背景色などを変更できます。ダイアログボックスのさまざまなタブを探索して、さまざまな書式オプションにアクセスできます。

  5. 所望の書式設定変更を行った後、「OK」ボタンをクリックして、選択したセルに書式を適用します。

C#を使用してセルのフォーマットを変更する方法

Aspose.Cells for Python via .NET を使用してセルのフォーマットを変更するには、以下の方法を使用します:

  1. Cell.set_style(style)
  2. Cell.set_style(style, explicit_flag)
  3. Cell.set_style(style, flag)

サンプルコード

この例では、Excelワークブックを作成し、いくつかのサンプルデータを追加し、最初のワークシートにアクセスし、2つのセル(“A2"および"B3”)を取得します。次に、セルのスタイルを取得し、さまざまな書式オプション(たとえば、フォントの色、太字)を設定し、セルの形式を変更します。最後に、作業ブックを新しいファイルに保存します。 todo:image_alt_text

from aspose.cells import StyleFlag, Workbook
from aspose.pydrawing import Color
# Create the workbook
workbook = Workbook()
# Get the first worksheet
ws = workbook.worksheets[0]
cells = ws.cells
# Setting the value to the cells
cell = cells.get("A1")
cell.put_value("Fruit")
cell = cells.get("B1")
cell.put_value("Count")
cell = cells.get("C1")
cell.put_value("Price")
cell = cells.get("A2")
cell.put_value("Apple")
cell = cells.get("A3")
cell.put_value("Mango")
cell = cells.get("A4")
cell.put_value("Blackberry")
cell = cells.get("A5")
cell.put_value("Cherry")
cell = cells.get("B2")
cell.put_value(5)
cell = cells.get("B3")
cell.put_value(3)
cell = cells.get("B4")
cell.put_value(6)
cell = cells.get("B5")
cell.put_value(4)
cell = cells.get("C2")
cell.put_value(5)
cell = cells.get("C3")
cell.put_value(20)
cell = cells.get("C4")
cell.put_value(30)
cell = cells.get("C5")
cell.put_value(60)
# Access the worksheet
worksheet = workbook.worksheets[0]
a2 = worksheet.cells.get("A2")
# Get style of A2
style = a2.get_style()
# Change the format
style.font.color = Color.red
style.font.is_bold = True
flag = StyleFlag()
flag.font_color = True
a2.set_style(style, flag)
b3 = worksheet.cells.get("B3")
# Get style of B3
style2 = b3.get_style()
# Change the format
style2.font.color = Color.blue
style2.font.is_italic = True
b3.set_style(style2)
# Save the modified workbook
workbook.save("output.xlsx")