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();
}