将数据从HTML表下载到excel [英] Download Data from HTML table to excel

查看:103
本文介绍了将数据从HTML表下载到excel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



如下所示:



我有一个表,用PHP构造,显示一个mysql表的结果。 pre> $ sql = mysql_query(SELECT * FROM survey);

echo< table border ='1'>
< tr>
< th>问题1< / th>
&问题2< / th>
< th>问题3< / th>
< th>问题4< / th>
< th>问题5& $ b< th>问题6< / th>
< th>问题7< / th>
< th>问题8< ; / th>
< th>问题10< / th>
< th>问题11< / th>
< th>问题12< / th>
问题13< th>
< th>问题14< / th>
< th> eMail< / th>
< / tr>

while($ row = mysql_fetch_array($ sql))
{
echo< tr>;
echo< td> 。 $ row ['Question1']。 < / TD> 中;
echo< td> 。 $ row ['Question2']。 < / TD> 中;
echo< td> 。 $ row ['Question3']。 < / TD> 中;
echo< td> 。 $ row ['Question4']。 < / TD> 中;
echo< td> 。 $ row ['Question5']。 < / TD> 中;
echo< td> 。 $ row ['Question6']。 < / TD> 中;
echo< td> 。 $ row ['Question7']。 < / TD> 中;
echo< td> 。 $ row ['Question8']。 < / TD> 中;
echo< td> 。 $ row ['Question9']。 < / TD> 中;
echo< td> 。 $ row ['Question10']。 < / TD> 中;
echo< td> 。 $ row ['Question11']。 < / TD> 中;
echo< td> 。 $ row ['Question12']。 < / TD> 中;
echo< td> 。 $ row ['Question13']。 < / TD> 中;
echo< td> 。 $ row ['Question14']。 < / TD> 中;
echo< td> 。 $ row ['eMail']。 < / TD> 中;
}
echo< / table>;

我想做的是将此表格输出到Excel文件,没有任何源代码或表格标签。有办法我可以做到吗?我被互联网上的信息淹没了,不太确定我需要什么。



如果可能,我更愿意这样做只需要PHP,没有任何额外的库。

您是否要使用PHP脚本下载Excel文件(即,PHP输出.xls文件),还是要生成可以复制/粘贴到Excel中的HTML输出?文件不会丢失格式?



如果是前者,请查看这个漂亮工具



如果您只想生成可以复制/粘贴到Excel的HTML,只要表格格式正确(并从您的来源代码,看起来您需要在while循环的末尾添加< / tr>),那么您应该能够拖放它



或者,Excel 应该足够聪明地识别HTML表,只要没有< html>标签封装。 这篇文章解释了这个过程,但实质上你可以发送一个Excel文件(按照第一个漂亮的工具建议)只是:

 标题(Content-type:application / vnd.ms-excel) ; 
echo
< table>
...
< / table>;


I have a table, constructed in PHP that displays results from a mysql table.

Like so:

$sql = mysql_query("SELECT * FROM survey");

            echo "<table border='1'>
            <tr>
            <th>Question 1</th>
            <th>Question 2</th>
            <th>Question 3</th>
            <th>Question 4</th>
            <th>Question 5</th>
            <th>Question 6</th>
            <th>Question 7</th>
            <th>Question 8</th>
            <th>Question 9</th>
            <th>Question 10</th>
            <th>Question 11</th>
            <th>Question 12</th>
            <th>Question 13</th>
            <th>Question 14</th>
            <th>eMail</th>
            </tr>";

            while ($row = mysql_fetch_array($sql))
            {
                echo "<tr>";
                echo "<td>" . $row['Question1'] . "</td>";
                echo "<td>" . $row['Question2'] . "</td>";
                echo "<td>" . $row['Question3'] . "</td>";
                echo "<td>" . $row['Question4'] . "</td>";
                echo "<td>" . $row['Question5'] . "</td>";
                echo "<td>" . $row['Question6'] . "</td>";
                echo "<td>" . $row['Question7'] . "</td>";
                echo "<td>" . $row['Question8'] . "</td>";
                echo "<td>" . $row['Question9'] . "</td>";
                echo "<td>" . $row['Question10'] . "</td>";
                echo "<td>" . $row['Question11'] . "</td>";
                echo "<td>" . $row['Question12'] . "</td>";
                echo "<td>" . $row['Question13'] . "</td>";
                echo "<td>" . $row['Question14'] . "</td>";
                echo "<td>" . $row['eMail'] . "</td>";              
            }
            echo "</table>";

What I want to do is output this table into an excel file, without any source code, or table tags. Is there a way I can do this? I've been overwhelmed with information on the internet and not quite sure what I need.

If possible I'd prefer to do this with just PHP, and without any additional libraries.

解决方案

Do you want to download an Excel file using your PHP script (ie- the PHP outputs a .xls file) or do you want it to produced HTML output that you can copy/paste into an Excel file without losing formatting?

If the former, look into this nifty tool

If you just want it to produce HTML that you can copy/paste into Excel, so long as your table is properly formatted (and from your source code, it looks like you need to add a </tr> at the end of your while loop) then you should just be able to drag and drop it

Alternatively, Excel should be smart enough to recognize HTML tables so long as there is no <html> tag encapsulating it. this article explains the process, but essentially you can send an Excel file (as per the first "nifty tool" suggestion) with just:

header("Content-type: application/vnd.ms-excel");
echo "
<table>
    ...
</table>";

这篇关于将数据从HTML表下载到excel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