{"record":{"id":"919d10d8849c8e13","repo":"PHPOffice/PhpSpreadsheet","slug":"no-writer-found-for-type-writertype","errorCode":null,"errorMessage":"No writer found for type $writerType","messagePattern":"No writer found for type \\$writerType","errorType":"exception","errorClass":"PhpOffice\\PhpSpreadsheet\\Writer\\Exception","httpStatus":null,"severity":"error","filePath":"src/PhpSpreadsheet/IOFactory.php","lineNumber":94,"sourceCode":"            self::WRITER_ODS => Writer\\Ods::class,\n            self::WRITER_CSV => Writer\\Csv::class,\n            self::WRITER_HTML => Writer\\Html::class,\n            'Tcpdf' => Writer\\Pdf\\Tcpdf::class,\n            'Dompdf' => Writer\\Pdf\\Dompdf::class,\n            'Mpdf' => Writer\\Pdf\\Mpdf::class,\n        ];\n    }\n\n    /**\n     * Create Writer\\IWriter.\n     */\n    public static function createWriter(Spreadsheet $spreadsheet, string $writerType): IWriter\n    {\n        /** @var class-string<IWriter> */\n        $className = $writerType;\n        if (!in_array($writerType, self::$writers, true)) {\n            if (!isset(self::$writers[$writerType])) {\n                throw new Writer\\Exception(\"No writer found for type $writerType\");\n            }\n\n            // Instantiate writer\n            $className = self::$writers[$writerType];\n        }\n\n        return new $className($spreadsheet);\n    }\n\n    /**\n     * Create IReader.\n     *\n     * @param array<string, class-string<IReader>> $mergeArray\n     *        Array to use to find reader, self::$readers will be used if $readers is empty.\n     */\n    public static function createReader(string $readerType, array $mergeArray = []): IReader\n    {\n        /** @var class-string<IReader> */","sourceCodeStart":76,"sourceCodeEnd":112,"githubUrl":"https://github.com/PHPOffice/PhpSpreadsheet/blob/65b080eef4d9fd11a5796135ab145883e5c3d6a6/src/PhpSpreadsheet/IOFactory.php#L76-L112","documentation":"IOFactory::createWriter() accepts either one of the registered short names ('Xls', 'Xlsx', 'Ods', 'Csv', 'Html', plus the PDF writers 'Tcpdf', 'Dompdf', 'Mpdf' and anything added via registerWriter()) or a full writer class-string. The lookup is case-sensitive: a string that is neither a registered value (a class name) nor a registered key throws immediately, before any file work starts.","triggerScenarios":"IOFactory::createWriter($spreadsheet, 'xlsx') with lowercase; 'Excel2007'/'Excel5' (pre-1.0 PHPExcel names); any typo or stray whitespace like 'Xlsx '.","commonSituations":"Deriving the writer type from pathinfo() extension and passing it unnormalized; following old PHPExcel tutorials; case differences introduced during refactors.","solutions":["Use the exact constants: IOFactory::WRITER_XLSX ('Xlsx'), WRITER_XLS ('Xls'), WRITER_CSV ('Csv'), WRITER_HTML ('Html'), WRITER_ODS ('Ods')","Map PHPExcel-era names: 'Excel2007' -> 'Xlsx', 'Excel5' -> 'Xls', 'OOCalc' -> 'Ods'","Normalize extension-derived strings with ucfirst() before passing, or pass a class-string like \\PhpOffice\\PhpSpreadsheet\\Writer\\Xlsx::class"],"exampleFix":"// before\n$type = strtolower(pathinfo($filename, PATHINFO_EXTENSION)); // 'xlsx'\n$writer = IOFactory::createWriter($spreadsheet, $type);\n\n// after\n$type = ucfirst(strtolower(pathinfo($filename, PATHINFO_EXTENSION)));\n$writer = IOFactory::createWriter($spreadsheet, $type); // 'Xlsx'","handlingStrategy":"validation","validationCode":"const KNOWN_WRITERS = ['Xls', 'Xlsx', 'Ods', 'Csv', 'Html', 'Tcpdf', 'Dompdf', 'Mpdf'];\n\n/** Accept user input, extension, or PHPExcel-era names; return a valid writer type. */\nfunction normalizeWriterType(string $type): string\n{\n    $aliases = [\n        'excel2007' => 'Xlsx', 'excel5' => 'Xls', 'oocalc' => 'Ods',\n        'excel' => 'Xlsx', 'pdf' => 'Mpdf',\n    ];\n    $type = $aliases[strtolower(trim($type))] ?? ucfirst(strtolower(trim($type)));\n    if (!in_array($type, KNOWN_WRITERS, true)\n        && !is_a($type, \\PhpOffice\\PhpSpreadsheet\\Writer\\IWriter::class, true)) {\n        throw new InvalidArgumentException(\"Unknown writer type: $type\");\n    }\n\n    return $type;\n}\n\n$writer = IOFactory::createWriter($spreadsheet, normalizeWriterType($userType));","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Pass IOFactory constants (IOFactory::WRITER_XLSX etc.) instead of string literals","When deriving from a file extension, normalize with ucfirst(strtolower(...)) first","Add a mapping table for legacy PHPExcel names when migrating old codebases"],"tags":["phpspreadsheet","iofactory","writer","invalid-type"],"backgroundTag":"unknown-writer-type","analyzedSha":"65b080eef4d9fd11a5796135ab145883e5c3d6a6","analyzedAt":"2026-08-17T05:40:41.646Z","schemaVersion":2},"datasetVersion":"2026-08-17T09:17:11.063Z"}