PHPExcel - 尝试加载和写作之后插入图像错误 [英] PHPExcel - error while trying to insert image after loading and writing

查看:861
本文介绍了PHPExcel - 尝试加载和写作之后插入图像错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到了那么多帖子这个问题。但都没有得到答复。
当我在一个excel文件中插入图像第一次,没有问题是there.but如果我加载一个Excel文件,并尝试在另一个单元格中插入其他图像时,会出现以下问题:

 致命错误:未捕获的异常'PHPExcel_Writer_Exception有消息文件的zip:// C:\\ XAMPP \\ htdocs中\\好\\ test.xlsx#XL /媒体/ well1.bmp不存在'在C:\\ XAMPP \\ htdocs中\\好\\类\\ PHPExcel \\作家\\ Excel2007中\\ ContentTypes.php:242堆栈跟踪:#0 C:\\ XAMPP \\ htdocs中\\好\\类\\ PHPExcel \\作家\\的Excel2007 \\ ContentTypes.php (181):PHPExcel_Writer_Excel2007_ContentTypes-> _getImageMimeType('ZIP:// C:\\ XAMPP \\ ...')#1 C:\\ XAMPP \\ htdocs中\\好\\类\\ PHPExcel \\作家\\ Excel2007.php(246):PHPExcel_Writer_Excel2007_ContentTypes - > writeContentTypes(对象(PHPExcel),FALSE)#2 C:\\ XAMPP \\ htdocs中\\好\\ test.php的(125):PHPExcel_Writer_Excel2007->保存('test.xlsx')#{3}主力抛出用C :\\ XAMPP \\ htdocs中\\好\\类\\ PHPExcel \\作家\\ Excel2007中\\ ContentTypes.php上线242。

这是我的code得出:

 `$ objDrawing =新PHPExcel_Worksheet_Drawing();
$ objDrawing->的setName('图像');
$ objDrawing-> setDescription('NNNNN');
$ objDrawing->的setpath(图像/ well.bmp');
$ objDrawing-> setCoordinates('I'$ S。);
$ objDrawing-> setWorksheet($ objPHPExcel-> getActiveSheet());`

我搜索很多,我看到关于它的一些帖子。但他们都没有得到回答。

编辑:
我的code:

  require_once'类/ PHPExcel.php';
require_once'类/ PHPExcel / IOFactory.php';
$ NAME ='statistics.xlsx';
如果(file_exists($名))
{
 $ objPHPExcel = PHPExcel_IOFactory ::负载($名);
 $ LASTROW = $ objPHPExcel-> getActiveSheet() - > getHighestRow();
 $ J = $ LASTROW + 10;
}
其他
{
 $ J = 1;
 $ objPHPExcel =新PHPExcel();
 }
 $ I ='A'; $ objPHPExcel-> setActiveSheetIndex(0);//我不写整个数据$ objPHPExcel-> getActiveSheet() - GT; setCellValue($ I $ ++Ĵ,'名');$ objDrawing =新PHPExcel_Worksheet_Drawing();
$ objDrawing->的setName('图像');
$ objDrawing-> setDescription('图像');
$ objDrawing->的setpath('im​​age.png');
$ objDrawing-> setCoordinates('E'$ S。);
$ objDrawing-> setWorksheet($ objPHPExcel-> getActiveSheet());$ objWriter = PHPExcel_IOFactory :: createWriter($ objPHPExcel,Excel2007中');
$ objWriter->保存($名);


解决方案

我觉得这是在PHPExcel 类的错误\\ PHPExcel \\作家\\ Excel2007.php 。它很容易修复。

简短的回答是:注释掉或删除线235-237。这将是这code:

 如果(file_exists($ pFilename)){
            取消链接($ pFilename);
        }

您code将工作即可。我检查了,现在我的作品。

现在,一些较长的解释在235-243行有这样的code:

 如果(file_exists($ pFilename)){
            取消链接($ pFilename);
        }
        //尝试打开ZIP文件
        如果($ objZip->开($ pFilename,$ zipOverWrite)==真!){
            如果($ objZip->开($ pFilename,$ zipCreate)==真!){
                抛出新PHPExcel_Writer_Exception(无法打开$ pFilename写作。);
            }
        }

现在,在这个code,三件事情发生在顺序为:


  1. 如果我们将数据保存到已存在的文件,那么该文件将被删除(取消链接

  2. 如果我们将数据保存到已存在的文件,覆盖此文件

  3. 如果我们将数据保存到不存在的文件,创建

正如你所见,从第2步操作决不会因为在步骤1文件中执行是的总是的删除。而在这个过程中的所有previously现有的文件附件都将丢失。这就是为什么后来你会得到错误文件XXX不存在 - 事实上,你刚才的图像文件不在新创建的文件present

明显修复这个问题是消除步骤1。如果文件存在,它应该被覆盖。我没有看到步骤1中的任何合理的解释也许它距离一些老code遗留下来的。

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:\xampp\htdocs\Well\test.xlsx#xl/media/well1.bmp does not exist' in C:\xampp\htdocs\Well\Classes\PHPExcel\Writer\Excel2007\ContentTypes.php:242 Stack trace: #0 C:\xampp\htdocs\Well\Classes\PHPExcel\Writer\Excel2007\ContentTypes.php(181): PHPExcel_Writer_Excel2007_ContentTypes->_getImageMimeType('zip://C:\xampp\...') #1 C:\xampp\htdocs\Well\Classes\PHPExcel\Writer\Excel2007.php(246): PHPExcel_Writer_Excel2007_ContentTypes->writeContentTypes(Object(PHPExcel), false) #2 C:\xampp\htdocs\Well\test.php(125): PHPExcel_Writer_Excel2007->save('test.xlsx') #3 {main} thrown in C:\xampp\htdocs\Well\Classes\PHPExcel\Writer\Excel2007\ContentTypes.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);

解决方案

I think this is a bug in PHPExcel in Classes\PHPExcel\Writer\Excel2007.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.

这篇关于PHPExcel - 尝试加载和写作之后插入图像错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