工作表标题中找到无效字符

BT6*_*643 8 php phpexcel

我正在尝试使用PHPExcel从某个源加载Excel文件.我无法控制如何生成这些Excel文件,并且需要使用PHPExcel自动打开它们而无需人工交互(重新保存文件等).

我收到以下错误:

致命错误:在第418行的C:\ path\to\PHPExcel\Worksheet.php中显示消息'表单标题中找到无效字符'的未捕获异常'Exception'

load()行发生错误,使用以下代码打开文件:

$reader = PHPExcel_IOFactory::createReader('Excel5');
$excel = $reader->load($filename_xls);
Run Code Online (Sandbox Code Playgroud)

表格标题与我们无关,所以可以忽略它吗?这样忽略错误?

oor*_*ori 14

你真的不需要破解核心,只需在你自己的代码中添加:

// $invalidCharacters = array('*', ':', '/', '\\', '?', '[', ']');
$invalidCharacters = $worksheet->getInvalidCharacters();
$title = str_replace($invalidCharacters, '', $title);
Run Code Online (Sandbox Code Playgroud)

Worksheet.php公开getInvalidCharacters()你可以使用的公共功能.或者变得懒惰并直接使用array()(从Workbook.php定义中复制和粘贴)


BT6*_*643 13

我们刚刚完成此操作以使其现在排序.它可能非常糟糕,我不会真的建议其他人这样做,但是嘿,它应该对我们有用!

1.7.8PHPExcel的版本中,在/Classes/PHPExcel/Worksheet.php周围line 414- 交换以下checkSheetTitle()函数:

private static function _checkSheetTitle($pValue)
{
    // Some of the printable ASCII characters are invalid:  * : / \ ? [ ]
    if (str_replace(self::$_invalidCharacters, '', $pValue) !== $pValue) {
        //throw new Exception('Invalid character found in sheet title');

        //Hack to remove bad characters from sheet name instead of throwing an exception
        return str_replace(self::$_invalidCharacters, '', $pValue);
    }

    // Maximum 31 characters allowed for sheet title
    if (PHPExcel_Shared_String::CountCharacters($pValue) > 31) {
        throw new Exception('Maximum 31 characters allowed in sheet title.');
    }

    return $pValue;
}
Run Code Online (Sandbox Code Playgroud)