objWriter->save('/home/test1/test.xls') Fails on Linux

Feb 16, 2009 at 7:11 AM


I execute the code below, but objWriter->save fails on the Linux giving the following error


Can't Open /home/test1/test.xls. It may be in use or protected.

stack trace

1. at ()

in SF_ROOT_DIR/plugins/sfPhpExcelPlugin-1.0.1/lib/PHPExcel/Shared/OLE/OLE_Root.php
line 103

100.   } else {
101.     $this->_FILEH = @fopen($filename,"wb");
102.     if($this->_FILEH == false)
103.    throw new Exception("Can't open $filename. It may be in use or protected.")
104.    }
105.  }


echo date('H:i:s') . " Create new PHPExcel object\n";
$objPHPExcel = new sfPhpExcel();

// Set properties
echo date('H:i:s') . " Set properties\n";
$objPHPExcel->getProperties()->setCreator("Maarten Balliauw");
$objPHPExcel->getProperties()->setLastModifiedBy("Maarten Balliauw");
$objPHPExcel->getProperties()->setTitle("Office 2007 XLSX Test Document");
$objPHPExcel->getProperties()->setSubject("Office 2007 XLSX Test Document");
$objPHPExcel->getProperties()->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.");
$objPHPExcel->getProperties()->setKeywords("office 2007 openxml php");
$objPHPExcel->getProperties()->setCategory("Test result file");

// Set default font
echo date('H:i:s') . " Set default font\n";

// Add some data, resembling some different data types
echo date('H:i:s') . " Add some data\n";
$objPHPExcel->getActiveSheet()->setCellValue('A1', 'String');
$objPHPExcel->getActiveSheet()->setCellValue('A2', 12);
$objPHPExcel->getActiveSheet()->setCellValue('A3', true);
$objPHPExcel->getActiveSheet()->setCellValue('A4', false);

// Rename sheet
echo date('H:i:s') . " Rename sheet\n";

// Set active sheet index to the first sheet, so Excel opens this as the first sheet

// Save Excel 2007 file
echo date('H:i:s') . " Write to Excel2007 format\n";
$objWriter = new PHPExcel_Writer_Excel5($objPHPExcel);

Feb 16, 2009 at 10:06 AM
This looks like a permission problem. If there is already a file named test.xls in that directory, try to delete it before running your script.

The problem could also be that the directory is not writable. Try for example this command:
chmod 777 /home/test1
Feb 17, 2009 at 9:27 AM
Dear Mr. Koyama,

Thanks for your kind help. It works now.


Deepak Bhatia