PHPSpreadsheet折线图数据系列自定义颜色引发文件损坏问题求助
I’ve run into exactly this problem before—setting custom line colors for PHPSpreadsheet line charts leads to Excel throwing that "Removed Part: /xl/drawings/drawing1.xml" error. The root cause is almost always incorrect color formatting or misusing the chart series objects. Here’s how to fix it:
Use the correct color format (ARGB, no # symbol)
PHPSpreadsheet expects 8-digit ARGB values (the first two digits are alpha/transparency,FFfor fully opaque) without a leading#. Using 6-digit RGB or including the#will generate invalid XML that Excel can’t parse. You can also use built-in color constants for reliability.Example of correct line color setup:
// Get your target chart series $plotGroup = $chart->getPlotArea()->getPlotGroupByIndex(0); $series = $plotGroup->getPlotSeriesByIndex(0); // Set line color (blue, fully opaque) $series->getLine()->setColor(new \PhpOffice\PhpSpreadsheet\Style\Color('FF0000FF')); // Alternatively, use a built-in constant $series->getLine()->setColor(new \PhpOffice\PhpSpreadsheet\Style\Color(\PhpOffice\PhpSpreadsheet\Style\Color::COLOR_BLUE));Target the Line object, not Fill
A common mistake is trying to set theFillproperty for a line chart series—this is meant for area fills, not the line itself. Always usegetLine()->setColor()to modify the line’s color. Setting Fill where it’s not supported will break the drawing XML.Verify you’re modifying the right series
Double-check that you’re accessing the correct plot group and series withgetPlotGroupByIndex()andgetPlotSeriesByIndex(). Accidentally modifying the plot area or a non-series object can corrupt the chart structure.Upgrade PHPSpreadsheet to the latest stable version
Older versions (pre-2.x) had known bugs in chart XML generation, especially around color handling. Updating to the latest release often resolves these edge cases.Debug the XML directly (if needed)
If you’re still stuck, rename your.xlsxfile to.zip, extract it, and openxl/drawings/drawing1.xml. Look for color nodes—they should look like<a:srgbClr val="FF0000FF"/>(no#). If you see invalid formatting here, that’s the issue to fix.
内容的提问来源于stack exchange,提问作者karadorde

