相對(duì)于laravel TP使用phpExcel比較麻煩蚁署,因?yàn)闆](méi)有composer,所以必須先引入phpexcel文檔
但是沒(méi)有命名空間使用use因不進(jìn)去曙聂,所以需要手動(dòng)引入
namespace? app\index\controller;
use? think\Loader;
use? think\Controller;
class Excel extends Controller
{
????function excel(){
$path=dirname(__FILE__);//找到當(dāng)前腳本所在路徑
Loader::import('PHPExcel.Classes.PHPExcel');
Loader::import('PHPExcel.Classes.PHPExcel.IOFactory.PHPExcel_IOFactory');
$PHPExcel=new\PHPExcel();
//????????$PHPExcel_IOFactory?=?new?PHPExcel_IOFactory();
$PHPSheet=$PHPExcel->getActiveSheet();
$PHPSheet->setTitle("demo");//給當(dāng)前活動(dòng)sheet設(shè)置名稱
$PHPSheet->setCellValue("A1","姓名")->setCellValue("B1","分?jǐn)?shù)");//表格數(shù)據(jù)
$PHPSheet->setCellValue("A2","張三")->setCellValue("B2","2121");//表格數(shù)據(jù)
$PHPWriter=\PHPExcel_IOFactory::createWriter($PHPExcel,"Excel2007");
header('Content-Disposition:?attachment;filename="表單數(shù)據(jù).xlsx"');
header('Content-Type:?application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
$PHPWriter->save("php://output");//表示在$path路徑下面生成demo.xlsx文件
}
}
excel表格數(shù)據(jù)導(dǎo)入數(shù)據(jù)庫(kù):
function inserExcel()
{
Loader::import('PHPExcel.Classes.PHPExcel');
Loader::import('PHPExcel.Classes.PHPExcel.IOFactory.PHPExcel_IOFactory');
Loader::import('PHPExcel.Classes.PHPExcel.Reader.Excel5');
//獲取表單上傳文件
$file=request()->file('excel');
$info=$file->validate(['ext'=>'xlsx'])->move(ROOT_PATH.'public'.DS.'uploads');
if($info){
//????????????echo?$info->getFilename();
$exclePath=$info->getSaveName();//獲取文件名
$file_name=ROOT_PATH.'public'.DS.'uploads'.DS.$exclePath;//上傳文件的地址
$objReader=\PHPExcel_IOFactory::createReader('Excel2007');
$obj_PHPExcel=$objReader->load($file_name,$encode='utf-8');//加載文件內(nèi)容,編碼utf-8
$excel_array=$obj_PHPExcel->getsheet(0)->toArray();//轉(zhuǎn)換為數(shù)組格式
array_shift($excel_array);//刪除第一個(gè)數(shù)組(標(biāo)題);
$city=[];
foreach($excel_arrayas$k=>$v){
$city[$k]['Id']=$v[0];
$city[$k]['code']=$v[1];
$city[$k]['path']=$v[2];
$city[$k]['pcode']=$v[3];
$city[$k]['name']=$v[4];
}
Db::name('city')->insertAll($city);//批量插入數(shù)據(jù)
}else{
echo?$file->getError();
}
導(dǎo)出
publicfunctionphpexcel(){
//測(cè)試
$this->display("User:xx");//跨模塊分配頁(yè)面User模塊xx.html
// xx\ThinkPHP/Extend/Vendor/Excel/PHPExcel.phpVendor('Excel.PHPExcel');//導(dǎo)入thinkphp第三方類庫(kù)
//創(chuàng)建一個(gè)讀Excel模板的對(duì)象$objReader=PHPExcel_IOFactory::createReader('Excel5');$objPHPExcel=$objReader->load ("template.xls");//讀取模板,模版放在根目錄
//獲取當(dāng)前活動(dòng)的表
$objActSheet=$objPHPExcel->getActiveSheet();
$objActSheet->setTitle('xxx');//設(shè)置excel的標(biāo)題
$objActSheet->setCellValue('A1','患者信息導(dǎo)出');
$objActSheet->setCellValue('A2','范圍:'.time().'--'.time()+5000);
$objActSheet->setCellValue('F2','導(dǎo)出時(shí)間:'.date('Y-m-d H:i:s'));//現(xiàn)在開(kāi)始輸出列頭了$objActSheet->setCellValue('A3','編號(hào)');
$objActSheet->setCellValue('B3','姓名');
$objActSheet->setCellValue('C3','性別');//具體有多少列,有多少就寫(xiě)多少,跟下面的填充數(shù)據(jù)對(duì)應(yīng)上就可以
//現(xiàn)在就開(kāi)始填充數(shù)據(jù)了 (從數(shù)據(jù)庫(kù)中)
$baseRow= 4;//數(shù)據(jù)從N-1行開(kāi)始往下輸出 這里是避免頭信息被覆蓋
$list=array(array('id'=>'似懂非懂','name'=>'的身份','sex'=>'好好'),array('id'=>'體育靜態(tài)','name'=>'全文','sex'=>'好那么好'),array('id'=>'他','name'=>'啥都','sex'=>'vb'),array('id'=>'大范甘迪','name'=>'后感覺(jué)','sex'=>'剛回家'),array('id'=>'阿瑟斯','name'=>'好','sex'=>'如啼眼'),array('id'=>'為二位','name'=>'福德宮','sex'=>'為'),);
foreach($listas$r=>$dataRow)
?{$row=$baseRow+$r;//將數(shù)據(jù)填充到相對(duì)應(yīng)的位置,對(duì)應(yīng)上面輸出的列頭
$objPHPExcel->getActiveSheet()->setCellValue('A' .$row,$dataRow['id'] );
$objPHPExcel->getActiveSheet()->setCellValue('B' .$row,$dataRow['name'] );
$objPHPExcel->getActiveSheet()->setCellValue('C' .$row,$dataRow['sex'] );
}//導(dǎo)出$filename='xxx患者信息導(dǎo)出';//excel文件名稱
$filename=iconv('utf-8',"gb2312",$filename);//轉(zhuǎn)換名稱編碼庶艾,防止亂碼
header( 'Content-Type: application/vnd.ms-excel;charset=utf-8');
header( 'Content-Disposition: attachment;filename="' .$filename. '.xls"' );//”‘.$filename.’.xls”
header( 'Cache-Control: max-age=0');
$objWriter= PHPExcel_IOFactory::createWriter ($objPHPExcel, 'Excel5' );//在內(nèi)存中準(zhǔn)備一個(gè)excel2003文件
$objWriter->save ('php://output');
}
參考鏈接:http://blog.csdn.net/will5451/article/details/51315267
????????????????????http://www.thinkphp.cn/topic/48111.html