تنسيق الخلايا

مقدمة

كيفية تنسيق الخلايا باستخدام أساليب GetStyle و SetStyle

تطبيق أنواع مختلفة من أنماط التنسيق على الخلايا لتعيين ألوان الخلفية أو الأمامية، الحدود، الخطوط، المحاور الأفقية والعمودية، مستوى المسافة البادئة، اتجاه النص، زاوية الدوران والمزيد.

كيفية استخدام أساليب GetStyle و SetStyle

إذا كان المطورون بحاجة لتطبيق أنماط تنسيق مختلفة إلى خلايا مختلفة، فإنه من الأفضل الحصول على Style للخلية باستخدام الطريقة Cell.get_style، تحديد سمات النمط ثم تطبيق التنسيق باستخدام الطريقة Cell.set_style. يُظهر المثال التالي هذا النهج لتطبيق تنسيقات مختلفة على خلية.

from aspose.cells import BorderType, CellBorderType, TextAlignmentType, Workbook
from aspose.pydrawing import Color
from os import os, path
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET
# The path to the documents directory.
dataDir = RunExamples.GetDataDir(".")
# Create directory if it is not already present.
IsExists = path.isdir(dataDir)
if notIsExists:
os.makedirs(dataDir)
# Instantiating a Workbook object
workbook = Workbook()
# Obtaining the reference of the first worksheet
worksheet = workbook.worksheets[0]
# Accessing the "A1" cell from the worksheet
cell = worksheet.cells.get("A1")
# Adding some value to the "A1" cell
cell.put_value("Hello Aspose!")
# Get the style from A1 cell
style = cell.get_style()
# Setting the vertical alignment
style.vertical_alignment = TextAlignmentType.CENTER
# Setting the horizontal alignment
style.horizontal_alignment = TextAlignmentType.CENTER
# Setting the font color of the text
style.font.color = Color.green
# Setting to shrink according to the text contained in it
style.shrink_to_fit = True
# Setting the bottom border color to red
style.borders.get(BorderType.BOTTOM_BORDER).color = Color.red
# Setting the bottom border type to medium
style.borders.get(BorderType.BOTTOM_BORDER).line_style = CellBorderType.MEDIUM
# Applying the style to A1 cell
cell.set_style(style)
# Saving the Excel file
workbook.save(dataDir + "book1.out.xls")

كيفية استخدام كائن النمط لتنسيق خلايا مختلفة

إذا كان المطورون بحاجة لتطبيق نفس النمط التنسيقي على خلايا مختلفة، فيمكنهم استخدام Style. يرجى اتباع الخطوات التالية لاستخدام Style:

  1. إضافة Style باستدعاء طريقة create_style من فئة Workbook
  2. الوصول إلى كائن Style الذي تمت إضافته حديثًا
  3. تعيين خصائص/سمات Style المطلوبة لتطبيق إعدادات التنسيق المرغوبة
  4. تعيين كائن Style المكون وتهيئته إلى الخلايا المرغوبة

يمكن أن يحسن هذا النهج بشكل كبير كفاءة تطبيقاتك ويوفر ذاكرة أيضًا.

from aspose.cells import BorderType, CellBorderType, TextAlignmentType, Workbook
from aspose.pydrawing import Color
from os import os, path
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET
# The path to the documents directory.
dataDir = RunExamples.GetDataDir(".")
# Create directory if it is not already present.
IsExists = path.isdir(dataDir)
if notIsExists:
os.makedirs(dataDir)
# Instantiating a Workbook object
workbook = Workbook()
# Adding a new worksheet to the Excel object
i = workbook.worksheets.add()
# Obtaining the reference of the first worksheet by passing its sheet index
worksheet = workbook.worksheets[i]
# Accessing the "A1" cell from the worksheet
cell = worksheet.cells.get("A1")
# Adding some value to the "A1" cell
cell.put_value("Hello Aspose!")
# Adding a new Style
style = workbook.create_style()
# Setting the vertical alignment of the text in the "A1" cell
style.vertical_alignment = TextAlignmentType.CENTER
# Setting the horizontal alignment of the text in the "A1" cell
style.horizontal_alignment = TextAlignmentType.CENTER
# Setting the font color of the text in the "A1" cell
style.font.color = Color.green
# Shrinking the text to fit in the cell
style.shrink_to_fit = True
# Setting the bottom border color of the cell to red
style.borders.get(BorderType.BOTTOM_BORDER).color = Color.red
# Setting the bottom border type of the cell to medium
style.borders.get(BorderType.BOTTOM_BORDER).line_style = CellBorderType.MEDIUM
# Assigning the Style object to the "A1" cell
cell.set_style(style)
# Apply the same style to some other cells
worksheet.cells.get("B1").set_style(style)
worksheet.cells.get("C1").set_style(style)
worksheet.cells.get("D1").set_style(style)
# Saving the Excel file
workbook.save(dataDir + "book1.out.xls")

