欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

php不使用插件导出excel的简单方法

程序员文章站 2023-01-06 21:33:07
复制代码 代码如下:$filename=date("y年m月d日")."数据信息统计结果.xls";     //文件名$ua =...

复制代码 代码如下:

$filename=date("y年m月d日")."数据信息统计结果.xls";     //文件名
$ua = $_server["http_user_agent"];        //中文文件名不乱码
if (preg_match("/msie/", $ua)) {
      $filename=urlencode($filename);
} else if (preg_match("/firefox/", $ua)) {
      $filename=$filename;
} else {
      $filename=urlencode($filename);
}
header("content-type:application/vnd.ms-excel");   //输出excel头
header("content-disposition:filename=".$filename);
$n = iconv('utf-8', 'gbk', '11');       //设置excel表格列
$t = iconv('utf-8', 'gbk', '22');
$k = iconv('utf-8', 'gbk', '33');
$s = iconv('utf-8', 'gbk', '44');
$r= iconv('utf-8', 'gbk', '55');
$a= iconv('utf-8', 'gbk', '66');

echo "{$n}\t";
echo "{$t}\t";
echo "{$k}\t";
echo "{$s}\t";
echo "{$r}\t";
echo "{$a}\t\n";

      
foreach($a as $kk=>$pp){          //对每列些数据
foreach($pp['child'] as $k=>$p){
echo iconv("utf-8", "gbk//ignore", $pp['u']."\t");
echo iconv("utf-8", "gbk//ignore", $p['u']."\t");
echo iconv("utf-8", "gbk//ignore", $p['s']."\t");
echo iconv("utf-8", "gbk//ignore", $p['t']."\t");
echo iconv("utf-8", "gbk//ignore", $p['c']."\t");
echo iconv("utf-8", "gbk//ignore", $p['t']."\t\n");
}
}

数组格式为

复制代码 代码如下:

array
(
    [0] => array
        (
[unitid] => 1234
[u] => aaa
[fid] => 1000
[shengid] => 1000
[shiid] => 0
[xianid] => 0
[rid] => 2
[orders] => 50
[sys_createuid] => 1
[sys_createtime] => 1383556976
[sys_updateuid] => 1
[sys_updatetime] => 1389853763
[child] => array
(
[0] => array
(
    [unitid] => 1035
    [u] => bbb
    [fid] => 1000
    [shengid] => 1000
    [shiid] => 0
    [xianid] => 0
    [rid] => 2
    [orders] => 50
    [sys_createuid] => 1
    [sys_createtime] => 1383556976
    [sys_updateuid] => 1
    [sys_updatetime] => 1389853763
    [funitname] =>
    [s] => 6
    [t] => 1
    [c] => 1
    [t] => 2
)
           [4] => array
(
    [u] => 合计
    [s] => 8
    [t] => 1
    [c] => 3
    [t] => 3
))))