قياس عرض وارتفاع قيمة الخلية بوحدة البكسل باستخدام ++C
Contents
[
Hide
]
في بعض الأحيان تحتاج إلى حساب عرض وارتفاع قيمة الخلية لتناسب قيمة الخلية داخل الخلية. توفر Aspose.Cells الطرق Cell.GetWidthOfValue() و Cell.GetHeightOfValue() لهذا الغرض. من خلال استخدام هذه الأساليب يمكنك حساب عرض وارتفاع قيمة الخلية ثم تعيين عرض العمود وارتفاع الصف لتلك الخلية على التوالي وبعد ذلك سيتم ضبط أو تناسب قيمة الخلية داخل الخلية.
بدلاً من ذلك، يمكنك أيضًا تلقائية طول الأعمدة والصفوف لخلية أو نطاق خلايا باستخدام واجهات برمجة التطبيقات Aspose.Cells.
الكود التالي يشرح استخدام الطرق Cell.GetWidthOfValue() و Cell.GetHeightOfValue().
#include <iostream>
#include "Aspose.Cells.h"
using namespace Aspose::Cells;
int main()
{
Aspose::Cells::Startup();
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-C
// Create workbook object
Workbook workbook;
// Access first worksheet
Worksheet worksheet = workbook.GetWorksheets().Get(0);
// Access cell B2 and add some value inside it
Cell cell = worksheet.GetCells().Get(u"B2");
cell.PutValue(u"Welcome to Aspose!");
// Enlarge its font to size 16
Style style = cell.GetStyle();
style.GetFont().SetSize(16);
cell.SetStyle(style);
// Calculate the width and height of the cell value in unit of pixels
int32_t widthOfValue = cell.GetWidthOfValue();
int32_t heightOfValue = cell.GetHeightOfValue();
// Print both values
std::wcout << u"Width of Cell Value: " << widthOfValue << std::endl;
std::wcout << u"Height of Cell Value: " << heightOfValue << std::endl;
// Set the row height and column width to adjust/fit the cell value inside cell
worksheet.GetCells().SetColumnWidthPixel(1, widthOfValue);
worksheet.GetCells().SetRowHeightPixel(1, heightOfValue);
// Save the output excel file
U16String outFilePath = u"output_out.xlsx";
workbook.Save(outFilePath);
Aspose::Cells::Cleanup();
}