Home > Mobile >  new excel spreadsheet library for excel generate demo for PHP codigniter
new excel spreadsheet library for excel generate demo for PHP codigniter

Time:02-25

i am using https://github.com/PHPOffice/PhpSpreadsheet this library. If we include this library we getting error related to class. We facing issue of spreadsheet class not found.

how to generate excel using spreadsheet library. if any example it is help full for me.

CodePudding user response:

Just go through https://phpspreadsheet.readthedocs.io/en/latest/#installation once for installation.Earlier i too faced the issue but i can use it on both localhost and liveserver. Here is the sample code...

<?php 
        $host = 'hostaddress';
        $user = 'root';
        $password = '';
        $database = 'db';

        $conn = mysqli_connect($host,$user,$password,$database);
        if(mysqli_connect_errno()){
            echo "Failed to connect to database" . mysqli_connect_errno();
        }

        $query1 = mysqli_query($conn, "SELECT * FROM restaurent_feedback ");
        $output = '';
        $i = 1;
        if($query1->num_rows > 0){
            $output = '<table border=1>
                        <tr>
                          <th>Slno</th>
                          <th>Restaurent_Name </th>
                          <th>Estimation_Name</th>
                          <th>Comment</th>
                          <th>Commented_By</th>
                          <th>Commented_on</th>
                        </tr>
            ';
        while($row = mysqli_fetch_array($query1)){
            $output .= '<tr>
                            <td>'.$i.'</td>
                            <td>'.$row['rest_name'].'</td>
                            <td>'.$row['est_name'].'</td>
                            <td>'.$row['comment'].'</td>
                            <td>'.$row['created_by'].'</td>
                            <td>'.$row['created_on'].'</td>
                </tr>';
                $i  ;
        }
        $output .= "</table>";
    }
    require "vendor/autoload.php";
    use PhpOffice\PhpSpreadsheet\Spreadsheet;
    use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
    $reader = new \PhpOffice\PhpSpreadsheet\Reader\Html();
    $spreadsheet = $reader->loadFromString($output);
    header("Content-Type: application/vnd.md-excel");
    header("Content-Disposition: attachment;filename=\"exporttoexcel.xls\"");
    $writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Xls');
    $writer->save("php://output"); 
    ?>

here is the output which i got

CodePudding user response:

i am trying PHP Version 7.4.28 generate excel file using excel libary but local setup properly work but live server not working and not any error show

  • Related