时间:2021-07-01 10:21:17 帮助过:3人阅读
你这只是片段,仅从这点是看不到问题的!
因为这个代码片段并没有错!
Uncaught exception 'Exception' with message 'A PHPExcel_Worksheet has already been assigned. Drawings can only exist on one PHPExcel_Worksheet.'
是说:一个 PHPExcel_Worksheet_Drawing 实例只能在一个单元格中存在
显然你把 $objDrawing 放到 A1 后,又放到了别处
/** 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 dirname(__FILE__) . '/Classes/PHPExcel.php';
$db = new mysqli('localhost','user.com','suMtOHQhPW','users');
mysqli_query($db, "set character set 'utf8'");
// Create new PHPExcel object
$objPHPExcel = new PHPExcel();
// Set document properties
$objPHPExcel->getProperties()->setCreator("fnsr")
->setLastModifiedBy("fnsr")
->setTitle("Office 2007 XLSX Document")
->setSubject("Office 2007 XLSX Document")
->setDescription("document for Office 2007 XLSX, generated using PHP classes.")
->setKeywords("office 2007 openxml php")
->setCategory("result file");
// Add some data
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A1', '微信号')
->setCellValue('B1', '名字')
->setCellValue('C1', '身高')
->setCellValue('D1', '三围')
->setCellValue('E1', '电话')
->setCellValue('F1', '简介')
->setCellValue('G1', '大头')
->setCellValue('H1', '半身')
->setCellValue('I1', '全身')
->setCellValue('J1', '推荐人')
->setCellValue('K1', '状态');
// Miscellaneous glyphs, UTF-8
$query = "SELECT * FROM enroll";
$result = $db->query($query);
$num_results = $result->num_rows;
$k=2;
$objDrawing = new PHPExcel_Worksheet_Drawing();
for($i=0;$i<$num_results;$i++){
$row = $result->fetch_assoc();
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue("A".$k, $row['nameid'])
->setCellValue("B".$k, $row['pname'])
->setCellValue("C".$k, $row['stature'])
->setCellValue("D".$k, $row['bust'])
->setCellValue("E".$k, $row['stel'])
->setCellValue("F".$k, $row['tips'])
//->setCellValue("G".$k, )
//->setCellValue("H".$k, '../upimg/'.$row['nameid'].'_2.jpg')
//->setCellValue("I".$k, '../upimg/'.$row['nameid'].'_3.jpg')
->setCellValue("J".$k, $row['refs'])
->setCellValue("K".$k, $row['state']);
//$pa=$_SERVER['DOCUMENT_ROOT'].'\\mweb\\tjnsr\\upimg\\';
//$pa='../upimg/';
if(file_exists($row['nameid'].'_1.jpg')){
$objPHPExcel->setActiveSheetIndex(0);
$objDrawing->setPath('D:\wwwroot\user\upimg\201460dZeETjg_1.jpg');
$objDrawing->setCoordinates("G1");
$objDrawing->setWorksheet($objPHPExcel->getActiveSheet());
}
/*if(file_exists($row['nameid'].'_2.jpg')){
$objDrawing->setPath($row['nameid'].'_2.jpg');
$objDrawing->setCoordinates("H".$k);
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue("H".$k, $row['nameid'].'_2.jpg');
//$objDrawing->setWorksheet($objPHPExcel->getActiveSheet());
}
if(file_exists($row['nameid'].'_3.jpg')){
$objDrawing->setPath($row['nameid'].'_3.jpg');
$objDrawing->setCoordinates("I".$k);
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue("I".$k, $row['nameid'].'_3.jpg');
//$objDrawing->setWorksheet($objPHPExcel->getActiveSheet());
}*/
$k++;
}
$result->free();
$db->close();
//$objPHPExcel->setActiveSheetIndex(0)
// ->setCellValue('A5', $pa);
// Rename worksheet
$objPHPExcel->getActiveSheet()->setTitle('联系表');
// Set active sheet index to the first sheet, so Excel opens this as the first sheet
$objPHPExcel->setActiveSheetIndex(0);
// Redirect output to a client’s web browser (Excel5)
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="报名人员表.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;
对呀!
你的 $objDrawing = new PHPExcel_Worksheet_Drawing(); 在循环外面
而 $objDrawing->setWorksheet($objPHPExcel->getActiveSheet()); 在循环里面
你如何能保证 $objDrawing = new PHPExcel_Worksheet_Drawing(); 只被这行一次呢?
虽然是套在条件分支中的,你有如何能保证条件只成立一次呢?
你只需把 $objDrawing = new PHPExcel_Worksheet_Drawing(); 放到循环里面,一切都解决了
多谢,问题找到,但是xls文档打不开
代码修改: if(file_exists($row['nameid'].'_1.jpg')){
$objDrawing[$k] = new PHPExcel_Worksheet_Drawing();
$objDrawing[$k]->setPath($row['nameid'].'_1.jpg');
$objDrawing[$k]->setCoordinates("G".$k);
$objDrawing[$k]->setWorksheet($objPHPExcel->getActiveSheet());
}