zoukankan      html  css  js  c++  java
  • php 导出excel文件

    out_excel.php

    <?php
    error_reporting(E_ALL);
    date_default_timezone_set('Asia/Shanghai');
    require_once 'PHPExcel/PHPExcel.php';
    $data=array(
    0=>array(
    'id'=>1001,
    'username'=>'张飞',
    'password'=>'123456',
    'address'=>'三国时高老庄250巷101室'
    ),
    1=>array(
    'id'=>1002,
    'username'=>'关羽',
    'password'=>'123456',
    'address'=>'三国时花果山'
    )
    );
    $objPHPExcel=new PHPExcel();
    $objPHPExcel->getProperties()->setCreator('http://www.jb51.net')
    ->setLastModifiedBy('http://www.jb51.net')
    ->setTitle('Office 2007 XLSX Document')
    ->setSubject('Office 2007 XLSX Document')
    ->setDescription('Document for Office 2007 XLSX, generated using PHP classes.')
    ->setKeywords('office 2007 openxml php')
    ->setCategory('Result file');
    $objPHPExcel->setActiveSheetIndex(0)
    ->setCellValue('A1','ID')
    ->setCellValue('B1','用户名')
    ->setCellValue('C1','密码')
    ->setCellValue('D1','地址');

    $i=2;
    foreach($data as $k=>$v){
    $objPHPExcel->setActiveSheetIndex(0)
    ->setCellValue('A'.$i,$v['id'])
    ->setCellValue('B'.$i,$v['username'])
    ->setCellValue('C'.$i,$v['password'])
    ->setCellValue('D'.$i,$v['address']);
    $i++;
    }
    $objPHPExcel->getActiveSheet()->setTitle('三年级2班');
    $objPHPExcel->setActiveSheetIndex(0);
    $filename=urlencode('学生信息统计表').'_'.date('Y-m-dHis');

    /**生成xlsx文件*/
    header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
    header('Content-Disposition: attachment;filename="'.$filename.'.xlsx"');
    header('Cache-Control: max-age=0');
    $objWriter=PHPExcel_IOFactory::createWriter($objPHPExcel,'Excel2007');

    /*
    *生成xls文件
    header('Content-Type: application/vnd.ms-excel');
    header('Content-Disposition: attachment;filename="'.$filename.'.xls"');
    header('Cache-Control: max-age=0');
    $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
    */
    $objWriter->save('php://output');
    exit;

  • 相关阅读:
    Android四大图片缓存(Imageloader,Picasso,Glide,Fresco)原理、特性对比
    Android系统更改状态栏字体颜色
    Zookeeper使用--命令行
    Zookeeper配置文件
    Zookeeper到底是干嘛的
    shell脚本学习之参数传递
    zabbix3.4.7之Zabbix_Trigger_Function详解
    zabbix3.4.7主动模式监控日志(多关键字)
    linux下grep命令详解
    一些不好理解的名词解释
  • 原文地址:https://www.cnblogs.com/lijiageng/p/7122870.html
Copyright © 2011-2022 走看看