【发布时间】:2012-06-11 06:01:04
【问题描述】:
我正在使用 PHPExcel 导出数据以供下载。当打开下载的文件时,单元格的编号很大,它显示“#######”而不是值编号。我为每一列尝试了setAutoSize(),然后调用$sheet->calculateColumnWidths(),但它仍然没有改变。我看到 calculateColumnWidths() at here,@Mark Baker 说“calculateColumnWidths() 将值增加 5% 以尝试确保整个列适合”。如果单元格中的数字长度超过 5%,似乎确实解决了问题
更新 这是我自动调整列大小的功能:
function autoFitColumnWidthToContent($sheet, $fromCol, $toCol) {
if (empty($toCol) ) {//not defined the last column, set it the max one
$toCol = $sheet->getColumnDimension($sheet->getHighestColumn())->getColumnIndex();
}
for($i = $fromCol; $i <= $toCol; $i++) {
$sheet->getColumnDimension($i)->setAutoSize(true);
}
$sheet->calculateColumnWidths();
}
【问题讨论】:
-
您在计算中应用了哪种自动调整大小方法?大约还是精确?您是否使用任何特定的字体?还是粗体/斜体样式?这个数字计算得有多远?也许增加 10% 会比 5% 更好。
-
@MarkBaker 我已经为我的问题更新了示例代码。我使用了 ColumnDimension 类的 setAutoSize()。
标签: php width phpexcel autosize