对象 Class 无法转换为字符串错误

Object Class could not be converted to string error

我正在尝试将我的数据库值导入到我的 index.xlsx 工作表中 所以我写了一个代码,使用 phpExcel 导入它

Index.php

<?php
require_once 'E:\Xampp\htdocs\Excel\Classes\phpExcel.php';
$con = mysql_connect('localhost','root','');
mysql_select_db('khader',$con);
$sql = "SELECT * FROM person";
$result = mysql_query($sql);
$filename = "index.xlsx";
$objReader = PHPExcel_IOFactory::createReader('Excel2007');
$objReader ->setReadDataOnly(true);
$objReader ->$objReader->load($filename);
$objWorksheet ->$objPHPExcel->getActiveSheet();
$objWorksheet->$objPHPExcel->setActiveSheetIndex(0);
echo " <form method='post'>
<input type='submit' value='Convert' name='submit'>
</form>";
if(isset($_POST['submit']))
{
    $col = 1;
    while($rows = mysql_fetch_row($result))
    {
     $row = 1 ;
        foreach($rows as $value )
        {
            $objPHPExcel ->getActivesheet()->setCellValueByColumnAndRow($col,$row,$value);
            $objPHPExcel ->getActivesheet()->getColumnDimension('A')->SetAutoSize(true);
            $objPHPExcel ->getActivesheet()->getColumnDimension('B')->SetAutoSize(true);
            $objPHPExcel ->getActivesheet()->getColumnDimension('C')->SetAutoSize(true);
            $objPHPExcel ->getActivesheet()->getColumnDimension('D')->SetAutoSize(true);
            $objPHPExcel ->getActivesheet()->getColumnDimension('E')->SetAutoSize(true);
            $row++;











        }
        $col++;









    }









echo "imported";
}
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save(index.xlsx);



?>

当我 运行 程序 index.php 我得到一个错误

Catchable fatal error: Object of class PHPExcel_Reader_Excel2007 could not be converted to string in E:\Xampp\htdocs\Example\index.php on line 10

谁能帮我解决这个问题谢谢!!

正如错误信息所说,在第 10 行

$objReader ->$objReader->load($filename);

应该是

$objPHPExcel=$objReader->load($filename);

您也不需要指定任何 reader。它将根据您的文件类型自动获取

$objPHPExcel  = PHPExcel_IOFactory::load($filename);
$objWorksheet = $objPHPExcel->getActiveSheet();

用于保存文件

header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="yourfile.xls"');
header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('php://output');