php如何导出excel

小编给大家分享一下php如何导出excel,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!

创新互联是一家集网站建设,海州企业网站建设,海州品牌网站建设,网站定制,海州网站建设报价,网络营销,网络优化,海州网站推广为一体的创新建站企业,帮助传统企业提升企业形象加强企业竞争力。可充分满足这一群体相比中小企业更为丰富、高端、多元的互联网需求。同时我们时刻保持专业、时尚、前沿,时刻以成就客户成长自我,坚持不断学习、思考、沉淀、净化自己,让我们为更多的企业打造出实用型网站。

方法一:直接用头部信息输出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

//利用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如何导出excel的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注创新互联行业资讯频道!


文章题目:php如何导出excel
新闻来源:http://ybzwz.com/article/ghesse.html