如何在 PHPExcel 中设置流内容? [关闭]
Posted
技术标签:
【中文标题】如何在 PHPExcel 中设置流内容? [关闭]【英文标题】:How to set the stream content in PHPExcel? [closed] 【发布时间】:2015-06-25 06:24:59 【问题描述】:我正在使用 php 和 PHPExcel 从数据库中获取图像文件名并执行流读取器操作。我使用setImageResource
函数设置了我的图像,但它没有按预期工作。这是我的代码:
<?php
require_once dirname(__FILE__) . '/../Classes/PHPExcel.php';
require_once '../classes/PHPExcel/IOFactory.php';
error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);
date_default_timezone_set('Asia/Calcutta');
if (PHP_SAPI == 'cli')
die('This example should only be run from a Web Browser');
$rendererName = PHPExcel_Settings::PDF_RENDERER_DOMPDF;
$rendererLibrary = 'dompdf.php';
$rendererLibraryPath = dirname(__FILE__) . '/../libs/classes/dompdf/';
// Create new PHPExcel object
$objPHPExcel = new PHPExcel();
$objPHPExcel->setActiveSheetIndex(0);
$objPHPExcel->getActiveSheet()->SetCellValue('A1', 'Tes');
$objPHPExcel->getActiveSheet()->SetCellValue('B1', 'Image...');
$objPHPExcel->getActiveSheet()->setTitle('Simple');
$img = imagecreatefrompng('C:/Users/Downloads/logo.png');
$objDrawing = new PHPExcel_Worksheet_MemoryDrawing();
$objDrawing->setName('Test image');
$objDrawing->setDescription('Test image');
$objDrawing->setImageResource($img);
$objDrawing->setRenderingFunction(PHPExcel_Worksheet_MemoryDrawing::RENDERING_PNG);
$objDrawing->setMimeType(PHPExcel_Worksheet_MemoryDrawing::MIMETYPE_DEFAULT);
$objDrawing->setHeight(150);
$objDrawing->setCoordinates('A4');
$objDrawing->setWorksheet($objPHPExcel->getActiveSheet());
if (!PHPExcel_Settings::setPdfRenderer($rendererName,$rendererLibraryPath))
die(
'NOTICE: Please set the $rendererName and $rendererLibraryPath values' .
'<br />' .
'at the top of this script as appropriate for your directory structure'
);
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'PDF');
$objWriter->save('test.pdf');
exit;
【问题讨论】:
你能提供一些代码吗,到目前为止你已经尝试过了? $file = fopen('logo.png', 'r'); $img = stream_get_contents($file); header("内容类型:图片/png"); $objDrawing = new PHPExcel_Worksheet_MemoryDrawing(); $objDrawing->setName('logo'); $objDrawing->setCoordinates('A1'); $objDrawing->setImageResource($img); $objDrawing->setRenderingFunction(PHPExcel_Worksheet_MemoryDrawing::RENDERING_PNG); $objDrawing->setMimeType(PHPExcel_Worksheet_MemoryDrawing::MIMETYPE_DEFAULT);$objDrawing->setHeight(36); $objDrawing->setWorksheet($objPHPExcel->getActiveSheet()); 请提供给您的问题。 :) 这段代码只有我习惯于尝试设置流内容。 您可以尝试在fopen
中使用$data = file_get_contents("logo.png");
代替$file and $img
来获取图片内容。
【参考方案1】:
好吧,首先PHPExcel_Worksheet_MemoryDrawing()
无法解决您的问题,如果您坚持使用流内容并将其传递到您的工作表,您的 PDF 将不会呈现您的图像。但是如果你也想渲染你的图像,你可以使用`PHPExcel_Worksheet_Drawing()'。你可以这样欺骗它:
-
您可以从
stream_get_contents
或file_get_contents
检索您的图像文件
然后将其重新创建到文件中。
最后你将图像文件路径传递给$objDrawing->setPath($path_to_your_image);
因此,如果您也想将图像渲染到工作表中,则必须使用 PHPExcel_Worksheet_Drawing()
,为此您必须指向文件路径才能渲染该图像。
我认为这是与 DomPDF
一起工作的 PHPExcel
的问题。因此,请在此处保留一些讨论并将您的 objDrawing
编辑为:
$objDrawing = new PHPExcel_Worksheet_Drawing();
$objDrawing->setPath('logo.png');
对于我制作的完整示例,试试这个:
include 'PHPExcel.php';
require_once("dompdf/dompdf_config.inc.php");
$rendererName = PHPExcel_Settings::PDF_RENDERER_DOMPDF;
$rendererLibrary = 'dompdf.php';
$rendererLibraryPath = dirname(__FILE__). '/dompdf';
$objPHPExcel = new PHPExcel();
$objPHPExcel->setActiveSheetIndex(0);
$objPHPExcel->getActiveSheet()->SetCellValue('A1', 'Tes');
$objPHPExcel->getActiveSheet()->SetCellValue('B1', 'Image...');
$objPHPExcel->getActiveSheet()->setTitle('Simple');
$objPHPExcel->getActiveSheet()
->getColumnDimension('A')
->setWidth(50);
$objPHPExcel->getActiveSheet()
->getRowDimension('2')
->setRowHeight(200);
// $img = imagecreatefrompng('logo.png');
$data = file_get_contents("logo.png");
// $img = imagecreatefromstring($data);
$fp = fopen("logo1.png","w");
fwrite($fp,$data);
fclose($fp);
$objDrawing = new PHPExcel_Worksheet_Drawing();
$objDrawing->setPath('logo1.png');
// $objDrawing = new PHPExcel_Worksheet_MemoryDrawing();
$objDrawing->setName('Test image');
$objDrawing->setDescription('Test image');
// $objDrawing->setImageResource($img);
// $objDrawing->setRenderingFunction(PHPExcel_Worksheet_MemoryDrawing::RENDERING_PNG);
// $objDrawing->setRenderingFunction(PHPExcel_Worksheet_MemoryDrawing::RENDERING_DEFAULT);
// $objDrawing->setMimeType(PHPExcel_Worksheet_MemoryDrawing::MIMETYPE_DEFAULT);
$objDrawing->setHeight(150);
$objDrawing->setCoordinates('A2');
$objDrawing->setWorksheet($objPHPExcel->getActiveSheet());
if (!PHPExcel_Settings::setPdfRenderer($rendererName,$rendererLibraryPath))
die(
'NOTICE: Please set the $rendererName and $rendererLibraryPath values' .
'<br />' .
'at the top of this script as appropriate for your directory structure'
);
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save(str_replace('.php', '.xlsx', __FILE__));
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'PDF');
$objWriter->save('test.pdf');
echo date('H:i:s') . " Done writing file.\r\n";
很高兴为您提供帮助,希望对您有所帮助:)
【讨论】:
评论不用于扩展讨论;这个对话是moved to chat。以上是关于如何在 PHPExcel 中设置流内容? [关闭]的主要内容,如果未能解决你的问题,请参考以下文章