ضبط تلقائي للصفوف والأعمدة

ضبط تلقائي

يوفر Aspose.Cells Workbook يمثل ملف Microsoft Excel. كما تحتوي الفئة Workbook على مجموعة Worksheets التي تسمح بالوصول إلى كل ورقة عمل في ملف Excel. يتم تمثيل ورقة العمل بواسطة فئة Worksheet. توفر الفئة Worksheet مجموعة واسعة من الخصائص والأساليب لإدارة ورقة عمل. تتناول هذه المقالة استخدام فئة Worksheet لضبط تلقائي للصفوف أو الأعمدة.

ضبط تلقائي للصف - بسيط

أبسط الطرق لضبط عرض وارتفاع الصف هي استدعاء Worksheet الفئة AutoFitRow. يستغرق الأمر المنهج AutoFitRow فهو خطوة بسيطة.

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
string InputPath = dataDir + "Book1.xlsx";
// Creating a file stream containing the Excel file to be opened
FileStream fstream = new FileStream(InputPath, FileMode.Open);
// Opening the Excel file through the file stream
Workbook workbook = new Workbook(fstream);
// Accessing the first worksheet in the Excel file
Worksheet worksheet = workbook.Worksheets[0];
// Auto-fitting the 3rd row of the worksheet
worksheet.AutoFitRow(1);
// Saving the modified Excel file
workbook.Save(dataDir + "output.xlsx");
// Closing the file stream to free all resources
fstream.Close();

كيفية ضبط صف تلقائيًا في مجموعة من الخلايا

يتكون الصف من العديد من الأعمدة. تُسمح Aspose.Cells للمطورين بضبط الصف تلقائيًا بناءً على المحتوى في مجموعة من الخلايا داخل الصف عن طريق استدعاء نسخة محملة لـ AutoFitRow. يأخذ الأسلوب الزائد الإصدار ما يلي:

  • فهرس الصف, فهرس الصف المراد ضبطه تلقائياً.
  • فهرس العمود الأول, فهرس العمود الأول للصف.
  • فهرس العمود الأخير, فهرس العمود الأخير للصف.

يقوم الأسلوب AutoFitRow بفحص محتوى كل الأعمدة في الصف ثم يضبط الصف تلقائيًا.

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
string InputPath = dataDir + "Book1.xlsx";
// Creating a file stream containing the Excel file to be opened
FileStream fstream = new FileStream(InputPath, FileMode.Open);
// Opening the Excel file through the file stream
Workbook workbook = new Workbook(fstream);
// Accessing the first worksheet in the Excel file
Worksheet worksheet = workbook.Worksheets[0];
// Auto-fitting the 3rd row of the worksheet
worksheet.AutoFitRow(1, 0, 5);
// Saving the modified Excel file
workbook.Save(dataDir + "output.xlsx");
// Closing the file stream to free all resources
fstream.Close();

كيفية ضبط العمود تلقائيًا في مجموعة من الخلايا

يتكون العمود من العديد من الصفوف. يمكن ضبط عرض العمود تلقائيًا بناءً على المحتوى في مجموعة من الخلايا في العمود عن طريق استدعاء نسخة محملة من AutoFitColumn الذي يأخذ المعلمات التالية:

  • فهرس العمود: فهرس العمود الذي سيتم تلائم حجمه تلقائياً.
  • فهرس الصف الأول: فهرس أول صف في العمود.
  • فهرس الصف الأخير: فهرس آخر صف في العمود.

يقوم الطريقة AutoFitColumn بفحص محتويات جميع الصفوف في العمود ثم يقوم بضبط حجم العمود تلقائياً.

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
string InputPath = dataDir + "Book1.xlsx";
// Creating a file stream containing the Excel file to be opened
FileStream fstream = new FileStream(InputPath, FileMode.Open);
// Opening the Excel file through the file stream
Workbook workbook = new Workbook(fstream);
// Accessing the first worksheet in the Excel file
Worksheet worksheet = workbook.Worksheets[0];
// Auto-fitting the Column of the worksheet
worksheet.AutoFitColumn(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 من الممكن تلائم حجم الصفوف تلقائياً حتى للخلايا التي تم دمجها باستخدام واجهة برمجة التطبيقات AutoFitterOptions. تقدم فئة AutoFitterOptions خاصية AutoFitMergedCellsType التي يمكن استخدامها لتلائم حجم الصفوف للخلايا المدمجة. AutoFitMergedCellsType تقبل عنصرًا قابلاً للتعداد AutoFitMergedCellsType الذي يحتوي على الأعضاء التالية.

  • None: تجاهل الخلايا المدمجة.
  • FirstLine: توسيع ارتفاع الصف الأول فقط.
  • LastLine: توسيع ارتفاع الصف الأخير فقط.
  • EachLine: توسيع ارتفاع كل صف على حدة.
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
//Output directory
string outputDir = RunExamples.Get_OutputDirectory();
// Instantiate a new Workbook
Workbook wb = new Workbook();
// Get the first (default) worksheet
Worksheet _worksheet = wb.Worksheets[0];
// Create a range A1:B1
Range range = _worksheet.Cells.CreateRange(0, 0, 1, 2);
// Merge the cells
range.Merge();
// Insert value to the merged cell A1
_worksheet.Cells[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
Aspose.Cells.Style style = _worksheet.Cells[0, 0].GetStyle();
// Set wrapping text on
style.IsTextWrapped = true;
// Apply the style to the cell
_worksheet.Cells[0, 0].SetStyle(style);
// Create an object for AutoFitterOptions
AutoFitterOptions options = new AutoFitterOptions();
// Set auto-fit for merged cells
options.AutoFitMergedCellsType = AutoFitMergedCellsType.EachLine;
// Autofit rows in the sheet(including the merged cells)
_worksheet.AutoFitRows(options);
// Save the Excel file
wb.Save(outputDir + "AutofitRowsforMergedCells.xlsx");

مهم معرفته

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