使用PHP创建包含多个工作表的excel文件

时间:2016-03-04 13:43:36

标签: php mysql excel

我有2个或更多文件生成表格:

档案:my_table_0.php

<table border="1">
    <tr>
        <th>Previews</th>
        <th>Sizes</th>
        <th>ShapeName</th>
    </tr>
    <?php
    //connection to mysql
    mysql_connect("localhost", "login", "pass"); //server , username , password
    mysql_select_db("db");

    //query get data
    $sql = mysql_query("SELECT * FROM Table0 WHERE ShapeName='value' ORDER BY Sizes ASC ");
    $no = 1;
    while($data = mysql_fetch_assoc($sql)){
        echo '
        <tr>
            <td>'.$data['Previews'].'</td>
            <td>'.$data['Sizes'].'</td>
            <td>'.$data['ShapeName'].'</td>
        </tr>
        ';
        $no++;
    }
    ?>
</table>

文件:my_table_1.php

<table border="1">
    <tr>
        <th>Previews</th>
        <th>Sizes</th>
        <th>ShapeName</th>
    </tr>
    <?php
    //connection to mysql
    mysql_connect("localhost", "login", "pass"); //server , username , password
    mysql_select_db("db");

    //query get data
    $sql = mysql_query("SELECT * FROM Table1 WHERE ShapeName='value' ORDER BY Sizes ASC ");
    $no = 1;
    while($data = mysql_fetch_assoc($sql)){
        echo '
        <tr>
            <td>'.$data['Previews'].'</td>
            <td>'.$data['Sizes'].'</td>
            <td>'.$data['ShapeName'].'</td>
        </tr>
        ';
        $no++;
    }
    ?>
</table>

我制作xls的文件是这样的:

文件:make_xls_with_sheets.php

<?php
// The function header by sending raw excel
header("Content-type: application/vnd-ms-excel");

// Defines the name of the export file "filename.xls"
header("Content-Disposition: attachment; filename=all_tables.xls");

// Add data table to first worksheet
include 'my_table_0.php';

// Add data table to next/another worksheet
include 'my_table_0.php';
?>

此文件在同一工作表中插入两个表...我喜欢它 为每个表在相同的xls文件中创建一个新的工作表,就是这样 可能的?

0 个答案:

没有答案