HTML表格能不能使用PHP直接转换成EXCEL表格文件

html-css06

HTML表格能不能使用PHP直接转换成EXCEL表格文件,第1张

有以下提示供参考:

1、你可以直接抓取html表格代码,然后用PHP输出,如以下示例:

function outputXlsHeader($data, $file_name = 'export')

{

    header('Content-Type: text/xls')

    header("Content-type:application/vnd.ms-excelcharset=utf-8")

    $str = mb_convert_encoding($file_name, 'gbk', 'utf-8')

    header('Content-Disposition: attachmentfilename="' . $str . '.xls"')

    header('Cache-Control:must-revalidate,post-check=0,pre-check=0')

    header('Expires:0')

    header('Pragma:public')

    $table_data = '<table border="1">'

    foreach ($data as $line) {

        $table_data .= '<tr>'

        foreach ($line as $key => &$item) {

            $item = mb_convert_encoding($item, 'gbk', 'utf-8')

            $table_data .= '<td>' . $item . '</td>'

        }

        $table_data .= '</tr>'

    }

    $table_data .= '</table>'

    echo $table_data

    die()

}

2、先抓取 Html 表格,再用 PhpExcel 类库进行 Excel 表格操作。

$result = mysql_query("SELECT * FROM table_name")

while($row = mysql_fetch_array($result))

{

echo $row['ID'] . " " . $row['xuehao'] . " " . $row['xingming'] . " " . $row['chengji1'] . " " . $row['chengji2']

echo "<br />"

}

<table>

<tr>

<td>ID</td>

<td>标题</td>

</tr>

<?php

$query=mysql_query('select.....')

while($row = mysql_fetch_array($query)){

?>

<tr>

<td><?php echo $row['id']?></td>

<td><?php echo $row['title']?></td>

</tr>

<?php

}

?>

</table>

原生的就这样循环出来

多个字段就加多几个td就行

最好做个处理,把查找出来的结果组编成新数组,使用foreach循环比较好