codeigniter 新Excel电子表格库为Excel生成演示PHP codigniter

4zcjmb1e  于 2022-12-07  发布在  PHP
关注(0)|答案(1)|浏览(135)

我正在使用https://github.com/PHPOffice/PhpSpreadsheet这个库。如果我们包含这个库,我们会得到与类相关的错误。我们面临找不到电子表格类的问题。
如何使用电子表格库生成excel。如果有任何例子,这对我很有帮助。

f0ofjuux

f0ofjuux1#

只要通过https://phpspreadsheet.readthedocs.io/en/latest/#installation一次安装。早些时候我也遇到过这个问题,但我可以在localhost和liveserver上使用它。下面是示例代码...

<?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

相关问题