1

I am trying to load and write contents on already existing .xlsm templates and save it as a new file in different directory.These files has macros ad the macros needs to be protected as it were in the original file. I have done the following things in php to get the result :

 <?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 is available in my class.

Everything works fine. I get exactly the same content as the original file but while enabling content i get

Runtime error '9': Subscript out of range.

Cloned File | Original File

I saw number of posts on github and wiki but didn't find what i needed. This was some how close but it had no answer or it just said phpexcel doesn't support xlsm file. If so , what is the best library that would support xlsm and macros things ? I have stucked in this since 3 days. Any kinds of suggestion and help are really appreciated.
Thanks.

Tekraj Shrestha
  • 1,228
  • 3
  • 20
  • 48

0 Answers0