PHP如何匯出Excel

來源:互聯網
上載者:User
本篇文章主要介紹PHP如何匯出Excel,感興趣的朋友參考下,希望對大家有所協助。

代碼如下:


<?phperror_reporting(E_ALL);date_default_timezone_set('Asia/Shanghai');require_once './Classes/PHPExcel.php';$data=array(  0=>array(    'id'=>1001,    'username'=>'張飛',    'password'=>'123456',    'address'=>'三國時高老莊250巷101室'  ),  1=>array(    'id'=>1002,    'username'=>'關羽',    'password'=>'123456',    'address'=>'三國時花果山'  ),  2=>array(    'id'=>1003,    'username'=>'曹操',    'password'=>'123456',    'address'=>'延安西路2055弄3號'  ),  3=>array(    'id'=>1004,    'username'=>'劉備',    'password'=>'654321',    'address'=>'愚園路188號3309室'  ));$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;


總結:以上就是本篇文的全部內容,希望能對大家的學習有所協助。

相關推薦:

php實現一段簡單機率的方法

php中while迴圈控制的的方法及簡單一實例

PHP實現跨網域作業的方法


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.