You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PhpSpreadsheet部署Ubuntu服务器后类找不到问题及解决

PhpSpreadsheet部署到Ubuntu服务器后出现类找不到的致命错误

本地通过Composer安装PhpSpreadsheet后代码可正常运行,但部署到DigitalOcean的Ubuntu服务器后,触发以下PHP致命错误:

[Thu Oct 20 02:05:24.760534 2022] [php:error] [pid 817] [client 181.129.171.154:7921] PHP致命错误:未捕获的Error:在/var/www/Ausen2/logic/ausen_excel.php第16行找不到类“PhpOffice\PhpSpreadsheet\SpreadSheet”
堆栈跟踪:
#0 {main}
在/var/www/Ausen2/logic/ausen_excel.php第16行抛出

相关代码

<?php

require '../vendor/autoload.php';


use PhpOffice\PhpSpreadsheet\{SpreadSheet, IOFactory};
use PhpOffice\PhpSpreadsheet\Writer\Xlsx; // 支持Csv、Xls格式

require("../db.php");

session_start();
$sqli = $_SESSION['ausen_list'];

//$nombre_admin   = $_SESSION['NOM_USUARIO'];
//$id_admin = $_SESSION['ID_USUARIO'];


// 创建Excel实例
$excel =new SpreadSheet();

// 设置元数据
$excel -> getProperties()->setCreator("Andres")->setTitle("我的Excel");
$excel->setActiveSheetIndex(0);  
$hojaActiva = $excel->getActiveSheet();
$hojaActiva->setTitle("员工列表");


// 设置表头信息
$hojaActiva->setCellValue('A1','员工证件号');  $hojaActiva->getColumnDimension('A')->setWidth(15);    
$hojaActiva->setCellValue('B1','开始日期');  $hojaActiva->getColumnDimension('B')->setWidth(15);    
$hojaActiva->setCellValue('C1','结束日期');   $hojaActiva->getColumnDimension('C')->setWidth(15);
$hojaActiva->setCellValue('D1','时长');
$hojaActiva->setCellValue('E1','备注'); $hojaActiva->getColumnDimension('E')->setWidth(20);
$hojaActiva->setCellValue('F1','工作安全');  $hojaActiva->getColumnDimension('F')->setWidth(15);
$hojaActiva->setCellValue('G1','用户ID');     $hojaActiva->getColumnDimension('G')->setWidth(15);
$hojaActiva->setCellValue('H1','缺勤类型');  $hojaActiva->getColumnDimension('H')->setWidth(15);


$ausentismos = $conectar->query($sqli);

$fila = 2; 
while($ausentismo = $ausentismos->fetch_assoc()){
    $hojaActiva->setCellValue('A'.$fila, $ausentismo['Cedula_F']);
    $hojaActiva->setCellValue('B'.$fila, $ausentismo['Fecha_Inicio']);
    $hojaActiva->setCellValue('C'.$fila, $ausentismo['Fecha_Fin']);
    $hojaActiva->setCellValue('D'.$fila, $ausentismo['Tiempo']);
    $hojaActiva->setCellValue('E'.$fila, $ausentismo['Observacion']);
    $hojaActiva->setCellValue('F'.$fila, $ausentismo['Seguridad_Trabajo']);
    $hojaActiva->setCellValue('G'.$fila, $ausentismo['ID_Usuario']);
        
    if($ausentismo['Tipo_Ausentismo'] == 1){
            $hojaActiva->setCellValue('H'.$fila, '病假');
    }elseif ($ausentismo['Tipo_Ausentismo'] == 2){
            $hojaActiva->setCellValue('H'.$fila, '调休');
    }elseif ($ausentismo['Tipo_Ausentismo'] == 3){
            $hojaActiva->setCellValue('H'.$fila, '事假');
    }elseif ($ausentismo['Tipo_Ausentismo'] == 4){
            $hojaActiva->setCellValue('H'.$fila, '休假');
    }

    $fila++;
}

// 输出Excel文件
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="报告.xlsx"');
header('Cache-Control: max-age=0');
$writer = IOFactory::createWriter($excel, 'Xlsx'); 
$writer->save('php://output');
exit; 
?>

排查步骤

已尝试以下操作但未解决问题:

  • 重新安装Composer
  • 执行composer dump-autoload命令

问题原因与解决方案

问题根源是文件系统大小写敏感性差异:

  • 本地环境(通常为Windows,文件系统大小写不敏感)中,使用SpreadSheet(第二个字母大写S)可以正常匹配类文件
  • Ubuntu服务器(Linux系统,文件系统大小写敏感)中,PhpSpreadsheet库的实际类名为Spreadsheet(第二个字母小写s)

只需修改代码中所有的SpreadSheet为Spreadsheet即可解决:

  1. 更新use导入语句:use PhpOffice\PhpSpreadsheet\{Spreadsheet, IOFactory};
  2. 修改实例化代码:$excel = new Spreadsheet();

内容的提问来源于stack exchange,提问作者AndresDM

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.15 21:55:19