Satırların ve Sütunların Otomatik Sığdırması
Otomatik Uydurma
Aspose.Cells, bir Microsoft Excel dosyasını temsil eden bir Workbook sınıfını sağlar. Workbook sınıfı, bir Excel dosyasındaki her çalışma sayfasına erişimi sağlayan bir Worksheets koleksiyonu içerir. Bir çalışma sayfası, Worksheet sınıfı tarafından temsil edilir. Worksheet sınıfı, bir çalışma sayfasını yönetmek için geniş bir özellik ve yöntem yelpazesi sağlar. Bu makale, Worksheet sınıfını kullanarak satırları veya sütunları otomatik sığdırma üzerine odaklanmaktadır.
Satırı Otomatik Uydurma - Basit
Bir satırın genişlik ve yüksekliğini otomatik ayarlamak için en doğrudan yaklaşım, Worksheet sınıfının AutoFitRow yöntemini çağırmaktır. AutoFitRow yöntemi, yeniden boyutlandırılacak satırın dizinini (satırın dizinini) parametre olarak alır.
// 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(); |
Hücre Aralığında Satır Otomatik Sığdırma
Bir satır birçok sütundan oluşur. Aspose.Cells geliştiricilere bir satırın, satır içindeki hücre aralığındaki içeriğe göre otomatik olarak sığdırılmasına olanak tanır. Bunun için AutoFitRow yönteminin aşırı yüklenmiş bir sürümü çağrılır. 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.
AutoFitRow yöntemi, satırın tüm sütunlarının içeriğini kontrol eder ve ardından satırı otomatik olarak sığdırır.
// 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(); |
Hücre Aralığında Sütun Otomatik Sığdırma
Bir sütun birçok satırdan oluşur. Bir sütunun, sütun içindeki hücre aralığındaki içeriğe göre otomatik olarak sığdırılması için AutoFitColumn yönteminin aşırı yüklenmiş bir sürümü çağrılabilir. Aşağıdaki parametreleri alır:
- Sütun dizini, otomatik olarak sığdırılacak sütunun dizini.
- İlk satır indeksi, sütunun ilk satırının indeksi.
- Son satır indeksi, sütunun son satırının indeksi.
AutoFitColumn metodu sütundaki tüm satırların içeriğini kontrol eder ve ardından sütunu otomatik olarak uygun şekilde ayarlar.
// 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(); |
Birleştirilmiş Hücreler İçin Satırları Otomatik Uydurma
Aspose.Cells ile, AutoFitterOptions API’sını kullanarak birleştirilmiş hücreler için bile satırları otomatik uydurmak mümkündür. AutoFitterOptions sınıfı, birleştirilmiş hücreler için satırları otomatik olarak uydurmak için kullanılabilecek AutoFitMergedCellsType özelliğini sunar. AutoFitMergedCellsType , aşağıdaki üyeleri olan AutoFitMergedCellsType numaralı ‘Count’ numaralı numaralandırıcıyı kabul eder.
- None: Birleştirilmiş hücreleri görmezden gel.
- FirstLine: Yalnızca ilk satırın yüksekliğini genişletir.
- LastLine: Yalnızca son satırın yüksekliğini genişletir.
- EachLine: 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-.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"); |
AutoFitRows ve AutoFitColumns metotların yüklenmiş versiyonlarını kullanarak, aralık ve seçilen satır/sütunları otomatik olarak uygun şekilde ayarlamak için AutoFitterOptions örneği kullanabilirsiniz.
Yukarıdaki metotların imzaları aşağıdaki gibidir:
- AutoFitRows(int startRow, int endRow, AutoFitterOptions seçenekleri)
- AutoFitColumns(int firstColumn, int lastColumn, AutoFitterOptions seçenekleri)