代码之家  ›  专栏  ›  技术社区  ›  Tekraj Shrestha

无法为用PhpExcel生成的.xlsm文件启用内容

  •  0
  • Tekraj Shrestha  · 技术社区  · 6 年前

    我正在尝试在现有的.xlsm模板上加载和写入内容,并将其另存为不同目录中的新文件。这些文件中有宏,宏需要像在原始文件中一样受到保护。为了得到结果,我在php中做了以下工作:

     <?php
    
        error_reporting(E_ALL);
        ini_set('display_errors', TRUE);
        ini_set('display_startup_errors', TRUE);
    
        require_once dirname(__FILE__) . '/php_excel/Classes/PHPExcel.php';
        require_once dirname(__FILE__) . '/php_excel/Classes/PHPExcel/IOFactory.php';
        require_once dirname(__FILE__) . '/php_excel/Classes/PHPExcel/Reader/IReadFilter.php';
        date_default_timezone_set('Europe/London');
    
        $sheets = array('Welcome', 'Instructions', 'Data Definitions', 'Clothing');
    
        $inputFileName = __DIR__ . '/templates/Clothing.xlsm';
    
        $inputFileType = PHPExcel_IOFactory::identify($inputFileName);
    
        $this->PHPExcelReader = PHPExcel_IOFactory::createReader($inputFileType);
        $sheetnames = $sheets;
        $this->PHPExcelReader->setLoadSheetsOnly($sheetnames);
        $this->PHPExcel = $this->PHPExcelReader->load($inputFileName);
    
    
        $this->PHPExcel->setActiveSheetIndex(3);
    
        // Redirect output to a client’s web browser (Excel2007)
        ob_end_clean();
        header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
        header('Content-Disposition: attachment;filename="test.xlsm"');
        header('Cache-Control: max-age=0');
    
        $objWriter = PHPExcel_IOFactory::createWriter($this->PHPExcel, 'Excel2007');
        ob_end_clean();
        $objWriter->save($this->filename);
    
        $this->PHPExcel->disconnectWorksheets();
        unset($this->PHPExcel);
    

    一切正常。我得到的内容与原始文件完全相同,但在启用内容时

    运行时错误“9”: 下标超出范围。

    Cloned File | Original File

    我在github和wiki上看到了很多帖子,但没有找到我需要的。 This was some how close 但它没有答案,或者只是说phpexcel不支持xlsm文件。如果是这样,那么支持xlsm和宏的最佳库是什么?三天以来我一直在这方面结巴。 非常感谢您的建议和帮助。
    谢谢。

    0 回复  |  直到 6 年前
    推荐文章