PHPExcel 导出Excel报错:PHPExcel_IOFactory::load()

背景

  • 近期在做 excel文件数据导出时,遇到如下报错: iconv(): Detected an illegal character in input string
  • 场景:计划任务后台,分步导出 大数据 excel文件
  • 发现在加载文件时,会有报错

报错信息 如下:

{
	"name": "PHP Notice",
	"message": "iconv(): Detected an illegal character in input string",
	"code": 8,
	"type": "yii\\base\\ErrorException",
	"file": "/usr/share/nginx/html/ycsh_automat/Classes/PHPExcel/Shared/String.php",
	"line": 490,
	"stack-trace": [
	  "#0 [internal function]: yii\\base\\ErrorHandler->handleError(8, 'iconv(): Detect...', '/usr/share/ngin...', 493, Array)", 
	  "#1 /usr/share/nginx/html/ycsh_automat/Classes/PHPExcel/Shared/String.php(490): iconv('CP1252', 'UTF-8', '\\xE8\\xAE\\xA2\\xE5\\x8D\\x95\\xE5\\x88\\x97\\xE8\\xA1\\xA8\\x00\\x00\\x00...')", 
	  "#2 /usr/share/nginx/html/ycsh_automat/Classes/PHPExcel/Reader/Excel5.php(1362): PHPExcel_Shared_String::ConvertEncoding('\\xE8\\xAE\\xA2\\xE5\\x8D\\x95\\xE5\\x88\\x97\\xE8\\xA1\\xA8\\x00\\x00\\x00...', 'UTF-8', 'CP1252')", 
	  "#3 /usr/share/nginx/html/ycsh_automat/Classes/PHPExcel/Reader/Excel5.php(633): PHPExcel_Reader_Excel5->readSummaryInformation()", 
	  "#4 /usr/share/nginx/html/ycsh_automat/Classes/PHPExcel/IOFactory.php(192): PHPExcel_Reader_Excel5->load('/usr/share/ngin...')", 
	  "#5 /usr/share/nginx/html/ycsh_automat/common/services/ExcelMzService.php(154): PHPExcel_IOFactory::load('/usr/share/ngin...')", 
	  "#6 /usr/share/nginx/html/ycsh_automat/common/models/ExportTask.php(306): common\\services\\ExcelMzService->cronCreateExportFile(Array, Array, '/usr/share/ngin...', Array, '\\xE8\\xAE\\xA2\\xE5\\x8D\\x95\\xE5\\x88\\x97\\xE8\\xA1\\xA8')", 
	  "#7 /usr/share/nginx/html/ycsh_automat/api/controllers/CronController.php(54): common\\models\\ExportTask::execExportTask(Object(common\\models\\ExportTask), 1, '{\"search_params...')", 
	  "#8 [internal function]: api\\controllers\\CronController->actionRunExportTask()", 
	  ...
}

问题分析

  1. 根据详细报错信息,可以知道,是执行 PHPExcel_IOFactory::load() 方法时,因为文件名编码问题造成程序中断
  2. 业务场景中,即在分步追加 excel文件数据,加载文件名时,识别转码有误
  3. 继而定位到 Classes\PHPExcel\Shared\String.phpConvertEncoding() 方法的的第一个条件判断不满足

解决方案

  • 修改文件 Classes\PHPExcel\Shared\String.phpConvertEncoding() 方法的的第一个条件判断,如下
    PHPExcel 导出Excel报错:PHPExcel_IOFactory::load()_第1张图片

附录

  1. 网上的经验,基本为 php iconv() : Detected an illegal character in input string, 测试发现都无法解决我的问题
  2. 重点提示:当前报错,在 windows 环境中,不会报错,只有上传到 Linux 服务器,才会有报错!

你可能感兴趣的:(excel,php)