php导出excel方法

方法一:

//直接用头部信息输出excel格式文件,内容以表格形式展示。

$filename='orderexcel';
    header("Content-type: application/vnd.ms-excel; charset=gbk");
    header("Content-Disposition: attachment; filename=$filename.xls");
//$list为数据库查询结果,既二维数组。利用循环出表格,直接输出,既在线生成execl文件
 foreach($list as $key => $val)
    {
        $data .= "";
        $data .= "";
        $data .= "";
        $data .= "";
        $data .= "";
        $data .= "
订单号:".$val['order_sn']."用户名:".$val['user_name']."收货人:".$val['consignee']."联系电话:".$val['tel']."
送货地址:".$val['address']."下单时间:".$val['add_time']."
序号货号商品名称市场价本店价购买数量小计
1".$val['goods_sn']."".$val['goods_name']."".$val['market_price']."".$val['goods_price']."".$val['goods_number']."".$val['money']."
"; $data .= "
"; } $data.=''; if (EC_CHARSET != 'gbk') { echo yzy_iconv(EC_CHARSET, 'gbk', $data) . "\t"; } else { echo $data. "\t"; }

方法二:

//利用excel导出插件PHPExcel
 // 引入phpexcel核心类文件
    require_once ROOT_PATH . '/includes/phpexcel/Classes/PHPExcel.php';
    // 实例化excel类
    $objPHPExcel = new PHPExcel();
    // 操作第一个工作表
    $objPHPExcel->setActiveSheetIndex(0);
    // 设置sheet名
    $objPHPExcel->getActiveSheet()->setTitle('xx列表');

    // 设置表格宽度
    $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(10);
    $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(20);
    $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(15);
    $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(50);
    $objPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth(20);
    $objPHPExcel->getActiveSheet()->getColumnDimension('F')->setWidth(5);
    $objPHPExcel->getActiveSheet()->getColumnDimension('G')->setWidth(20);


    // 列名表头文字加粗
    $objPHPExcel->getActiveSheet()->getStyle('A1:J1')->getFont()->setBold(true);
    // 列表头文字居中
    $objPHPExcel->getActiveSheet()->getStyle('A1:J1')->getAlignment()
        ->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);

    // 列名赋值
    $objPHPExcel->getActiveSheet()->setCellValue('A1', '编号');
    $objPHPExcel->getActiveSheet()->setCellValue('B1', '姓名');
    $objPHPExcel->getActiveSheet()->setCellValue('C1', '电话');
    $objPHPExcel->getActiveSheet()->setCellValue('D1', '擅长');
    $objPHPExcel->getActiveSheet()->setCellValue('E1', '创建日期');
    $objPHPExcel->getActiveSheet()->setCellValue('F1', '审核');
    $objPHPExcel->getActiveSheet()->setCellValue('G1', '审核时间');

    // 数据起始行
    $row_num = 2;
    // 向每行单元格插入数据
    foreach($res as $value)
    {
        // 设置所有垂直居中
        $objPHPExcel->getActiveSheet()->getStyle('A' . $row_num . ':' . 'J' . $row_num)->getAlignment()
            ->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
        // 设置价格为数字格式
        $objPHPExcel->getActiveSheet()->getStyle('D' . $row_num)->getNumberFormat()
            ->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_NUMBER_00);
        // 居中
        $objPHPExcel->getActiveSheet()->getStyle('E' . $row_num . ':' . 'H' . $row_num)->getAlignment()
            ->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);

        // 设置单元格数值
        $objPHPExcel->getActiveSheet()->setCellValue('A' . $row_num, $value['id']);
        $objPHPExcel->getActiveSheet()->setCellValue('B' . $row_num, $value['teacher_name']);
        $objPHPExcel->getActiveSheet()->setCellValue('C' . $row_num, $value['teacher_mobile']);
        $objPHPExcel->getActiveSheet()->setCellValue('D' . $row_num, $value['teacher_desc']);
        $objPHPExcel->getActiveSheet()->setCellValue('E' . $row_num, date('Y-m-d h:i:s',$value['createtime']));
        $objPHPExcel->getActiveSheet()->setCellValue('F' . $row_num, $value['state'] ? '√' : '×');
        $objPHPExcel->getActiveSheet()->setCellValue('G' . $row_num, date('Y-m-d h:i:s',$value['statetime']));
        $row_num++;
    }

    $outputFileName = 'teacher_' . time() . '.xls';
    $xlsWriter = new PHPExcel_Writer_Excel5($objPHPExcel);
    header("Content-Type: application/force-download");
    header("Content-Type: application/octet-stream");
    header("Content-Type: application/download");
    header('Content-Disposition:inline;filename="' . $outputFileName . '"');
    header("Content-Transfer-Encoding: binary");
    header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
    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");
    $xlsWriter->save("php://output");
    echo file_get_contents($outputFileName);

 

你可能感兴趣的:(php)