Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
773 views
in Technique[技术] by (71.8m points)

php - PHPExcel - error while trying to insert image after loading and writing

i saw this problem in so many posts. but none have been answered. first time when I insert image in an excel file, no problem is there.but if i load that excel file again and try to insert another image in another cell,following problem occurs:

Fatal error: Uncaught exception 'PHPExcel_Writer_Exception' with message 'File zip://C:xampphtdocsWellest.xlsx#xl/media/well1.bmp does not exist' in C:xampphtdocsWellClassesPHPExcelWriterExcel2007ContentTypes.php:242 Stack trace: #0 C:xampphtdocsWellClassesPHPExcelWriterExcel2007ContentTypes.php(181): PHPExcel_Writer_Excel2007_ContentTypes->_getImageMimeType('zip://C:xampp...') #1 C:xampphtdocsWellClassesPHPExcelWriterExcel2007.php(246): PHPExcel_Writer_Excel2007_ContentTypes->writeContentTypes(Object(PHPExcel), false) #2 C:xampphtdocsWellest.php(125): PHPExcel_Writer_Excel2007->save('test.xlsx') #3 {main} thrown in C:xampphtdocsWellClassesPHPExcelWriterExcel2007ContentTypes.php on line 242.

This is my code to draw:

`$objDrawing = new PHPExcel_Worksheet_Drawing();
$objDrawing->setName('image');
$objDrawing->setDescription('nnnnn');
$objDrawing->setPath('images/well.bmp');
$objDrawing->setCoordinates('I'.$s);
$objDrawing->setWorksheet($objPHPExcel->getActiveSheet());`

I searched lot and I saw some posts regarding it. but none of them have been answered.

Edit: My code:

require_once 'Classes/PHPExcel.php';
require_once 'Classes/PHPExcel/IOFactory.php';
$name='statistics.xlsx';
if(file_exists($name))
{
 $objPHPExcel = PHPExcel_IOFactory::load($name);
 $lastRow = $objPHPExcel->getActiveSheet()->getHighestRow();
 $j=$lastRow+10;
}
else
{
 $j=1;
 $objPHPExcel = new PHPExcel();
 }
 $i='A';

 $objPHPExcel->setActiveSheetIndex(0);

// I am not writing the entire data

$objPHPExcel->getActiveSheet()->setCellValue($i++.$j, 'name');

$objDrawing = new PHPExcel_Worksheet_Drawing();
$objDrawing->setName('image');
$objDrawing->setDescription('image');
$objDrawing->setPath('image.png');
$objDrawing->setCoordinates('E'.$s);
$objDrawing->setWorksheet($objPHPExcel->getActiveSheet());

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save($name);
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I think this is a bug in PHPExcel in ClassesPHPExcelWriterExcel2007.php. Its easy to fix.

Short answer is: comment out or remove lines 235-237. That would be this code:

        if (file_exists($pFilename)) {
            unlink($pFilename);
        }

Your code will work then. I've checked, it works for me now.

Now, some longer explanation. On lines 235-243 there is this code:

        if (file_exists($pFilename)) {
            unlink($pFilename);
        }
        // Try opening the ZIP file
        if ($objZip->open($pFilename, $zipOverWrite) !== true) {
            if ($objZip->open($pFilename, $zipCreate) !== true) {
                throw new PHPExcel_Writer_Exception("Could not open " . $pFilename . " for writing.");
            }
        }

Now, in this code, three things take place in order:

  1. If we're saving data to existing file, then this file is deleted (unlink)
  2. If we're saving data to existing file, overwrite this file
  3. If we're saving data to non-existing file, create it

As you see, operation from step 2 is never executed because in step 1 file is always deleted. And in this process all previously existing file attachments are lost. That's why later you get error File XXX does not exist - indeed, your earlier image files are not present in newly created file.

Obvious fix to this problem is to remove step 1. If file exists, it should be overwritten. I do not see any logical explanation for step 1. Maybe its a left over from some old code.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...