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

时间:2019-10-30 09:40:31

标签: php codeigniter phpspreadsheet

我正在尝试将单元格的宽度设置为内容的宽度,这样就不会发生很多空白。

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

所以我想使用

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

1 个答案:

答案 0 :(得分:0)

这是我实施的解决方案

    $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);
            }
        }
    }`