ضبط تلقائي للصفوف والأعمدة
ضبط تلقائي
Aspose.Cells لبيثون via .NET توفر فئة تمثل ملف Excel لميكروسوفت. فئة تحتوي على مجموعة تسمح بالوصول إلى كل ورق عمل في ملف Excel. الورقة العمل ممثلة بفئة. فئة توفر مجموعة واسعة من الخصائص والطرق لإدارة ورقة عمل. هذا المقال يستعرض استخدام الفئة لتناسب صفوف أو أعمدة.
ضبط تلقائي للصف - بسيط
أبسط الطرق لضبط عرض وارتفاع الصف هي استدعاء Worksheet الفئة auto_fit_row. يستغرق الأمر المنهج auto_fit_row فهو خطوة بسيطة.
from aspose.cells import Workbook | |
# 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(".") | |
InputPath = dataDir + "Book1.xlsx" | |
# Creating a file stream containing the Excel file to be opened | |
fstream = open(InputPath, "rb") | |
# Opening the Excel file through the file stream | |
workbook = Workbook(fstream) | |
# Accessing the first worksheet in the Excel file | |
worksheet = workbook.worksheets[0] | |
# Auto-fitting the 3rd row of the worksheet | |
worksheet.auto_fit_row(1) | |
# Saving the modified Excel file | |
workbook.save(dataDir + "output.xlsx") | |
# Closing the file stream to free all resources | |
fstream.close() |
كيفية ضبط صف تلقائيًا في مجموعة من الخلايا
الصف يتكون من عدة أعمدة. Aspose.Cells لبيثون via .NET تسمح للمطورين بتحديد الصف على أساس المحتوى في نطاق الخلايا داخل الصف عن طريق استدعاء النسخة المعدلة من الطريقة. تأخذ المعلمات التالية:
- فهرس الصف, فهرس الصف المراد ضبطه تلقائياً.
- فهرس العمود الأول, فهرس العمود الأول للصف.
- فهرس العمود الأخير, فهرس العمود الأخير للصف.
يقوم الأسلوب auto_fit_row بفحص محتوى كل الأعمدة في الصف ثم يضبط الصف تلقائيًا.
from aspose.cells import Workbook | |
# 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(".") | |
InputPath = dataDir + "Book1.xlsx" | |
# Creating a file stream containing the Excel file to be opened | |
fstream = open(InputPath, "rb") | |
# Opening the Excel file through the file stream | |
workbook = Workbook(fstream) | |
# Accessing the first worksheet in the Excel file | |
worksheet = workbook.worksheets[0] | |
# Auto-fitting the 3rd row of the worksheet | |
worksheet.auto_fit_row(1, 0, 5) | |
# Saving the modified Excel file | |
workbook.save(dataDir + "output.xlsx") | |
# Closing the file stream to free all resources | |
fstream.close() |
كيفية ضبط العمود تلقائيًا في مجموعة من الخلايا
يتكون العمود من العديد من الصفوف. يمكن ضبط عرض العمود تلقائيًا بناءً على المحتوى في مجموعة من الخلايا في العمود عن طريق استدعاء نسخة محملة من auto_fit_column الذي يأخذ المعلمات التالية:
- فهرس العمود: فهرس العمود الذي سيتم تلائم حجمه تلقائياً.
- فهرس الصف الأول: فهرس أول صف في العمود.
- فهرس الصف الأخير: فهرس آخر صف في العمود.
يقوم الطريقة auto_fit_column بفحص محتويات جميع الصفوف في العمود ثم يقوم بضبط حجم العمود تلقائياً.
from aspose.cells import Workbook | |
# 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(".") | |
InputPath = dataDir + "Book1.xlsx" | |
# Creating a file stream containing the Excel file to be opened | |
fstream = open(InputPath, "rb") | |
# Opening the Excel file through the file stream | |
workbook = Workbook(fstream) | |
# Accessing the first worksheet in the Excel file | |
worksheet = workbook.worksheets[0] | |
# Auto-fitting the Column of the worksheet | |
worksheet.auto_fit_column(4, 4, 6) | |
# Saving the modified Excel file | |
workbook.save(dataDir + "output.xlsx") | |
# Closing the file stream to free all resources | |
fstream.close() |
كيفية تلائم حجم الصفوف للخلايا المدمجة
مع Aspose.Cells لبيثون via .NET من الممكن تحديد تناسب صفوف حتى للخلايا المدمجة باستخدام واجهة. توفر فئة خاصية يمكن استخدامها لتحديد تناسب الصفوف للخلايا المدمجة. تقبل مجموعة تفاضلية تحتوي على الأعضاء التالية.
- NONE: تجاهل الخلايا المدمجة.
- FIRST_LINE: فقط يوسع ارتفاع السطر الأول.
- السطر_الأخير: يزيد فقط ارتفاع الصف الأخير.
- كل_سطر: يزيد فقط ارتفاع كل صف.
from aspose.cells import AutoFitMergedCellsType, AutoFitterOptions, Workbook | |
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET | |
# Output directory | |
outputDir = RunExamples.Get_OutputDirectory() | |
# Instantiate a new Workbook | |
wb = Workbook() | |
# Get the first (default) worksheet | |
_worksheet = wb.worksheets[0] | |
# Create a range A1:B1 | |
range = _worksheet.cells.create_range(0, 0, 1, 2) | |
# Merge the cells | |
range.merge() | |
# Insert value to the merged cell A1 | |
_worksheet.cells.get(0, 0).value = "A quick brown fox jumps over the lazy dog. A quick brown fox jumps over the lazy dog....end" | |
# Create a style object | |
style = _worksheet.cells.get(0, 0).get_style() | |
# Set wrapping text on | |
style.is_text_wrapped = True | |
# Apply the style to the cell | |
_worksheet.cells.get(0, 0).set_style(style) | |
# Create an object for AutoFitterOptions | |
options = AutoFitterOptions() | |
# Set auto-fit for merged cells | |
options.auto_fit_merged_cells_type = AutoFitMergedCellsType.EACH_LINE | |
# Autofit rows in the sheet(including the merged cells) | |
_worksheet.auto_fit_rows(options) | |
# Save the Excel file | |
wb.save(outputDir + "AutofitRowsforMergedCells.xlsx") |
يمكنك أيضًا محاولة استخدام الإصدارات الزائدة من الأساليب auto_fit_rows و auto_fit_columns التي تقبل مجموعة من الصفوف/الأعمدة ومثيلًا من AutoFitterOptions لتلائم حجم الصفوف/الأعمدة المحددة بالطريقة التي ترغب فيها AutoFitterOptions.
توقيعات الأساليب المذكورة أعلاه هي كالتالي: