public function insert_excel(){
set_time_limit(0);
require_once './data/excel/PHPExcel.php';
$objReader = PHPExcel_IOFactory::createReader('Excel5');
$objReader->setReadDataOnly(true);
$objPHPExcel = $objReader->load("./Public/Book1.xls");
$objWorksheet = $objPHPExcel->getSheet(0);
$highestRow = $objWorksheet->getHighestRow();
$highestColumn = $objWorksheet->getHighestColumn();
$highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn);
$excelData = array();
//注意列数和行数的起始位置
for ($row = 2; $row <= $highestRow;$row++){
for ($col=0;$col<$highestColumnIndex;$col++) {
$excelData[$row][]=(string)$objWorksheet->getCellByColumnAndRow($col,$row)->getValue();
}
}
$map = M('map_info');
//获取该地图的;
foreach($excelData as $key => $val){
//此处需要对自定义的字段序列map_map_custom.cid
$result = curl_get('http://api.map.baidu.com/geocoder/v2/?address=云南鲁甸'.$val[1].'&output=json&ak=2d9a98266c38177f3ef9688f3c228243');
$info = json_decode($result,true);
$data= array(
'uid' => 139,
'mid'=>441,
'tid'=>1472,
'status'=>0,
'title' => $val[0],
'contents' => $val[2],
'create_time' => time(),
'coordinate_lng'=>$info['result']['location']['lng'] ? $info['result']['location']['lng'] : '',
'coordinate_lat'=>$info['result']['location']['lat'] ? $info['result']['location']['lat'] : '',
);
$id = $map -> add($data);
unset($data);
echo $id." --- 成功
";
}
//dump($data);
exit;
}
上面是导入数据
public function export_excel(){
$map_info = D("MapInfo");
$mid = intval($_GET["mid"]);
$mapinformation = M("map_info");
$search_type = $_REQUEST['search_type'];
$keywords = t($_REQUEST['keywords']);
$status = $_REQUEST['status'];
if(!empty($status) || $status === '0'){
$where['status'] = $status;
$map['status'] = $status;
}
$where['uid'] = $this->uid;
$where['is_del'] = 0;
if(empty($search_type) && !empty($_REQUEST['keywords'])){
$map['search_type'] = $search_type;
$map['keywords'] = $keywords;
$_search_where['title'] = array('like',"%".$keywords."%");
$_search_where['contents'] = array('like',"%".$keywords."%");
$_search_where['_logic'] = 'or';
$where['_complex'] = $_search_where;
}elseif(!empty($search_type) && !empty($_REQUEST['keywords'])){
$map['search_type'] = $search_type;
$map['keywords'] = $keywords;
$where[$search_type] = array('like',"%".$keywords."%");;
}
$map = D('Map');
if($mid){
$where['mid'] = $mid;
$excel_title = $map->where("mid=$mid") -> getField('title');
$map_info_field = "title,contents,map_info";
}else{
$excel_title = "我的全部地图数据";
$map_info_field = "title,contents,mid";
}
$info_list = $map_info -> getAllMapInfo($mid,$where,$map_info_field);
header("Content-type:application/vnd.ms-excel");
header("Content-Disposition:filename=".$excel_title."-".date("Ymd",time()).".xls");
echo iconv('utf-8', 'gb2312', '标题'); echo "\t";
echo iconv('utf-8', 'gb2312', '简介'); echo "\t";
if(!$mid){
echo iconv('utf-8', 'gb2312', '地图名称'); echo "\t";
}else{
$custom_list = D('MapCustom') -> getCustomList($mid);
foreach($custom_list as $k =>$v){
if($v['title']){
echo iconv('utf-8', 'gb2312', $v['title']); echo "\t";
}
}
}
echo "\n";
$order = array("'•'","'\r\n'", "'\n'", "'\r'","'\s+'");//正则匹配回车,换行,空格
$replace = array('','','','','');
foreach($info_list as $key => $val){
echo mb_convert_encoding(preg_replace($order,$replace,strip_tags($val['title']) ) ,'gb2312', 'utf-8'); echo "\t";
echo mb_convert_encoding(preg_replace($order,$replace,strip_tags($val['contents']) ) ,'gb2312', 'utf-8'); echo "\t";
if(!$mid){
$map_title = $map->where("mid=".$val['mid'])->getField('title');
echo iconv('utf-8', 'gb2312', preg_replace($order,$replace,$map_title) ); echo "\t";
echo "\n";
}else{
foreach($custom_list as $ke => $va){
echo iconv('utf-8', 'gb2312', preg_replace($order,$replace, strip_tags( $val['map_info'][$va['cid']] ) ) ); echo "\t";
}
echo "\n";
}
}
}
PHPEXCEL类最好自己下载,我只是提交一个文件而已