就是给程序添加了一个excel表导入数据库功能,利用了Phpexcel这个东西。测试通过,修改了下产品属性这里的显示,把选择框,改成了输入框了。有需要的留言吧。不多就不放全部源码了。思路是下载phpexcel放到后台的插件中,\client\manage\plugs\phpexcel,
仿照后台的执行sql语句页面,制作了相应的our_excle.php和模板our_excel.html
提交页面和其他的按照https://www.cnblogs.com/zxf100/archive/2017/06/07/6957532.html中制作的,里面那个判断文件上传的代码是错误的,错误代码如下
- //判断是否选择了要上传的表格
- if (empty($_POST['myfile'])) {
- echo "<script>alert(您未选择表格);history.go(-1);</script>";
- }
复制代码 自己修改如下:- //判断是否选择了要上传的表格
- if (empty($_FILES['myfile']['name'])) {
- $ourphp_font = 4;
- $ourphp_content = '您未选择表格!';
- $ourphp_class = 'ourphp_excel.php?id=ourphp';
- require 'ourphp_remind.php';
- }
复制代码 其他的基本上和案例的代码类似,就是相应的按照ourphp程序修改成了适合的代码了,其他的自己研究下了,贴出ourphp_excel.php全部代码,仅供参考了。
- <?php
- include 'ourphp_admin.php';
- include 'ourphp_checkadmin.php';
- include 'plugs/phpexcel/PHPExcel/IOFactory.php';
- if (isset($_GET["ourphp_cms"]) == "add"){
- header("Content-type:text/html;charset=utf-8");
- if($_POST["kl"] == ''){
-
- $ourphp_font = 4;
- $ourphp_content = '口令码不能为空!';
- $ourphp_class = 'ourphp_excel.php?id=ourphp';
- require 'ourphp_remind.php';
-
- }elseif($_POST["kl"] != $ourphp['validation']){
-
- $ourphp_font = 4;
- $ourphp_content = '口令码错误!';
- $ourphp_class = 'ourphp_excel.php?id=ourphp';
- require 'ourphp_remind.php';
- }
- //判断是否选择了要上传的表格
- if (empty($_FILES['myfile']['name'])) {
- $ourphp_font = 4;
- $ourphp_content = '您未选择表格!';
- $ourphp_class = 'ourphp_excel.php?id=ourphp';
- require 'ourphp_remind.php';
- }
- //获取表格的大小,限制上传表格的大小5M
- $file_size = $_FILES['myfile']['size'];
- if ($file_size>5*1024*1024) {
- $ourphp_font = 4;
- $ourphp_content = '上传失败,上传的表格不能超过5M的大小';
- $ourphp_class = 'ourphp_excel.php?id=ourphp';
- require 'ourphp_remind.php';
- }
- //限制上传表格类型
- $file_type = $_FILES['myfile']['type'];
- //application/vnd.ms-excel 为xls文件类型
- if ($file_type!='application/vnd.ms-excel') {
- $ourphp_font = 4;
- $ourphp_content = '上传失败,只能上传excel2003的xls格式!';
- $ourphp_class = 'ourphp_excel.php?id=ourphp';
- require 'ourphp_remind.php';
- }
- //判断表格是否上传成功
- if (is_uploaded_file($_FILES['myfile']['tmp_name'])) {
- require_once 'plugs/phpexcel/PHPExcel.php';
- require_once 'plugs/phpexcel/PHPExcel/IOFactory.php';
- require_once 'plugs/phpexcel/PHPExcel/Reader/Excel5.php';
- //以上三步加载phpExcel的类
- $objReader = PHPExcel_IOFactory::createReader('Excel5');//use excel2007 for 2007 format
- //接收存在缓存中的excel表格
- $filename = $_FILES['myfile']['tmp_name'];
- $objPHPExcel = $objReader->load($filename); //$filename可以是上传的表格,或者是指定的表格
- $sheet = $objPHPExcel->getSheet(0);
- $highestRow = $sheet->getHighestRow(); // 取得总行数
- $highestColumn = $sheet->getHighestColumn(); // 取得总列数
-
- //循环读取excel表格,读取一条,插入一条
- //ii表示从哪一行开始读取 从第二行开始读取,因为第一行是标题不保存
- //$a表示列号
- for($ii=2;$ii<=$highestRow;$ii++){
- $a = $objPHPExcel->getActiveSheet()->getCell("A".$ii)->getValue();//获取A(产品名称)列的值
- $b = $objPHPExcel->getActiveSheet()->getCell("B".$ii)->getValue();//获取B(产品编号)列的值
- $c = $objPHPExcel->getActiveSheet()->getCell("C".$ii)->getValue();//获取C(产品货号)列的值
- $d = $objPHPExcel->getActiveSheet()->getCell("D".$ii)->getValue();//获取D(大类)列的值
- $e = $objPHPExcel->getActiveSheet()->getCell("E".$ii)->getValue();//获取E(小类)列的值
- $f = $objPHPExcel->getActiveSheet()->getCell("F".$ii)->getValue();//获取F(价格)列的值
- $g = $objPHPExcel->getActiveSheet()->getCell("G".$ii)->getValue();//获取G(数量)列的值
- $h = $objPHPExcel->getActiveSheet()->getCell("H".$ii)->getValue();//获取H(描述)列的值
- $i = $objPHPExcel->getActiveSheet()->getCell("I".$ii)->getValue();//获取I(内容)列的值
- $j = $objPHPExcel->getActiveSheet()->getCell("J".$ii)->getValue();//获取J(缩略图)列的值
- $k = $objPHPExcel->getActiveSheet()->getCell("K".$ii)->getValue();//获取K(大图)列的值
- $zarr=array("L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z","AA","AB","AC","AD","AE","AF","AG","AH","AI");
- $zcount=array_search($highestColumn,$zarr);
- $n=array();
- for($jj=0;$jj<=$zcount;$jj++){
- $l=$objPHPExcel->getActiveSheet()->getCell($zarr[$jj].$ii)->getValue();//获取L-AI(附加属性)列的值
- $m=$objPHPExcel->getActiveSheet()->getCell($zarr[$jj]."1")->getValue();//获取L-AI属性名
- $n[]=$m.":".$l;
- //查询属性中是否含有属性名称,如果不含有则添加,含有则不添加
- $ourphp_rs = $db -> select("*","`ourphp_productattribute`","where `OP_Title` = '".$m."'");
- if(empty($ourphp_rs)){
- $query = $db -> insert("`ourphp_productattribute`","`OP_Title` = '".admin_sql($m)."',`OP_Class` = '1',`OP_Text` = '',`OP_Sorting` = '".admin_sql($jj)."',`time` = '".date("Y-m-d H:i:s")."'","");
- }
- }
- $o = implode('|',$n);
-
- $ourphp_rs = $db -> select("*","`ourphp_column`","where `OP_Uid` = '4' and `OP_Columntitle` = '".$d."'");
- if(empty($ourphp_rs)){
- $query = $db -> insert("`ourphp_column`","`OP_Uid` = '4',`OP_Lang` = 'cn',`OP_Columntitle` = '".admin_sql($d)."',`OP_Columntitleto` = '',`OP_Model` = 'product',`OP_Templist` = 'cn_product.html',`OP_Tempview` = 'cn_productview.html',`OP_Url` = '',`OP_About` = '',`OP_Hide` = '0',`OP_Sorting` = '0',`OP_Briefing` = '',`OP_Img` = '',`OP_Userright` = '0',`OP_Weight` = '0',`OP_Adminopen` = '1,0,1'","");
- $pcateid=msyql_insert_id();
- $query = $db -> insert("`ourphp_column`","`OP_Uid` = '".$pcateid."',`OP_Lang` = 'cn',`OP_Columntitle` = '".admin_sql($e)."',`OP_Columntitleto` = '',`OP_Model` = 'product',`OP_Templist` = 'cn_product.html',`OP_Tempview` = 'cn_productview.html',`OP_Url` = '',`OP_About` = '',`OP_Hide` = '0',`OP_Sorting` = '0',`OP_Briefing` = '',`OP_Img` = '',`OP_Userright` = '0',`OP_Weight` = '0',`OP_Adminopen` = '1,0,1'","");
- $ccateid=msyql_insert_id();
- }else{
- $pcateid=$ourphp_rs['id'];
- $ourphp_rs2 = $db -> select("*","`ourphp_column`","where `OP_Uid` = '".$pcateid."' and `OP_Columntitle` = '".$e."'");
- if(empty($ourphp_rs)){
- $query = $db -> insert("`ourphp_column`","`OP_Uid` = '".$pcateid."',`OP_Lang` = 'cn',`OP_Columntitle` = '".admin_sql($e)."',`OP_Columntitleto` = '',`OP_Model` = 'product',`OP_Templist` = 'cn_product.html',`OP_Tempview` = 'cn_productview.html',`OP_Url` = '',`OP_About` = '',`OP_Hide` = '0',`OP_Sorting` = '0',`OP_Briefing` = '',`OP_Img` = '',`OP_Userright` = '0',`OP_Weight` = '0',`OP_Adminopen` = '1,0,1'","");
- $ccateid=msyql_insert_id();
- }else{
- $ccateid=$ourphp_rs2['id'];;
- }
-
- }
-
- //null 为主键id,自增可用null表示自动添加
- $query = $db -> insert("`ourphp_product`","`OP_Class` = '".$ccateid."',`OP_Lang` = 'cn',`OP_Title` = '".admin_sql($a)."',`OP_Number` = '".admin_sql($b)."',`OP_Goodsno` = '".admin_sql($c)."',`OP_Brand` = '',`OP_Market` = '".admin_sql($f)."',`OP_Webmarket` = '".admin_sql($f)."',`OP_Stock` = '".admin_sql($g)."',`OP_Usermoney` = '1:0.00|2:0.00|3:0.00|4:0.00|5:0.00',`OP_Specificationsid` = '',`OP_Specificationstitle` = '',`OP_Specifications` = '',`OP_Pattribute` = '".$o."',`OP_Minimg` = '".$j."',`OP_Maximg` = '".$k."',`OP_Img` = '',`OP_Content` = '".admin_sql($i)."',`OP_Down` = '2',`OP_Weight` = '1',`OP_Freight` = '1',`OP_Tag` = '',`OP_Sorting` = '99',`OP_Attribute` = '',`OP_Url` = '',`OP_Description` = '".compress_html($h)."',`time` = '".date("Y-m-d H:i:s")."',`OP_Integral` = '0.00',`OP_Integralok` = '0',`OP_Integralexchange` = '0.00',`OP_Suggest` = ''","");
-
- }
- }
-
- $ourphp_font = 5;
- $ourphp_img = 'ok.png';
- $ourphp_content = '操作成功!';
- $ourphp_class = 'ourphp_excel.php?id=ourphp';
- require 'ourphp_remind.php';
- }
- Admin_click('批量上传Excel','ourphp_excel.php?id=ourphp');
- $smarty->display('ourphp_excel.html');
- ?>
复制代码
|