كيفية استخدام أنماط Microsoft Excel 2007 المحددة مسبقًا

إذا كنت بحاجة إلى تطبيق أنماط تنسيق مختلفة على Microsoft Excel 2007، استخدم طريقة التطبيق باستخدام API الخاص بـ Aspose.Cells for Python via .NET. يُوضح المثال أدناه كيفية تطبيق نمط معرف مسبقًا على خلية.

from aspose.cells import Workbook
from os import os, path
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET
# The path to the documents directory.
dataDir = RunExamples.GetDataDir(".")
# Create directory if it is not already present.
IsExists = path.isdir(dataDir)
if notIsExists:
os.makedirs(dataDir)
# Instantiate a new Workbook.
workbook = Workbook()
# Create a style object .
style = workbook.create_style()
# Input a value to A1 cell.
workbook.worksheets[0].cells.get("A1").put_value("Test")
# Apply the style to the cell.
workbook.worksheets[0].cells.get("A1").set_style(style)
# Save the Excel 2007 file.
workbook.save(dataDir + "book1.out.xlsx")

كيفية تنسيق الأحرف المحددة في خلية

تشرح التعامل مع إعدادات الخط كيفية تنسيق النص في الخلايا، لكنها تشرح فقط كيفية تنسيق كل مضمون الخلية. ماذا إذا كنت ترغب في تنسيق الأحرف المحددة فقط؟

يدعم Aspose.Cells لبايثون via .NET أيضًا هذه الميزة. يشرح هذا الموضوع كيف نستخدم هذه الميزة بشكل فعال.

كيفية تنسيق الأحرف المحددة

يوفر Aspose.Cells لبايثون via .NET فئة، Workbook التي تمثل ملف إكسل من مايكروسوفت. تحتوي فئة Workbook على مجموعة worksheets التي تتيح الوصول إلى كل ورقة عمل في ملف إكسل. يتم تمثيل ورقة العمل بواسطة فئة Worksheet. توفر فئة Worksheet مجموعة cells. كل عنصر في مجموعة cells يمثل كائن من فئة Cell.

توفر الفئة Cell الطريقة characters التي تأخذ المعلمات التالية لتحديد نطاق الحروف داخل خلية:

  • فهرس البداية, وهو فهرس الحرف الذي يبدأ منه التحديد.
  • عدد الحروف, عدد الأحرف المراد تحديدها.

تعيد الطريقة characters مثيلًا للفئة FontSetting التي تسمح للمطورين بتنسيق الحروف بنفس الطريقة التي يفعلون بها مع الخلية كما هو موضح أدناه في مثال الكود. في ملف الإخراج، في الخلية A1، سيتم تنسيق الكلمة ‘زيارة’ بالخط الأمامي ولكن كلمة ‘Aspose!’ ستكون سمينة وزرقاء.

from aspose.cells import Workbook
from aspose.pydrawing import Color
from os import os, path
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET
# The path to the documents directory.
dataDir = RunExamples.GetDataDir(".")
# Create directory if it is not already present.
IsExists = path.isdir(dataDir)
if notIsExists:
os.makedirs(dataDir)
# Instantiating a Workbook object
workbook = Workbook()
# Obtaining the reference of the first(default) worksheet by passing its sheet index
worksheet = workbook.worksheets[0]
# Accessing the "A1" cell from the worksheet
cell = worksheet.cells.get("A1")
# Adding some value to the "A1" cell
cell.put_value("Visit Aspose!")
# Setting the font of selected characters to bold
cell.characters(6, 7).font.is_bold = True
# Setting the font color of selected characters to blue
cell.characters(6, 7).font.color = Color.blue
# Saving the Excel file
workbook.save(dataDir + "book1.out.xls")

كيفية تنسيق الصفوف والأعمدة

في بعض الأحيان، يحتاج المطورون إلى تطبيق نفس التنسيق على الصفوف أو الأعمدة. تطبيق التنسيق على الخلايا واحدة تلو الأخرى غالبًا ما يستغرق وقتا أطول وليس حلا جيدًا. لمعالجة هذه المشكلة، يوفر Aspose.Cells لبايثون via .NET طريقة بسيطة وسريعة نوقشت بالتفصيل في هذا المقال.

تنسيق الصفوف والأعمدة

