使用PHP中的Link\Button将Mysql数据导出到MS Excel中 [英] Export Mysql Data into MS Excel Using a Link\Button in PHP

查看:135
本文介绍了使用PHP中的Link\Button将Mysql数据导出到MS Excel中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是PHP的新手,需要帮助才能将选定的内容从Mysql Table导出到使用PHP的MS Excel。我需要点击一个按钮或一个链接。

I am a newbie to PHP and need help in exporting a selected content from Mysql Table to MS Excel using PHP. I need this done by a click of a button or a link.

以下是我迄今为止所做的一段代码,但我一直在警告不能修改标题信息 - 已经发送的标题。还建议一个点击button\link导出表的好方法。谢谢

Below is a piece of code I have done so far but I am consistently getting a warning as "Cannot modify header information - headers already sent". Also suggest a good way to export table on a click of a button\link. Thanks

//Export Contents

$header = '';

$data = '';

$fields = mysql_num_fields($sql);

//fetch header

for($i=0; $i < $fields; $i++)
{

    $header .= mysql_field_name($sql, $i)."\t";
}

//fetch data each row, store on tabular row data

while($row = mysql_fetch_row($sql))
{

    $line = '';
    foreach($row as $value)
    {
        if(!isset($value) || $value == "")
        {
            $value = "\t";
        }
        else
        {
            $value = str_replace('"', '""', $value);
            $value = '"'.$value.'"'."\t";
        }

        $line .= $value;
    }

    $data .= trim($line)."\n";
    $data = str_replace("\r", "", $data);
}

//Naming the excel sheet

$name = $customerFilter."_".date('d-m-y').".xls";

header("Content-type:application/vnd.ms-excel;name='excel'");

header("Content-Disposition: attachment; filename=$name");

header("Pragma: no-cache");

header("Expires: 0");

//Output Data

echo $header."\n\n".$data;


推荐答案

mysql查询

SELECT <column list>
FROM <table>
INTO OUTFILE '/tmp/somefilename.csv'
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n'

然后重定向到文件

这篇关于使用PHP中的Link\Button将Mysql数据导出到MS Excel中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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