从MySQL数据库填充Excel表单字段

时间:2012-09-28 08:41:46

标签: php mysql excel file

我有一个现有的Form,我需要直接从MySQL数据库填充这些字段。

我的老板希望我们客户的个人资料税计算能够自动输入Excel表格。

有关于此的任何建议吗?

提前致谢...

BTW,使用PHP

3 个答案:

答案 0 :(得分:1)

第二次编辑:

<?php

    function connect_db($db_name) {

        $con = mysql_connect('localhost', 'db_user', 'db_pass');
        $seldb = mysql_select_db($db_name);

        if($con && $seldb) {

            return true;

        }
        else {

            return false;

        }

    }

    function get_data_from_mysql() {

        $data_query = "SELECT date, new_users, old_users, income FROM stats ORDER BY date DESC LIMIT 30";

        $data_mysql = mysql_query($data_query);

        $data_finished = array();
        while($data_fetch = mysql_fetch_assoc($data_mysql)) {

            $data_finished[] = $data_fetch;

        }

        return $data_finished;

    }

    function print_to_xls($filename, $data_from_mysql) {

        $file = "xls_archive/" . $filename . ".xls";
        $file_abs_path = "http://www.yoursite.com/xls_archive/" . $file;
        $content = "date\t new users\t old users\t income\n";;

        foreach($data_from_mysql as $data) {

            $content .= $data['date'] . "\t " . $data['new_users'] . "\t " . $data['old_users'] . "\t " . $data['income'] . "\n";

        }

        $saving_file = $file;
        $fo = fopen($saving_file, 'w') or die("can't open file");
        $data_to_write = $content;
        fwrite($fo, $data_to_write);
        fclose($fo);

        header("Content-type: application/ms-excel");
        header("Content-Transfer-Encoding: Binary"); 
        header("Content-Disposition: attachment; filename=" . $file);

        readfile($file_abs_path);

    }

    $connect_to_db = connect_db("db_name");

    if ($connect_to_db) {

        $data_to_print = get_data_from_mysql();

        $today = date("Y_m_d");
        $filename = "dailyreport_" . $today;

        print_to_xls($filename, $data_to_print);

    }
    else {

        print "ERROR";

    }

?>

上面的代码将从MySQL数据库生成的文件中生成.xls *代码未经过测试,但我认为它完全有效......

第一次编辑:

你也可以手工完成,以表格格式打包数据并将标题更改为excel,如下所示:

$file ="dailyreport.xls";
$content = "data1 \t data2 \t data3 \t \n";

header("Content-type: application/ms-excel");
header("Content-Disposition: attachment; filename=" . $file);

print $content;

此代码将(生成后)提示用户下载文件“dailyreport.xls”

你也可以尝试这个php库来生成xls文件:
http://phpexcel.codeplex.com/

答案 1 :(得分:0)

您可以使用PHP从MySQL获取数据,但在Excel中显示可以通过多种方式完成,假设您有一个与mysql通信并获取所需数据及其XML格式的过程。

现在你可以执行导出到excel,理想情况下你在这里做的是将响应类型更改为application / vnd.ms-excel并以表格格式格式化数据,以便excel将其视为行和列。< / p>

以上是一种方式,还有很多其他方法,您可以在后端生成excel并为用户提供链接,或者您可以将数据作为服务公开并编写excel插件来读取数据来自您的服务

答案 2 :(得分:0)

将整个表单创建为html表,不带表单字段,然后将内容作为excel电子表格提供。

这个答案提供了更多细节

HTML table to excel - PHP

每次打开电子表格时都会收到安全警告,但由于您是创建电子表格的人,而且是内部使用的,您可以忽略安全警告。

警告会说明......

"The file you are trying to open, '[filename]', is in a different format than specified by the file extension. Verify that the file is not corrupted and is from a trusted source before opening the file. Do you want to open the file?"

另见What's XLSHTML?

相关问题