1.我这里以TP3.2为例进行讲解。
2.首先下载PHPExcel ,并放在项目ThinkPHP\Library\Vendor\PHPExcel目录下。
3.控制器代码如下:
<?php
// 本类由系统自动生成,仅供测试用途
namespace Home\Controller;
use Think\Controller;
class IndexController extends Controller {
/**
*
* Enter 导出excel共同方法 ...
* @param unknown_type $expTitle
* @param unknown_type $expCellName
* @param unknown_type $expTableData
*/
function index(){
$this->display();
}
public function exportExcel($expTitle,$expCellName,$expTableData){
$xlsTitle = iconv('utf-8', 'gb2312', $expTitle);//文件名称
$fileName = $_SESSION['account'].date('_YmdHis');//or $xlsTitle 文件名称可根据自己情况设定
$cellNum = count($expCellName);
$dataNum = count($expTableData);
vendor("PHPExcel.PHPExcel");
$objPHPExcel = new \PHPExcel();
$cellName = array('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','AA','AB','AC','AD','AE','AF','AG','AH','AI','AJ','AK','AL','AM','AN','AO','AP','AQ','AR','AS','AT','AU','AV','AW','AX','AY','AZ');
$objPHPExcel->getActiveSheet(0)->mergeCells('A1:'.$cellName[$cellNum-1].'1');//合并单元格
// $objPHPExcel->setActiveSheetIndex(0)->setCellValue('A1', $expTitle.' Export time:'.date('Y-m-d H:i:s'));
for($i=0;$i<$cellNum;$i++){
$objPHPExcel->setActiveSheetIndex(0)->setCellValue($cellName[$i].'2', $expCellName[$i][1]);
}
// Miscellaneous glyphs, UTF-8
for($i=0;$i<$dataNum;$i++){
for($j=0;$j<$cellNum;$j++){
$objPHPExcel->getActiveSheet(0)->setCellValue($cellName[$j].($i+3), $expTableData[$i][$expCellName[$j][0]]);
}
}
header('pragma:public');
header('Content-type:application/vnd.ms-excel;charset=utf-8;name="'.$xlsTitle.'.xls"');
header("Content-Disposition:attachment;filename=$fileName.xls");//attachment新窗口打印inline本窗口打印
$objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');
exit;
}
/**
*
* 导出Excel
*/
function expUser(){//导出Excel
$xlsName = "User";
$xlsCell = array(
array('bikeid','bikeid'),
array('mac','蓝牙MAC'),
array('mac2','蓝牙MAC'),
);
$xlsModel = M('Member2');
$xlsData = $xlsModel->Field('bikeid,mac,mac2')->select();
$this->exportExcel($xlsName,$xlsCell,$xlsData);
}
/**
*
* 显示导入页面 ...
*/
/**实现导入excel
**/
function impUser(){
if (!empty($_FILES)) {
//import("@.ORG.UploadFile");
$config=array(
'exts'=>array('xlsx','xls'),
'rootPath'=>"./Public/",
'savePath'=>'Uploads/',
//'autoSub' => true,
'subName' => array('date','Ymd'),
);
$upload = new \Think\Upload($config);
//var_dump($upload);exit;
if (!$info=$upload->upload()) {
$this->error($upload->getError());
} /*else {
//$info = $upload->getUploadFileInfo();
}
*/
vendor("PHPExcel.PHPExcel");
$file_name=$upload->rootPath.$info['import']['savepath'].$info['import']['savename'];
$extension = substr(strrchr($_FILES["import"]["name"], '.'), 1);
if ($extension =='xlsx') {
$objReader = \PHPExcel_IOFactory::createReader('Excel2007');
} else if ($extension =='xls') {
$objReader = \PHPExcel_IOFactory::createReader('Excel5');
}
$objPHPExcel = $objReader->load($file_name,$encode='utf-8');
$sheet = $objPHPExcel->getSheet(0);
$highestRow = $sheet->getHighestRow(); // 取得总行数
$highestColumn = $sheet->getHighestColumn(); // 取得总列数
for($i=2;$i<=$highestRow;$i++)
{
$data2['bikeid'] = $objPHPExcel->getActiveSheet()->getCell("A".$i)->getValue();
$mac = $objPHPExcel->getActiveSheet()->getCell("B".$i)->getValue();
$data2['mac'] = str_replace(":","",$mac);
$mac2 = $objPHPExcel->getActiveSheet()->getCell("C".$i)->getValue();
$data2['mac2'] = str_replace(":","",$mac2);
$res = M('Member2')->add($data2);
}
$sum = M('Member2')->count();
$this->success('导入成功!');
}else
{
$this->error("请选择上传的文件");
}
}
}
3.视图代码如下:
<html>
<head>
</head>
<body>
<P><a href="{:U('Index/expUser')}" >导出数据并生成excel</a></P><br/>
<form action="{:U('Index/impUser')}" method="post" enctype="multipart/form-data">
<input type="file" name="import"/>
<input type="hidden" name="table" value="tablename"/>
<input type="submit" value="导入"/>
</form>
</body>
</html>
全部源码我已经上传到csdn,下载地址:http://download.csdn.net/download/ougexingfuba/10131587