PhpSpreadsheet导出Excel图表在MS Excel空白,如何显示隐藏行数据?
解决PhpSpreadsheet导出图表在MS Excel中因隐藏行空白的问题
你遇到的这个场景我太有共鸣了——用PhpSpreadsheet生成带图表的Excel文件,隐藏了数据行后,LibreOffice能完美渲染图表,但到了MS Excel里图表区域直接空白。本质原因就是你代码里的一个关键参数设置和需求反向了。
看你提供的图表构建代码,在创建Chart对象时,第5个参数是plotVisibleOnly,你现在传的是true:
$chart = new PhpOffice\PhpSpreadsheet\Chart\Chart( 'chart1', // name $title, // title $legend, // legend $plotArea, // plotArea true, // plotVisibleOnly ← 就是这个参数 0, // displayBlanksAs null, // xAxisLabel $yAxisLabel // yAxisLabel );
这个参数的作用是控制图表是否仅引用可见单元格的数据,设为true时,MS Excel会忽略隐藏行里的数据,导致图表空白;而LibreOffice默认会读取隐藏数据,所以显示正常。
解决方案
只需要把plotVisibleOnly的参数值从true改成false即可:
$chart = new PhpOffice\PhpSpreadsheet\Chart\Chart( 'chart1', // name $title, // title $legend, // legend $plotArea, // plotArea false, // plotVisibleOnly → 修改为false,允许图表使用隐藏数据 0, // displayBlanksAs null, // xAxisLabel $yAxisLabel // yAxisLabel );
修改后重新导出Excel文件,MS Excel就会自动加载隐藏行里的数据来渲染图表,不需要手动在Excel里调整任何设置,和LibreOffice的显示效果保持一致。
内容的提问来源于stack exchange,提问作者Ryan Arief
相关产品推荐
相关产品推荐

