1

i try to create new excel file using phpexcel writer

<?php

     set_include_path(get_include_path() . PATH_SEPARATOR . 'Classes/');
     include 'Classes/PHPExcel/IOFactory.php';
     include 'Classes/PHPExcel/Writer/Excel2007.php';     
     $inputFileName ='mak.xlsx';

    try 
    {
         $inputFileType = PHPExcel_IOFactory::identify($inputFileName);
         $objReader = PHPExcel_IOFactory::createReader($inputFileType);
         $objPHPExcel = $objReader -> load($inputFileName);
         $objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
         $objWriter->save('MyExcel.xlsx');            
    }
    catch(Exception $e) 
    {
        die('Error loading file "'.pathinfo($inputFileName,PATHINFO_BASENAME).'": '.$e->getMessage());
    }

?>

i first load exiting excel file then pass the file data into PHPExcel_Writer_Excel2007($objPHPExcel). new file is create as a blank file

Niranjan N Raju
  • 12,047
  • 4
  • 22
  • 41
Maksud Mansuri
  • 631
  • 13
  • 26
  • If the writer saves the file but empty, than the problem must be that the $objPHPExcel is empty. I suggest using a debugger like Kint to inspect that object. Or quickly: echo '
    '; var_dump( $objPHPExcel ); echo '
    '; (just below defining it)
    – Jos Oct 07 '15 at 12:50
  • I wouldn't recommend var_dumping a PHPExcel object, it's the top of a pretty complex hierarchy of objects; but as @JosFaber points out, if the file is being saved empty, it's because it is empty.... try using the worksheet's `toArray()` method to read the content of your loaded file – Mark Baker Oct 07 '15 at 12:58

1 Answers1

2

Try this:

$sheet = $objPHPExcel->setActiveSheetIndex(0);
$sheet->setCellValue('A1','TEST');

$objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);

In your example $objPHPExcel is empty so that's why you get empty document

  • `$sheet->setCellValue('A1', 'TEST');` - the cell address should be the first argument, and should be a string – Mark Baker Oct 07 '15 at 18:21