欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

PHP導(dǎo)出Excel實(shí)例講解

 更新時(shí)間:2016年01月24日 16:39:02   投稿:lijiao  
這篇文章主要為大家介紹了PHP導(dǎo)出Excel的實(shí)例,

本次實(shí)現(xiàn)PHP導(dǎo)出Excel文件使用的是PHP開源程序PHPExcel,部分關(guān)鍵代碼分享給大家,具體內(nèi)容如下

<?php
error_reporting(E_ALL);
date_default_timezone_set('Asia/Shanghai');
require_once './Classes/PHPExcel.php';
$data=array(
  0=>array(
    'id'=>1001,

    'username'=>'張飛',
    'password'=>'123456',
    'address'=>'三國(guó)時(shí)高老莊250巷101室'
  ),
  1=>array(
    'id'=>1002,
    'username'=>'關(guān)羽',
    'password'=>'123456',
    'address'=>'三國(guó)時(shí)花果山'
  ),
  2=>array(
    'id'=>1003,
    'username'=>'曹操',
    'password'=>'123456',
    'address'=>'延安西路2055弄3號(hào)'
  ),
  3=>array(
    'id'=>1004,
    'username'=>'劉備',
    'password'=>'654321',
    'address'=>'愚園路188號(hào)3309室'
  )
);
$objPHPExcel=new PHPExcel();
$objPHPExcel->getProperties()->setCreator('http://www.dbjr.com.cn')
               ->setLastModifiedBy('http://www.dbjr.com.cn')
               ->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('三年級(jí)2班');

$objPHPExcel->setActiveSheetIndex(0);
$filename=urlencode('學(xué)生信息統(tǒng)計(jì)表').'_'.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;

注意:如果導(dǎo)出中文時(shí)出現(xiàn)亂碼,可以嘗試將字符串轉(zhuǎn)換成gb2312,代碼類似如下:

查看代碼打印

$str=mb_convert_encoding("gb2312","UTF-8",$str);

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助。

相關(guān)文章

最新評(píng)論