قياس العرض والارتفاع لقيمة الخلية بوحدات البكسل
في بعض الأحيان تحتاج إلى حساب عرض وارتفاع قيمة الخلية لتناسب قيمة الخلية داخل الخلية. توفر Aspose.Cells الطرق Cell.GetWidthOfValue() و Cell.GetHeightOfValue() لهذا الغرض. من خلال استخدام هذه الأساليب يمكنك حساب عرض وارتفاع قيمة الخلية ثم تعيين عرض العمود وارتفاع الصف لتلك الخلية على التوالي وبعد ذلك سيتم ضبط أو تناسب قيمة الخلية داخل الخلية.
بالإضافة إلى ذلك، يمكنك أيضًا ضبط تلقائي للصفوف والأعمدة في الخلية أو نطاق الخلايا باستخدام واجهات برمجة التطبيقات Aspose.Cells.
الكود التالي يشرح استخدام الطرق Cell.GetWidthOfValue() و Cell.GetHeightOfValue().
// 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); | |
// Create workbook object | |
Workbook workbook = new Workbook(); | |
// Access first worksheet | |
Worksheet worksheet = workbook.Worksheets[0]; | |
// Access cell B2 and add some value inside it | |
Cell cell = worksheet.Cells["B2"]; | |
cell.PutValue("Welcome to Aspose!"); | |
// Enlarge its font to size 16 | |
Style style = cell.GetStyle(); | |
style.Font.Size = 16; | |
cell.SetStyle(style); | |
// Calculate the width and height of the cell value in unit of pixels | |
int widthOfValue = cell.GetWidthOfValue(); | |
int heightOfValue = cell.GetHeightOfValue(); | |
// Print both values | |
Console.WriteLine("Width of Cell Value: " + widthOfValue); | |
Console.WriteLine("Height of Cell Value: " + heightOfValue); | |
// Set the row height and column width to adjust/fit the cell value inside cell | |
worksheet.Cells.SetColumnWidthPixel(1, widthOfValue); | |
worksheet.Cells.SetRowHeightPixel(1, heightOfValue); | |
// Save the output excel file | |
workbook.Save(dataDir + "output_out.xlsx"); |