تناسب الصفوف والأعمدة تلقائيًا
ضبط تلقائي
توفر 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. يأخذ الأسلوب 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(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 المطلوبة وفقًا لذلك.
توقيعات الأساليب المذكورة أعلاه كما يلي:
- autoFitRows(int startRow, int endRow, AutoFitterOptions options)
- autoFitColumns(int firstColumn, int lastColumn, AutoFitterOptions options)