php 导出excel,csv

function result_export($title, $list, $field) {

//调用PHP_Excel

    Vendor("PhpExcel.PHPExcel");

    $objExcel = new PHPExcel();

    $objWriter = new PHPExcel_Writer_Excel2007($objExcel);

    $objExcel->setActiveSheetIndex(0);

    $objActSheet = $objExcel->getActiveSheet();

    $objActSheet->setTitle($title);

    $letter = 'A';

    foreach ($field as $values) {

        $objActSheet->setCellValue($letter . '1', $values);

        ++$letter;

    }

    if (is_array($list)) {

        foreach ($list as $k => $v) {

            $letter = 'A';

            $k = $k + 2;

            reset($field);

            foreach ($field as $key => $value) {

                $objActSheet->setCellValueExplicit($letter . $k, $v[$key], PHPExcel_Cell_DataType::TYPE_STRING); //setCellValue($letter.$k,$v[$key])

                ++$letter;

            }

        }

    }

    $objExcel->setActiveSheetIndex(0);

    // 框架中若使用了autoload,会和phpexcel的autoload冲突,需要重新注册,如:spl_autoload_register(array('Think', 'autoload')); 

    header("Content-Type:application/force-download");

    header("Content-Type:application/octet-stream");

    header("Content-Type:application/download");

    header('Content-Disposition:inline;filename="' . mb_convert_encoding($title, 'gb2312', 'UTF-8') . '.xlsx"');

    header("Content-Transfer-Encoding: binary");

    header("Last-Modified:" . gmdate("D, d M Y H:i:s") . "GMT");

    header("Cache-Control:must-revalidate,post-check=0,pre-check=0");

    header("Pragma: no-cache");

    $objWriter->save('php://output');

}



function result_export_csv($title, $list, $field) {

    $filename = $title . ".csv";

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

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

    header('Cache-Control:must-revalidate,post-check=0,pre-check=0');

    header('Expires:0');

    header('Pragma:public');

    $data = implode(',', $field) . PHP_EOL;

    if (is_array($list)) {

        foreach ($list as $k => $v) {

            reset($field);

            $row = '';

            foreach ($field as $key => $value) {

                $row .= $v[$key] . ',';

            }

            $data .= rtrim($row, ',') . PHP_EOL;

        }

    }

    echo $data;

}



/**

 * export data from database

 * @param type $head $head = array('name'=>'姓名', 'gender'=>'性别', 'age'=>'年龄', 'email'=>'Email', 'phone'=>'电话', '……');

 * @param type $list

 */

function exportCSV($head, $list, $title = '') {

    //$this->outputCsv($head,$list,date('YmdHis'));

    // 输出Excel文件头,可把user.csv换成你要的文件名

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

    header('Content-Disposition: attachment;filename = "' . ($title ? $title : date('YmdHis')) . '.csv"');

    header('Cache-Control: max-age=0');

    $fp = fopen('php://output', 'a');

    // 输出Excel列名信息

    foreach ($head as $i => $v) {

        // CSV的Excel支持GBK编码,一定要转换,否则乱码

        $head[$i] = iconv('utf-8', 'gbk', $v);

    }

    // 将数据通过fputcsv写到文件句柄

    fputcsv($fp, $head);

    foreach ($list as $val) {

        foreach ($head as $i => $v) {

            $row[] = mb_convert_encoding($val[$i], 'gbk', 'utf-8');

        }

        fputcsv($fp, $row);

    }

    fclose($fp);

}

 

你可能感兴趣的:(导出Excel)