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

时间:2015-06-17 03:56:10

标签: php phpexcel

我试图在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);



?>
&#13;
&#13;
&#13;

当我运行程序index.php时,我得到错误

&#13;
&#13;
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
&#13;
&#13;
&#13;

有人可以帮我这个谢谢你!

2 个答案:

答案 0 :(得分:1)

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

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

应该是

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

答案 1 :(得分:1)

您也不需要指定任何读者。它将由您的文件类型

自动获取
$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');