PHPExcel - error while trying to insert image after loading and writing(PHPExcel - 加载和写入后尝试插入图像时出错)
问题描述
我在很多帖子中都看到了这个问题.但没有人回答.第一次在 excel 文件中插入图像时,没有问题.但是如果我再次加载该 excel 文件并尝试在另一个单元格中插入另一个图像,则会出现以下问题:
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:xampphtdocsWell est.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:xampphtdocsWell est.php(125): PHPExcel_Writer_Excel2007->save('test.xlsx') #3 {main} thrown in C:xampphtdocsWellClassesPHPExcelWriterExcel2007ContentTypes.php on line 242.
这是我要绘制的代码:
`$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.
我的代码:
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);
推荐答案
我认为这是 ClassesPHPExcelWriterExcel2007.php
中 PHPExcel 的一个错误.它很容易修复.
I think this is a bug in PHPExcel in ClassesPHPExcelWriterExcel2007.php
. Its easy to fix.
简短的回答是:注释掉或删除第 235-237 行.那就是这段代码:
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.
现在,一些更长的解释.在第 235-243 行有以下代码:
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:
- 如果我们将数据保存到现有文件,则该文件将被删除 (
unlink
) - 如果我们将数据保存到现有文件,请覆盖此文件
- 如果我们要将数据保存到不存在的文件中,请创建它
如您所见,第 2 步的操作永远不会执行,因为第 1 步中的文件总是被删除.在此过程中,所有先前存在的文件附件都将丢失.这就是为什么稍后您会收到错误 File XXX 不存在
- 实际上,您之前的图像文件不存在于新创建的文件中.
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.
解决此问题的明显方法是删除第 1 步.如果文件存在,则应将其覆盖.对于第 1 步,我没有看到任何合乎逻辑的解释.可能是一些旧代码遗留下来的.
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 - 加载和写入后尝试插入图像时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:PHPExcel - 加载和写入后尝试插入图像时出错
基础教程推荐
- 在 PHP 中强制下载文件 - 在 Joomla 框架内 2022-01-01
- 在多维数组中查找最大值 2021-01-01
- 超薄框架REST服务两次获得输出 2022-01-01
- XAMPP 服务器不加载 CSS 文件 2022-01-01
- 通过 PHP SoapClient 请求发送原始 XML 2021-01-01
- mysqli_insert_id 是否有可能在高流量应用程序中返回 2021-01-01
- Libpuzzle 索引数百万张图片? 2022-01-01
- WooCommerce 中选定产品类别的自定义产品价格后缀 2021-01-01
- 如何在 PHP 中的请求之间持久化对象 2022-01-01
- 在 Woocommerce 中根据运输方式和付款方式添加费用 2021-01-01