Satır ve Sütunları Otomatik Uydur
Otomatik Uydurma
Aspose.Cells, Workbook adında bir Microsoft Excel dosyasını temsil eden bir sınıf sağlar. Workbook sınıfı, Excel dosyasındaki her bir çalışma sayfasına erişim sağlayan Worksheets kolleksiyonuna sahiptir.
Bir çalışma sayfası Worksheet sınıfı tarafından temsil edilir. Worksheet sınıfı, çalışma sayfasını yönetmek için geniş bir yelpazede özellikler ve yöntemler sağlar. Bu makale, Worksheet sınıfını kullanarak satırları veya sütunları otomatik olarak uyarlama konusuna bakmaktadır.
Satırı Otomatik Uydurma - Basit
Bir satırın genişlik ve yüksekliğini otomatik olarak ayarlamanın en basit yaklaşımı, Worksheet sınıfının autoFitRow yöntemini çağırmaktır. autoFitRow yöntemi, bir parametre olarak bir satır dizini (yeniden boyutlandırılacak satırın dizini) alır.
// 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."); |
Hücrelerin Aralığında Satırı Otomatik Uydur
Bir satır, birçok sütundan oluşur. Aspose.Cells, geliştiricilere bir satırın içindeki hücrelerin bir aralığındaki içeriğe bağlı olarak bir satırı otomatik olarak uyarlama olanağı sağlar. Bu, autoFitRow yönteminin birden fazla parametre alan aşırı yüklenmiş bir sürümünü çağırarak gerçekleştirilir. Aşağıdaki parametreleri alır:
- Satır dizini, otomatik olarak uyarlama yapılacak satırın dizini.
- İlk sütun dizini, satırın ilk sütununun dizini.
- Son sütun dizini, satırın son sütununun dizini.
The autoFitRow metodu, satırdaki tüm sütunların içeriğini kontrol eder ve ardından satırı otomatik olarak uygun hale getirir.
// 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."); |
Kolay Otomatik Uydurma
Bir sütunun genişlik ve yüksekliğini otomatik olarak ayarlamanın en kolay yolu, Worksheet sınıfının autoFitColumn metodunu çağırmaktır. autoFitColumn metodu, genişletilecek sütunun sütun indeksini parametre olarak alır.
// 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."); |
Hücre Aralığındaki Otomatik Uydurma Sütunu
Bir sütun, birçok satırdan oluşur. autoFitColumn metodunun aşırı yüklenmiş bir versiyonunu çağırarak bir sütundaki hücre aralığındaki içeriğe göre bir sütunu otomatik olarak uydurmak mümkündür. Bu versiyon, aşağıdaki parametreleri alan bir autoFitColumn metodunu alır:
- Sütun indeksi, otomatik olarak uygun hale getirilmesi gereken sütunun indeksini temsil eder
- İlk satır indeksi, sütunun ilk satırının indeksini temsil eder.
- Son satır indeksi, sütunun son satırının indeksini temsil eder.
autoFitColumn metodu, tüm sütundaki tüm satırların içeriğini kontrol eder ve ardından sütunu otomatik olarak uygun hale getirir.
// 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."); |
Birleştirilmiş Hücreler için Satırları Otomatik Uydurma
Aspose.Cells ile AutoFitterOptions API’sını kullanarak birleştirilmiş hücreler için satırları otomatik olarak genişletebilirsiniz. AutoFitterOptions sınıfı, birleştirilmiş hücreler için satırları otomatik olarak genişletmek için kullanılabilecek bir AutoFitMergedCellsType özelliğini sağlar. AutoFitMergedCellsType, aşağıdaki üyelere sahip olan bir AutoFitMergedCellsType numaralandırmayı kabul eder:
- YOK: Birleştirilmiş hücreleri yok sayar.
- İLK_SATIR: Sadece ilk satırın yüksekliğini genişletir.
- SON_SATIR: Sadece son satırın yüksekliğini genişletir.
- HER_SATIR: Her satırın yüksekliğini genişletir.
// 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"); |
Seçilen satırları/sütunları ve istenen AutoFitterOptions ile otomatik olarak uygun hale getirmek için autoFitRows ve autoFitColumns metodlarının aşırı yüklenmiş versiyonlarını ve AutoFitterOptions örneğini kullanabilirsiniz.
Yukarıda bahsedilen metodların imzaları aşağıdaki gibidir:
- autoFitRows(int startRow, int endRow, AutoFitterOptions options)
- autoFitColumns(int firstColumn, int lastColumn, AutoFitterOptions options)