Link to home
Start Free TrialLog in
Avatar of APD Toronto
APD TorontoFlag for Canada

asked on

PHPExcel Deletes Embedded VBA

Hi Experts.

I'm developing a PHP Application that needs to write data to an Excel report.  To do this, I'm using a pre-created Excel file (.xls, not .xlt) that has buttons and VBA, copy it, then use the PHPExcel library to update the copied file, using the following code:

	private static function excel_create_file($title, $with_compare, $sess_id, $date_range_1, $date_range_2,&$db){
        
        //copy template file
       
            //Temporarily Change directory to root to copy files...
            $currentDIR = getcwd();
            chdir("../");
            $rootDIR = getcwd();
            //Change DIR back
            chdir($currentDIR);
            
        $slash = DIRECTORY_SEPARATOR;

        //echo 'dir = ' . $currentDIR . '<br>';
        
        $file_name = "SearchReport_" . $sess_id . ".xls";
        
        $file_from = $rootDIR . $slash . "search_bookings" . $slash . "report_template.xls";
        $file_physical = $rootDIR . $slash . "downloads" . $slash . $file_name;
        
        global $env;
        $file_virtual = $env['url'] . '/downloads/' . $file_name;
        
        $copy_success = copy($file_from, $file_physical);
        
    /** Include PHPExcel */
        error_reporting(E_ALL);
        set_time_limit(0);
                
        $fileType = 'Excel5';
        
        require_once '../excel_lib/PHPExcel/IOFactory.php';
        
        // Read the file
        $objReader = PHPExcel_IOFactory::createReader($fileType);
        $objPHPExcel = $objReader->load($file_physical);

        // Admin/cover sheet
        self::excel_sheet_admin($objPHPExcel, $title, $date_range_1, $date_range_2);
/*        
        //Create 1 product sheets
        self::excel_sheet_1_product($objPHPExcel, 'Loc_CSSTAlpha', TRUE, $with_compare, $sess_id, $db); //Report 4.4
*/
        // Write the file
        $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, $fileType);
        $objWriter->save($file_physical);
        
        return $file_virtual;
        
    }
    
    private static function excel_sheet_admin(&$excel_obj, $title, $date_range_1, $date_range_2){
        
        $excel_obj->setActiveSheetIndexByName('Admin')
                  ->setCellValue('B3', $title)
                  ->setCellValue('B4', $date_range_1)
                  ->setCellValue('B5', $date_range_2);
    }
    

Open in new window


if I comment out Lines 27-47 (the PHPExcel) the buttons and their VBA are maintained, but as soon as I release these lines, and run the above code, I lose my buttons and VBA. Even hitting Alt+F11 my VBA code is deleted.

How can I populate an existing Excel file while maintaining the buttons and VBA?

I know that VBA is not PHP, but Excel is VBA.

Any feedback will be greatly appreciated.
Avatar of Scott Fell
Scott Fell
Flag of United States of America image

Is your server windows or linux?  I wonder if you would need to edit it using .NET in order to preserve any vba.  If that is the case, create a .NET page to edit the excel file. Have the .NET page accept a get or post like an internal web service and to hit it from PHP using cURL would be the only thing I can think of.
Avatar of APD Toronto

ASKER

It's Linux.  Can it be the file type (line 30)?
You can read about the file types in the documentation https://github.com/PHPOffice/PHPExcel/blob/1.8/Documentation/PHPExcel%20developer%20documentation.doc but that is not the issue.

Using this PHPExcel to access an excel file and edit it does not do the same thing as if you manually updated the file.  PHPExcel will read what it can leaving out VBA, images etc. then follow your instructions to update data and recreate it.  I would bet if you use a chart function, it does not look the same because it uses a 3rd party chart library to create the chart. (http://jpgraph.net/)

The answer is you you can't do what you want with what you are using.
Any other php tool, or suggestion?
ASKER CERTIFIED SOLUTION
Avatar of Scott Fell
Scott Fell
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I am out right now, but I found this

http://phpexcel.codeplex.com/workitem/19017

As well, what do you think about JS?
That looks promising.
Do you have any idea on how to install and use it?
I'm just starting with PHPExcel myself.  I can't come back to this until later next week though.  Looking at the files in the link you provided it looks like you may have to have access to the macros separate?
What do you mean separate?

I am also trying the code they posted, which I am including below, but I am missing IOFactory.php, which was part of the original package. I am assuming that I need to copy/paste files.