无法在Excel文件中显示图像

时间:2017-01-03 06:50:25

标签: php mysqli phpexcel

我能够从mysql数据库中导出excel中的所有数据。问题是,目前只打印存储在数据库中的图像名称。但是想要显示存储在图像文件夹中的图像。请帮帮我......

提前致谢..

<?php

  header("Content-Type:application/vnd.ms-excel");
  header("Content-Disposition:attachment;filename=product.xls");
  header("Pragma:no-cache");
  header("Expires:0");

  header('Content-Type: text/html; charset=UTF-8');
  header("Content-type: application/octetstream");
  header('Content-Disposition: attachment; filename="export.csv"');
 /** Error reporting */
  error_reporting(E_ALL);
  ini_set('display_errors', TRUE);
  ini_set('display_startup_errors', TRUE);
  date_default_timezone_set('Europe/London');

  if (PHP_SAPI == 'cli')
  die('This example should only be run from a Web Browser');

  /** Include PHPExcel */
  require_once 'classes/PHPExcel.php';
  Include ('config.php');
  $objPHPExcel = new PHPExcel();

  $sql = 'SELECT * from exceloutput';
  $res = mysqli_query($conn, $sql);
  if (!$res) {
    die(mysqli_error($conn));
  }
  error_reporting(E_ALL);

  date_default_timezone_set('Europe/London');
  $objPHPExcel = new PHPExcel();
  $col = 0; 
  $row = 1; 
  while($mrow = mysqli_fetch_assoc($res)) { 
    $col = 0; 
    foreach($mrow as $key=>$value) { 
        // TODO: Do Something With the Column Name like set the row header. Note this crude code sets it every time. You really just want to do it the first time only.
        $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, 0, $key);
        $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row + 1, $value); 
        $col++; 
    } 
    $row++; 
  }
  $objPHPExcel->setActiveSheetIndex(0);

  // Save Excel 2007 file
  $objPHPExcel->setActiveSheetIndex(0);


  header('Content-Type: application/vnd.ms-excel');
  header('Content-Disposition: attachment;filename="report.xls"');
  header('Cache-Control: max-age=0');
  // If you're serving to IE 9, then the following may be needed
  header('Cache-Control: max-age=1');
  // If you're serving to IE over SSL, then the following may be needed
  header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
  header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); // always modified
  header ('Cache-Control: cache, must-revalidate'); // HTTP/1.1
  header ('Pragma: public'); // HTTP/1.0

  $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
    $objWriter->save('php://output');


  exit;

1 个答案:

答案 0 :(得分:0)

查看05featuredemo.inc.phpPHPExcel documentation等示例,了解如何将图片添加到工作表中。

$objDrawing = new PHPExcel_Worksheet_Drawing();
$objDrawing->setName('PHPExcel logo');
$objDrawing->setDescription('PHPExcel logo');
$objDrawing->setPath('./images/phpexcel_logo.gif');
$objDrawing->setHeight(36);
$objDrawing->setCoordinates('D24');
$objDrawing->setOffsetX(10);
$objDrawing->setWorksheet($objPHPExcel->getActiveSheet());

在向StackOverflow询问之前,您应该先阅读文档。