XLS ist empty after saving 84 times?

Topics: Developer Forum, User Forum
Feb 3, 2014 at 8:41 AM
Hi,

I have a project were I'm trying to save posts from a wordpress database to a .xls File.

At first, I clone another xls file, which has 3 rows already filled with data
        require_once plugin_dir_path(__FILE__).'phpExcel/PHPExcel/IOFactory.php';
            
        $inputFileName = plugin_dir_path(__FILE__).'template.xls';
        $objReader = new PHPExcel_Reader_Excel5();
        $objPHPExcel = $objReader->load($inputFileName);            
        
        $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');  
        $file = 'listing-'.time().'-'.uniqid().'.xls';
        $objWriter->save( plugin_dir_path(__FILE__).'tmp/'.$file );
I'm calling this code via ajax and retrieve the new filename. Now another script will write the posts from the database to the xls file. ( 130 posts with one row per post at the moment ).
function addPostsToFile(){
                
        ini_set('memory_limit', '92M');
        
        require_once plugin_dir_path(__FILE__).'phpExcel/PHPExcel/IOFactory.php';
        require_once plugin_dir_path(__FILE__).'helperFunctions.php';
            
        $file = plugin_dir_path(__FILE__).'tmp/'.$_POST['file'];        
        
        $objReader = new PHPExcel_Reader_Excel5();
        $objPHPExcel = $objReader->load($file);
        
        $objPHPExcel->setActiveSheetIndex(0);       
            
        foreach( $_POST['posts'] as $post_id ) {
            add_Post_to_XLS( intval($post_id), $objPHPExcel);       
        }
        
        $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');  
        $objWriter->save( $file );
        
        die();
    }
This works at least for now. But I already have a few thousands of database querys to get the data to fill in ( and it will be rapidly growing ). So I thought it is a good idea to add only one post at once.

    function addPostToFile(){
        
        ini_set('memory_limit', '92M');
        
        require_once plugin_dir_path(__FILE__).'phpExcel/PHPExcel/IOFactory.php';
        require_once plugin_dir_path(__FILE__).'helperFunctions.php';
            
        $file = plugin_dir_path(__FILE__).'tmp/'.$_POST['file'];
        
        $objReader = new PHPExcel_Reader_Excel5();
        $objPHPExcel = $objReader->load($file);
        
        $objPHPExcel->setActiveSheetIndex(0);
            
        add_Post_to_XLS( intval($_POST['post_id']), $objPHPExcel);      
        
        $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');  
        $objWriter->save( $file );
        
        die();
    }
So I loop my posts with ajax and call the function above, which is almost the same but of course the loading and saving of the xls file will be repeated for each post ( remember, 130 post atm).

now I have the strange problem, that this works great for the first 83 posts ( 86 rows filled ), but it always fails for each call after that ( no matter what individual post is added ). The new rows are successfully added to the $objPHPExcel, but after saving the .xls file just contains the 3 rows from the template.

When I stop the script after 82 posts, the cells are filled as wanted.
When I add 2 rows per post the script fails at row 171 (even after saving 84 times).
When I change the sort order of my posts, I still fails at 84.

PHPVersion 5.2.1
PHPExcel 1.7.9
I'm just using setCellValueByColumnAndRow to fill in the data, no calculations, style or something.


I have no more Idea where to search for the error. I guess it's a lack of basic understanding how phpExcel works?

Thanks for any advice!
best regards from germany