php 导出excel CVS格式

php  excel导出 CVS格式  乐杨俊

 

<?php

  $param=array();   //模拟导出数据
  $param [1] = array (
   ‘po‘ => ‘651651616‘,
            ‘pick_mode‘ => ‘1‘,
            ‘auto_add_goods‘ => ‘0‘,
            ‘type‘ => ‘jit‘,
            ‘schedule_time‘ => ‘20013-00-00‘,
            ‘num‘ => ‘25045‘,
            ‘sale_num‘ => ‘2054‘,
            ‘unpick_num‘ => ‘6234‘,
            ‘warehouse_name‘ => ‘bj‘, );
  $param [2] = array (
   ‘po‘ => ‘651651612‘,
            ‘pick_mode‘ => ‘1‘,
            ‘auto_add_goods‘ => ‘0‘,
            ‘type‘ => ‘jit2‘,
            ‘schedule_time‘ => ‘20013-00-002‘,
            ‘num‘ => ‘2502‘,
            ‘sale_num‘ => ‘20267‘,
            ‘unpick_num‘ => ‘622‘,
            ‘warehouse_name‘ => ‘bj2‘,
  );
  $param [3] = array (
   ‘po‘ => ‘651651613‘,
            ‘pick_mode‘ => ‘1‘,
            ‘auto_add_goods‘ => ‘0‘,
            ‘type‘ => ‘jit3‘,
            ‘schedule_time‘ => ‘20013-00-003‘,
            ‘num‘ => ‘2503‘,
            ‘sale_num‘ => ‘203‘,
            ‘unpick_num‘ => ‘6236‘,
            ‘warehouse_name‘ => ‘bj3‘,
  );
  $param [4] = array (
   ‘po‘ => ‘651651614‘,
            ‘pick_mode‘ => ‘1‘,
            ‘auto_add_goods‘ => ‘0‘,
            ‘type‘ => ‘jit4‘,
            ‘schedule_time‘ => ‘20013-00-004‘,
            ‘num‘ => ‘2504‘,
            ‘sale_num‘ => ‘204‘,
            ‘unpick_num‘ => ‘6246‘,
            ‘warehouse_name‘ => ‘bj4‘,
  );
  $param [5] = array (
   ‘po‘ => ‘812231134‘,
            ‘pick_mode‘ => ‘0‘,
            ‘auto_add_goods‘ => ‘1‘,
            ‘type‘ => ‘jit5‘,
            ‘schedule_time‘ => ‘2013-00-004‘,
            ‘num‘ => ‘8888‘,
            ‘sale_num‘ => ‘8848‘,
            ‘unpick_num‘ => ‘6000‘,
            ‘warehouse_name‘ => ‘BJ‘,
  );

  $name=‘leyangjun‘;
  $filename = mb_convert_encoding($name, ‘gb2312‘, ‘utf-8‘);
        $title = array(
            ‘po‘ => ‘PO单编号‘,
            ‘pick_mode‘ => ‘拣货方式‘,
            ‘auto_add_goods‘ => ‘自动补货‘,
            ‘type‘ => ‘合作模式‘,
            ‘schedule_time‘ => ‘档期时间‘,
            ‘num‘ => ‘虚拟总库存‘,
            ‘sale_num‘ => ‘销售数‘,
            ‘unpick_num‘ => ‘未拣货‘,
            ‘warehouse_name‘ => ‘仓库‘,
        );
       $ii = 0;
        if (!empty($param)) {
            foreach ($param as $key => $val) {
                $val[‘type‘] = isset($val[‘type‘]) ? $val[‘type‘] : ‘‘;
                $val[‘schedule_time‘] = isset($val[‘sell_time_from‘]) || isset($val[‘sell_time_to‘]) ? ($val[‘sell_time_from‘] . ‘—‘ . $val[‘sell_time_to‘]) : ‘‘;
                $val[‘pick_mode‘] = isset($val[‘pick_mode‘]) ? ($val[‘pick_mode‘] > 1 ? ‘分类‘ : ‘普通‘) : ‘‘;
                $val[‘auto_add_goods‘] = isset($val[‘auto_add_goods‘]) ? ($val[‘auto_add_goods‘] > 0 ? ‘开启‘ : ‘关闭‘) : ‘‘;
                $val[‘schedule_time‘] = isset($val[‘schedule_time‘]) ? ($val[‘schedule_time‘] == ‘0000-00-00 00:00:00—0000-00-00 00:00:00‘ ? ‘‘ : $val[‘schedule_time‘]) : ‘‘;

                foreach ($title as $tk => $tv) {
                    if (!isset($val[$tk])) {
                        $val[$tk] = ‘‘;
                    }
                    $list[$ii][] = $val[$tk];
                }
                $ii++;
            }
        }

        //未设置给默认--防报错
        if (!isset($list)) {
            $list[0][0] = "";
            $list[0][1] = "";
            $list[0][2] = "";
            $list[0][3] = "";
            $list[0][4] = "";
            $list[0][5] = "";
            $list[0][6] = "";
            $list[0][7] = "";
            $list[0][8] = "";
            $list[0][9] = "";
            $list[0][10] = "";
        }

        //表头数据
        $headerDate = array_values($title);
        //内容体数据
        $bodyData = $list;

        //设置工作区名称
        $sheetName = ‘Jit_po列表详情‘;
        //设置要导出的excel文件名
  
        $fileName = $name;
        //设置要导出的excel文件后缀名,支持xls和xlsx
        $fileExt = ‘xls‘;
        //调用类库导出excel
        //ini_set(‘memory_limit‘, ‘512M‘);     
   //cvs格式 
    to_csv($fileName, $headerDate, $bodyData, array(0, 10, 14, 15), $flag=1);
   

 

  //CVS格式
/*  
 function to_csv($file_name, $headerDate, $bodyData, $array_text, $flag) {

        // 输出Excel文件头,可把user.csv换成你要的文件名 
        if (!headers_sent()) {
            header(‘Content-Type: application/vnd.ms-excel‘);
            header(‘Content-Disposition: attachment;filename="‘ . $file_name . ‘.csv"‘);
            header(‘Cache-Control: max-age=0‘);
        }
        // 打开PHP文件句柄,php://output 表示直接输出到浏览器 
        $fp = fopen(‘php://output‘, ‘a‘);
        foreach ($headerDate as $i => $v) {
            // CSV的Excel支持GBK编码,一定要转换,否则乱码 
            $headerDate[$i] = iconv(‘utf-8‘, ‘gbk‘, $v);
        }
        // 将数据通过fputcsv写到文件句柄   
        if ($flag == 1) {
            fputcsv($fp, $headerDate);
        }
        // 逐行取出数据,不浪费内存 
        foreach ($bodyData as $row) {
            foreach ($row as $i => $v) {
                $row[$i] = iconv(‘utf-8‘, ‘gbk‘, $v);
                if (in_array($i, $array_text)) {
                    $row[$i] = "\t" . $row[$i];
                }
            }
            fputcsv($fp, $row);
        }
    }
*/
  

?>

php 导出excel CVS格式

上一篇:应用netlink的内核模块和应用程序Makefile编写


下一篇:PHP正则提取啊汉字字母数字下划线的方法以及常用正则表达式