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

php導(dǎo)入導(dǎo)出excel實(shí)例

 更新時(shí)間:2013年10月25日 10:14:40   作者:  
使用PHPExcelphp導(dǎo)入excel、導(dǎo)出excel,支持office2007格式,同時(shí)兼容2003,具體看例子學(xué)習(xí)吧!

這里實(shí)現(xiàn)的PHP導(dǎo)入導(dǎo)出excel功能用到的是開(kāi)源PHPExcel,執(zhí)行下面的操作之前請(qǐng)先下載該類庫(kù)文件,官方網(wǎng)站:http://www.codeplex.com/PHPExcel,官網(wǎng)案例代碼很多,導(dǎo)出pdf什么的都有,這里主要介紹PHP導(dǎo)入導(dǎo)出excel的功能,導(dǎo)出excel文件是office2007格式,同時(shí)兼容2003。

php導(dǎo)入excel
導(dǎo)入的excel文件的數(shù)據(jù)格式,截圖如下:
下面是將該excel文件的數(shù)據(jù)導(dǎo)入到數(shù)據(jù)庫(kù)的具體代碼:

復(fù)制代碼 代碼如下:

<?php
require_once 'Classes/PHPExcel.php';
require_once 'Classes/PHPExcel/IOFactory.php';
require_once 'Classes/PHPExcel/Reader/Excel5.php';

$objReader=PHPExcel_IOFactory::createReader('Excel5');//use excel2007 for 2007 format
$objPHPExcel=$objReader->load($file_url);//$file_url即Excel文件的路徑
$sheet=$objPHPExcel->getSheet(0);//獲取第一個(gè)工作表
$highestRow=$sheet->getHighestRow();//取得總行數(shù)
$highestColumn=$sheet->getHighestColumn(); //取得總列數(shù)
//循環(huán)讀取excel文件,讀取一條,插入一條
for($j=2;$j<=$highestRow;$j++){//從第一行開(kāi)始讀取數(shù)據(jù)
 $str='';
 for($k='A';$k<=$highestColumn;$k++){            //從A列讀取數(shù)據(jù)
 //這種方法簡(jiǎn)單,但有不妥,以'\\'合并為數(shù)組,再分割\\為字段值插入到數(shù)據(jù)庫(kù),實(shí)測(cè)在excel中,如果某單元格的值包含了\\導(dǎo)入的數(shù)據(jù)會(huì)為空       
  $str.=$objPHPExcel->getActiveSheet()->getCell("$k$j")->getValue().'\\';//讀取單元格
 }
 //explode:函數(shù)把字符串分割為數(shù)組。
 $strs=explode("\\",$str);
 $sql="INSERT INTO `".TB_PREFIX."business`(`username`,`password`,`company`,`prov`,`address`,`btime`,`phone`,`email`,`name`) VALUES (
 '{$strs[0]}',
 '{$strs[1]}',
 '{$strs[2]}',
 '{$strs[3]}',
 '{$strs[4]}',
 '{$strs[5]}',
 '{$strs[6]}',
 '{$strs[7]}',
 '{$strs[8]}')";
 $db->query($sql);//這里執(zhí)行的是插入數(shù)據(jù)庫(kù)操作
}
unlink($file_url); //刪除excel文件
?>

php導(dǎo)出excel
下面直接放出本人總結(jié)的使用PHP導(dǎo)出Excel的部分調(diào)用代碼。

復(fù)制代碼 代碼如下:

<?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;

相關(guān)文章

最新評(píng)論