能不能用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;
}
$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";
}
}
} 追é®
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
温馨提示:答案为网友推荐,仅供参考