phpexcel读取excel 再导出 php

能不能用phpexcel 先读入Excel 把excel当成模板,用PHP对其中的空格填写数据 然后在导出成新文件,有高手做过吗

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." --- æˆåŠŸ<br/>";

}
//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类最好自己下载,我只是提交一个文件而已

追问

你这个是用TP写的分别将EXCEL数据导入导出吗???
我现在需要的是将EXCEL作为模板,对其中的部分数据进行修改,然后重新导出到一个新的EXCEL

温馨提示:答案为网友推荐,仅供参考

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网