添加excel上传菜单
路径:根目录\phpcms\model
创建文件excel_model.class.php
粘贴下面代码
<?php defined('IN_PHPCMS') or exit('No permission resources.'); pc_base::load_sys_class('model', '', 0); class excel_model extends model { public $table_name = ''; public function __construct() { $this->db_config = pc_base::load_config('database'); $this->db_setting = 'default';
$this->table_name = 'jxscx';//数据表名称改成自己的 parent::__construct(); } } ?>
路径根目录\phpcms\modules\content
找到content.php
最下面添加代码
public function public_add_excel() { if($_FILES){ $file_path=$_FILES['file_stu']['tmp_name']; require PC_PATH.'libs/classes/PHPExcel.php'; require PC_PATH.'libs/classes/PHPExcel/IOFactory.php'; require PC_PATH.'libs/classes/PHPExcel/Reader/Excel2007.php'; require PC_PATH.'libs/classes/PHPExcel/Reader/Excel5.php';
$this->db = pc_base::load_model('excel_model'); //$this->db_data = pc_base::load_model('luqu_data_model'); //$this->db_cat = pc_base::load_model('category_model');
//上传地址,时间戳防止重复,不然直接点导入会把上次的文件再次导入 $fname='./excel/'.time().'tmp.xls'; move_uploaded_file($file_path, $fname);//将临时文件移动到上传地址 $array=$this->format_excel2array($fname);//从上传地址读取excel $where ="id>0"; //删除表中数据 $this->db->delete($where,true); //删除表中数据 //array_shift($array); //将数组入库按照官方方法入库 foreach ($array as $key => $value) { $mzinfo=array(); $mzinfo['title']=$value['A'];//标题 $mzinfo['keywords']=$value['B'];//联系方式 $mzinfo['description']=$value['C']; //经销商等级 $mzinfo['catid']=65; //经销商等级 $mzinfo['inputtime']=time(); //经销商等级 $this->db->insert($mzinfo,true); }
showmessage(L('operation_success'), HTTP_REFERER); }else{ include template('content','excel'); }
}
路径根目录\phpcms\templates\default\content
创建文件 excel.html
添加代码
<form method="post" action="?m=content&c=content&a=public_add_excel" enctype="multipart/form-data"> <h3>导入excel表:</h3><input type="file" name="file_stu"/> <input type="submit" value="导入"/> </form>
路径根目录\phpcms\libs\classes
PHPexcel 文件包放到这