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

ضبط تلقائي

توفر Aspose.Cells صفًا من الصفوف Workbook تمثل ملف Microsoft Excel. يحتوي صف Workbook على مجموعة Worksheets التي تسمح بالوصول إلى كل ورقة عمل في ملف Excel.

تمثل ورقة العمل بواسطة الصف Worksheet. يوفر صف Worksheet مجموعة كبيرة من الخصائص والأساليب لإدارة ورقة العمل. يتناول هذا المقال استخدام صف Worksheet لتحديد تلقائي للصفوف أو الأعمدة.

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

أبسط طريقة لضبط تلقائي لعرض وارتفاع الصف هي استدعاء طريقة autoFitRow من فئة Worksheet. تأخذ طريقة autoFitRow فهرس الصف (للصف الذي سيتم تغيير حجمه) كمعامل.

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getSharedDataDir(AutoFitRowsandColumns.class) + "rows_cloumns/";
// Instantiating a Workbook object
Workbook workbook = new Workbook(dataDir + "book1.xls");
// Accessing the first worksheet in the Excel file
Worksheet worksheet = workbook.getWorksheets().get(0);
// Auto-fitting the 2nd row of the worksheet
worksheet.autoFitRow(1);
// Auto-fitting the 1st column of the worksheet
worksheet.autoFitColumn(0);
// Saving the modified Excel file in default (that is Excel 2003) format
workbook.save(dataDir + "AutoFitRowsandColumns_out.xls");
// Print message
System.out.println("Row and Column auto fit successfully.");

تحديد الصف تلقائيًا في مجموعة من الخلايا

يتألف الصف من العديد من الأعمدة. يتيح Aspose.Cells للمطورين ضبط تلقائي لصف واحد استنادًا إلى المحتوى في نطاق من الخلايا داخل الصف عن طريق استدعاء نسخة محملة فوق من طريقة autoFitRow. تأخذ هذه الطريقة المعاملات التالية:

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

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

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getSharedDataDir(AutoFitRowsinaRangeofCells.class) + "rows_cloumns/";
// Instantiating a Workbook object
Workbook workbook = new Workbook(dataDir + "book1.xls");
// Accessing the first worksheet in the Excel file
Worksheet worksheet = workbook.getWorksheets().get(0);
// Auto-fitting the row of the worksheet
worksheet.autoFitRow(1, 0, 5);
// Saving the modified Excel file in default (that is Excel 2003) format
workbook.save(dataDir + "AutoFitRowsinaRangeofCells_out.xls");
// Print message
System.out.println("Row auto fit successfully.");

تلائم العمود تلقائيًا - بسيط

أسهل طريقة لضبط تلقائي لعرض وارتفاع عمود هي استدعاء طريقة autoFitColumn من فئة Worksheet. تأخذ هذه الطريقة فهرس العمود (للعمود الذي سيتم تغيير حجمه) كمعامل.

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getSharedDataDir(AutoFitRowsandColumns.class) + "rows_cloumns/";
// Instantiating a Workbook object
Workbook workbook = new Workbook(dataDir + "book1.xls");
// Accessing the first worksheet in the Excel file
Worksheet worksheet = workbook.getWorksheets().get(0);
// Auto-fitting the 2nd row of the worksheet
worksheet.autoFitRow(1);
// Auto-fitting the 1st column of the worksheet
worksheet.autoFitColumn(0);
// Saving the modified Excel file in default (that is Excel 2003) format
workbook.save(dataDir + "AutoFitRowsandColumns_out.xls");
// Print message
System.out.println("Row and Column auto fit successfully.");

تلائم العمود في مجموعة من الخلايا

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

  • فهرس العمود, يمثل فهرس العمود الذي تحتاج محتوياته إلى التكيف تلقائيًا
  • فهرس الصف الأول, يمثل فهرس الصف الأول للعمود
  • فهرس الصف الأخير, يمثل فهرس الصف الأخير للعمود

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

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getSharedDataDir(AutoFitColumnsinaRangeofCells.class) + "rows_cloumns/";
// Instantiating a Workbook object
Workbook workbook = new Workbook(dataDir + "book1.xls");
// Accessing the first worksheet in the Excel file
Worksheet worksheet = workbook.getWorksheets().get(0);
// Auto-fitting the Column of the worksheet
worksheet.autoFitColumn(4, 4, 6);
// Saving the modified Excel file in default (that is Excel 2003) format
workbook.save(dataDir + "AutoFitColumnsinaRangeofCells_out.xls");
// Print message
System.out.println("Columns auto fit successfully.");

تلائم حجم الصفوف للخلايا المدمجة

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

  • NONE: تجاهل الخلايا المدمجة.
  • FIRST_LINE: يوسع ارتفاع الصف الأول فقط.
  • LAST_LINE: يوسع ارتفاع الصف الأخير فقط.
  • EACH_LINE: يوسع ارتفاع كل صف فقط.
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getSharedDataDir(AutofitRowsforMergedCells.class) + "RowsAndColumns/";
// Instantiating a Workbook object
Workbook workbook = new Workbook();
// Accessing the first worksheet in the Excel file
Worksheet worksheet = workbook.getWorksheets().get(0);
// Create a range A1:B1
Range range = worksheet.getCells().createRange(0, 0, 1, 2);
// Merge the cells
range.merge();
// Insert value to the merged cell A1
worksheet.getCells().get(0, 0).setValue("A quick brown fox jumps over the lazy dog. A quick brown fox jumps over the lazy dog....end");
// Create a style object
Style style = worksheet.getCells().get(0, 0).getStyle();
// Set wrapping text on
style.setTextWrapped(true);
// Apply the style to the cell
worksheet.getCells().get(0, 0).setStyle(style);
// Create an object for AutoFitterOptions
AutoFitterOptions options = new AutoFitterOptions();
// Set auto-fit for merged cells
options.setAutoFitMergedCellsType(AutoFitMergedCellsType.EACH_LINE);
// Autofit rows in the sheet(including the merged cells)
worksheet.autoFitRows(options);
// Save the Excel file
workbook.save(dataDir + "AutofitRowsforMergedCells_out.xlsx");

يمكنك أيضًا استخدام النسخ الزائد من طرق autoFitRows و autoFitColumns التي تقبل نطاق الصفوف/الأعمدة ونسخة من AutoFitterOptions لضبط تلقائي للصفوف/الأعمدة المختارة مع خيارات AutoFitterOptions المرغوب فيها.

توقيعات الأساليب المذكورة أعلاه كما يلي:

  1. autoFitRows(int startRow, int endRow, AutoFitterOptions options)
  2. autoFitColumns(int firstColumn, int lastColumn, AutoFitterOptions options)

مهم معرفته