有没有办法在PHPSpreadsheet中获取单元格中内容的宽度

Is there a way to get the width of contents in a cell in PHPSpreadsheet

我正在尝试将单元格的宽度设置为内容的宽度,这样就不会出现很多空白 space 我使用了以下内容。

$tmExport->getActiveSheet()->getColumnDimension($courseColumn)->setAutoSize(true);

所以我想使用

$tmExport->getActiveSheet()->getColumnDimension($courseColumn)->setWidth($contentWidth);

这是我实施的解决方案

    $cellValue = $tmExport->getActiveSheet()->getCell($courseCell)->getValue(); 
    $width     = mb_strwidth($cellValue);$lastCourseId = end($this->courses)->courseid;

    foreach ($this->courses as $courseItem) {
        if ($courseItem->courseid === $lastCourseId) {
            $tmExport->getActiveSheet()->getColumnDimension($courseColumn)->setWidth($width);
        } else {
            if ($width > $maxWidth) {
                $tmExport->getActiveSheet()->getColumnDimension($courseColumn)->setWidth($maxWidth);
            } else {
                $tmExport->getActiveSheet()->getColumnDimension($courseColumn)->setWidth($width);
            }
        }
    }`