php 导出表到excel文件格式,PHP导出数据到Excel

本章内容主要是通过代码演示php导出excel表

下面是php创建数据集和文章排序数组代码:

$data = array(

array("firstname" => "Mary", "lastname" => "Johnson", "age" => 25),

array("firstname" => "Amanda", "lastname" => "Miller", "age" => 18),

array("firstname" => "James", "lastname" => "Brown", "age" => 31),

array("firstname" => "Patricia", "lastname" => "Williams", "age" => 7),

array("firstname" => "Michael", "lastname" => "Davis", "age" => 43),

array("firstname" => "Sarah", "lastname" => "Miller", "age" => 24),

array("firstname" => "Patrick", "lastname" => "Miller", "age" => 27)

);

?>

将输出的结果进行excel表排序代码:

header("Content-Type: text/plain");

$flag = false;

foreach($data as $row) {

if(!$flag) {

// display field/column names as first row

echo implode("\t", array_keys($row)) . "\r\n";

$flag = true;

}

echo implode("\t", array_values($row)) . "\r\n";

}

exit;

?>

php输出excel表结果:

firstnamelastnameage

MaryJohnson25

AmandaMiller18

JamesBrown31

PatriciaWilliams7

MichaelDavis43

SarahMiller24

PatrickMiller27

php导出excel(xls格式)方法1:

function cleanData(&$str)

{

$str = preg_replace("/\t/", "\\t", $str);

$str = preg_replace("/\r?\n/", "\\n", $str);

if(strstr($str, '"')) $str = '"' . str_replace('"', '""', $str) . '"';

}

// filename for download

$filename = "website_data_" . date('Ymd') . ".xls";

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

header("Content-Type: application/vnd.ms-excel");

$flag = false;

foreach($data as $row) {

if(!$flag) {

// display field/column names as first row

echo implode("\t", array_keys($row)) . "\r\n";

$flag = true;

}

array_walk($row, 'cleanData');

echo implode("\t", array_values($row)) . "\r\n";

}

exit;

?>

php导出excel(xls格式)方法2:

// Original PHP code by Chirp Internet: www.chirp.com.au

// Please acknowledge use of this code by including this header.

function cleanData(&$str)

{

$str = preg_replace("/\t/", "\\t", $str);

$str = preg_replace("/\r?\n/", "\\n", $str);

if(strstr($str, '"')) $str = '"' . str_replace('"', '""', $str) . '"';

}

// filename for download

$filename = "website_data_" . date('Ymd') . ".xls";

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

header("Content-Type: application/vnd.ms-excel");

$flag = false;

$result = pg_query("SELECT * FROM table ORDER BY field") or die('Query failed!');

while(false !== ($row = pg_fetch_assoc($result))) {

if(!$flag) {

// display field/column names as first row

echo implode("\t", array_keys($row)) . "\r\n";

$flag = true;

}

array_walk($row, 'cleanData');

echo implode("\t", array_values($row)) . "\r\n";

}

exit;

?>

php导出excel(csv格式)方法3:

// Original PHP code by Chirp Internet: www.chirp.com.au

// Please acknowledge use of this code by including this header.

function cleanData(&$str)

{

if($str == 't') $str = 'TRUE';

if($str == 'f') $str = 'FALSE';

if(preg_match("/^0/", $str) || preg_match("/^\+?\d{8,}$/", $str) || preg_match("/^\d{4}.\d{1,2}.\d{1,2}/", $str)) {

$str = "'$str";

}

if(strstr($str, '"')) $str = '"' . str_replace('"', '""', $str) . '"';

}

// filename for download

$filename = "website_data_" . date('Ymd') . ".csv";

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

header("Content-Type: text/csv");

$out = fopen("php://output", 'w');

$flag = false;

$result = pg_query("SELECT * FROM table ORDER BY field") or die('Query failed!');

while(false !== ($row = pg_fetch_assoc($result))) {

if(!$flag) {

// display field/column names as first row

fputcsv($out, array_keys($row), ',', '"');

$flag = true;

}

array_walk($row, 'cleanData');

fputcsv($out, array_values($row), ',', '"');

}

fclose($out);

exit;

?>

php导出excel(xls格式)方法4:

// Original PHP code by Chirp Internet: www.chirp.com.au

// Please acknowledge use of this code by including this header.

function cleanData(&$str)

{

if($str == 't') $str = 'TRUE';

if($str == 'f') $str = 'FALSE';

if(preg_match("/^0/", $str) || preg_match("/^\+?\d{8,}$/", $str) || preg_match("/^\d{4}.\d{1,2}.\d{1,2}/", $str)) {

$str = "'$str";

}

if(strstr($str, '"')) $str = '"' . str_replace('"', '""', $str) . '"';

$str = mb_convert_encoding($str, 'UTF-16LE', 'UTF-8');

}

// filename for download

$filename = "website_data_" . date('Ymd') . ".csv";

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

header("Content-Type: text/csv; charset=UTF-16LE");

$out = fopen("php://output", 'w');

$flag = false;

$result = pg_query("SELECT * FROM table ORDER BY field") or die('Query failed!');

while(false !== ($row = pg_fetch_assoc($result))) {

if(!$flag) {

// display field/column names as first row

fputcsv($out, array_keys($row), ',', '"');

$flag = true;

}

array_walk($row, 'cleanData');

fputcsv($out, array_values($row), ',', '"');

}

fclose($out);

exit;

?>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值