يوفر Aspose.Cells لبايثون via .NET فئة، Workbook التي تمثل ملف إكسل من مايكروسوفت. تحتوي فئة Workbook على مجموعة worksheets التي تتيح الوصول إلى كل ورقة عمل في ملف إكسل. يتم تمثيل ورقة العمل بواسطة فئة Worksheet. توفر فئة Worksheet مجموعة cells. توفر مجموعة cells مجموعة rows.

كيفية تنسيق صف

كل عنصر في مجموعة rows يمثل كائن Row. يقدم كائن Row الطريقة apply_style المستخدمة لتعيين تنسيق الصف. لتطبيق نفس التنسيق على الصف، استخدم كائن Style. توضح الخطوات أدناه كيفية استخدامه.

  1. أضف كائن Style إلى الفئة Workbook عن طريق استدعاء طريقته create_style.
  2. ضبط خصائص كائن Style لتطبيق إعدادات التنسيق.
  3. جعل السمات ذات الصلة ON لكائن StyleFlag.
  4. تعيين كائن Style المكون إلى الكائن Row المكون.
from aspose.cells import BorderType, CellBorderType, StyleFlag, TextAlignmentType, Workbook
from aspose.pydrawing import Color
from os import os, path
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET
# The path to the documents directory.
dataDir = RunExamples.GetDataDir(".")
# Create directory if it is not already present.
IsExists = path.isdir(dataDir)
if notIsExists:
os.makedirs(dataDir)
# Instantiating a Workbook object
workbook = Workbook()
# Obtaining the reference of the first (default) worksheet by passing its sheet index
worksheet = workbook.worksheets[0]
# Adding a new Style to the styles
style = workbook.create_style()
# Setting the vertical alignment of the text in the "A1" cell
style.vertical_alignment = TextAlignmentType.CENTER
# Setting the horizontal alignment of the text in the "A1" cell
style.horizontal_alignment = TextAlignmentType.CENTER
# Setting the font color of the text in the "A1" cell
style.font.color = Color.green
# Shrinking the text to fit in the cell
style.shrink_to_fit = True
# Setting the bottom border color of the cell to red
style.borders.get(BorderType.BOTTOM_BORDER).color = Color.red
# Setting the bottom border type of the cell to medium
style.borders.get(BorderType.BOTTOM_BORDER).line_style = CellBorderType.MEDIUM
# Creating StyleFlag
styleFlag = StyleFlag()
styleFlag.horizontal_alignment = True
styleFlag.vertical_alignment = True
styleFlag.shrink_to_fit = True
styleFlag.borders = True
styleFlag.font_color = True
# Accessing a row from the Rows collection
row = worksheet.cells.rows[0]
# Assigning the Style object to the Style property of the row
row.apply_style(style, styleFlag)
# Saving the Excel file
workbook.save(dataDir + "book1.out.xls")

كيفية تنسيق عمود

توفر مجموعة cells أيضًا مجموعة columns. كل عنصر في مجموعة columns يمثل كائن Column. على غرار كائن Row، يقدم كائن Column أيضًا الطريقة apply_style لتنسيق عمود.

from aspose.cells import BorderType, CellBorderType, StyleFlag, TextAlignmentType, Workbook
from aspose.pydrawing import Color
from os import os, path
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET
# The path to the documents directory.
dataDir = RunExamples.GetDataDir(".")
# Create directory if it is not already present.
IsExists = path.isdir(dataDir)
if notIsExists:
os.makedirs(dataDir)
# Instantiating a Workbook object
workbook = Workbook()
# Obtaining the reference of the first (default) worksheet by passing its sheet index
worksheet = workbook.worksheets[0]
# Adding a new Style to the styles
style = workbook.create_style()
# Setting the vertical alignment of the text in the "A1" cell
style.vertical_alignment = TextAlignmentType.CENTER
# Setting the horizontal alignment of the text in the "A1" cell
style.horizontal_alignment = TextAlignmentType.CENTER
# Setting the font color of the text in the "A1" cell
style.font.color = Color.green
# Shrinking the text to fit in the cell
style.shrink_to_fit = True
# Setting the bottom border color of the cell to red
style.borders.get(BorderType.BOTTOM_BORDER).color = Color.red
# Setting the bottom border type of the cell to medium
style.borders.get(BorderType.BOTTOM_BORDER).line_style = CellBorderType.MEDIUM
# Creating StyleFlag
styleFlag = StyleFlag()
styleFlag.horizontal_alignment = True
styleFlag.vertical_alignment = True
styleFlag.shrink_to_fit = True
styleFlag.borders = True
styleFlag.font_color = True
# Accessing a column from the Columns collection
column = worksheet.cells.columns[0]
# Applying the style to the column
column.apply_style(style, styleFlag)
# Saving the Excel file
workbook.save(dataDir + "book1.out.xls")

مواضيع متقدمة