Scott Machlovski
Scott Machlovski

Reputation: 270

Can't Export to Excel using Php7 and Mysql

I need to import mysql datas to an excel file. I'm using php7. I wrote a code but the problem is all the data are printed in a single column in excel file. Could anyone help me with code to change the row and column in excel file. This is my code: `

$connect=  mysqli_connect("localhost", "root", "", "crmweb");
$output='';$output2='';

    $sql="SELECT name,hname,cno FROM hencus ";
    $result=  mysqli_query($connect, $sql);
    if(mysqli_num_rows($result)>0)
    {
        $output.=' 
                <table class="table" border="1>
                <tr>
                    <th>Name</th>
                    <th>House</th>
                    <th>Contact</th>
                    </tr>';
        while($row= mysqli_fetch_array($result))
        {
            $output.=' 

                <tr>
                    <td>'.$row['name'].'</td>
                    <td>'.$row['hname'].'</td>
                    <td>'.$row['cno'].'</td>
                    </tr>';

        }
        $output.='</table>';

        header("Content-Type: application/xls");
        header("Content-Disposition: attachment; filename=download.xls");
        echo $output;
       // echo $output2;


    }

    ?>      `      

Upvotes: 1

Views: 3070

Answers (1)

Nicolaesse
Nicolaesse

Reputation: 2714

Try using this:

header ("Content-Type: application/vnd.ms-excel");
header ("Content-Disposition: inline; filename=download.xls");

instead of

header("Content-Type: application/xls");
header("Content-Disposition: attachment; filename=download.xls");

Here you can find a good tutorial.

Upvotes: 1

Related Questions