使用C++获取单元格值的文本宽度

获取单元值的文本宽度

有时,开发者可能需要计算单元格值的宽度以进行布局安排。Aspose.Cells提供了CellsHelper::GetTextWidth方法,允许开发者获取单元格值的文本宽度。下列示例展示如何使用CellsHelper::GetTextWidth访问单元格值的文本宽度。

以下代码片段中使用的源文件,请参考附件。

源文件

示例代码

#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

    // Directory path for source files
    U16String sourceDir(u"..\\Data\\01_SourceDirectory\\");

    // Create workbook from the specified Excel file
    Workbook workbook(sourceDir + u"GetTextWidthSample.xlsx");

    // Calculate the text width for the string value of cell A1
    double textWidth = CellsHelper::GetTextWidth(workbook.GetWorksheets().Get(0).GetCells().Get(u"A1").GetStringValue(), workbook.GetDefaultStyle().GetFont(), 1);

    // Output the text width
    std::wcout << u"Text width: " << textWidth << std::endl;

    Aspose::Cells::Cleanup();
}