「槍神Trigun」,您的建議確實幫助。我能夠從http://phpexcel.codeplex.com/releases下載最新的PHPExcel Classes,並立即開始運行。但是,花了一些額外的時間來弄清楚如何將圖像添加到excel文件。你的解釋沒有多大幫助。
這裏是如何做到這一點的完整描述:
首先,下載庫,並將其放置在一個合理的地方在您的網站:
sites/all/libraries/phpexcel/Classes
現在你創建你的PHP文件的任何地方在你的網站,你想它是和添加以下到文件:
1)允許在屏幕上打印錯誤消息:
error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);
date_default_timezone_set('Europe/London');
define('EOL',(PHP_SAPI == 'cli') ? PHP_EOL : '<br \>');
2)包含了Excel類文件:
/** Include PHPExcel */
require_once $_SERVER["DOCUMENT_ROOT"] . "/sites/all/libraries/phpexcel/Classes/PHPExcel.php";
3)創建 「PHPExcel」 對象:
// Create new PHPExcel object
echo date('H:i:s') , " Create new PHPExcel object" , EOL;
$objPHPExcel = new PHPExcel();
4)設置一些Excel的元數據,例如標題和說明。
// Set document properties
echo date('H:i:s') , " Set document properties" , EOL;
$objPHPExcel->getProperties()->setCreator("Maarten Balliauw")
->setLastModifiedBy("Maarten Balliauw")
->setTitle("PHPExcel Test Document")
->setSubject("PHPExcel Test Document")
->setDescription("Test document for PHPExcel, generated using PHP classes.")
->setKeywords("office PHPExcel php")
->setCategory("Test result file");
5)一些數據添加到 「B1」 細胞:
// Add some data
echo date('H:i:s') , " Add some data" , EOL;
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('B1', 'Hello world!')
6)創建一個 「繪圖」 對象,我們可以在我們的圖像加載到。記得替換使用有效的圖像URL圖片URL在Web服務器:
// Add a drawing to the worksheet
echo date('H:i:s') , " Add a drawing to the worksheet" , EOL;
$objDrawing = new PHPExcel_Worksheet_Drawing();
$objDrawing->setName('Thumb');
$objDrawing->setDescription('Thumbnail Image');
$objDrawing->setPath($_SERVER["DOCUMENT_ROOT"] . '/sites/default/files/product-images/10098.jpg');
$objDrawing->setHeight(21);
7)的圖像複製到我們的「$ objPHPExcel」對象的「A1」單元。
$objDrawing->setCoordinates('A1');
$objDrawing->setWorksheet($objPHPExcel->getActiveSheet());
8)將「$ objPHPExcel」對象保存爲Excel文件格式。
// Save Excel 95 file
echo date('H:i:s') , " Write to Excel5 format" , EOL;
$callStartTime = microtime(true);
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save(str_replace('.php', '.xls', __FILE__));
9)打印一些輕度有用的信息在屏幕上:
$callEndTime = microtime(true);
$callTime = $callEndTime - $callStartTime;
echo date('H:i:s') , " File written to " , str_replace('.php', '.xls', pathinfo(__FILE__, PATHINFO_BASENAME)) , EOL;
echo 'Call time to write Workbook was ' , sprintf('%.4f',$callTime) , " seconds" , EOL;
// Echo memory usage
echo date('H:i:s') , ' Current memory usage: ' , (memory_get_usage(true)/1024/1024) , " MB" , EOL;
// Echo memory peak usage
echo date('H:i:s') , " Peak memory usage: " , (memory_get_peak_usage(true)/1024/1024) , " MB" , EOL;
// Echo done
echo date('H:i:s') , " Done writing files" , EOL;
echo 'Files have been created in ' , getcwd() , EOL;
這就是整個事情!
加1回來回答你的問題。 –