我正在尝试在现有的 .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);
$this->filename 在我的课堂上可用。
一切正常。我得到与原始文件完全相同的内容,但是在启用内容时我得到了
运行时错误“9”:下标超出范围。
我在 github 和 wiki 上看到了很多帖子,但没有找到我需要的。这有点接近但它没有答案,或者它只是说 phpexcel 不支持 xlsm 文件。如果是这样,支持 xlsm 和宏的最佳库是什么?自3天以来,我一直坚持这一点。任何形式的建议和帮助都非常感谢。
谢谢。