我正在尝试将单元格的宽度设置为内容的宽度,这样就不会出现太多空白。我使用了以下内容。
$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);
}
}
}`