Objectclass cannot be converted to string error

im trying to import my database values ​​in my index.xlsx sheet so i wrote some code that uses phpExcel to import it

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);



?>
      

Run code


And when I run the index.php program, I get the error

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
      

Run code


Can someone help me with this thanks!

+3


source to share


2 answers


As said in the error message, on line 10

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

      



it should be

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

      

+1


source


You also don't need to specify the reader. It will automatically download according to your file type

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

      



To save the file

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');

      

+1


source







All Articles