程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> 關於PHP編程 >> php導入excel表格,導入excel表格

php導入excel表格,導入excel表格

編輯:關於PHP編程

php導入excel表格,導入excel表格


我們做網站的時候經常要用到excel導入和導出的功能,我們通常的做法是用phpexcel工具包來完成,具體方法如下:

html代碼:

<form action="{:U('Mall/updExcel')}" method="POST" enctype="multipart/form-data">    //提交form表單到Mall控制器下的upExcel方法中
              <div >   
                <div >
                    <input type='submit' value="更改價格" />
                </div>
                <div >
                    <input type='file' value="" name="import"/>
                </div>
                <input type="hidden" id="url" name="url" value=""/>      //添加一個隱藏域 傳遞url
             </div>

</form>

寫ExcelController工具類:此類用來被實例化

<?php
namespace Home\Controller;
use Think\Controller;
include "./Public/Plugin/PHPExcel.class.php";
include "./Public/Plugin/PHPExcel/Writer/Excel5";
include "./Public/Plugin/PHPExcel/IOFactory.php";

class ExcelController extends Controller {
    //導入功能
    public function updExcel($file){ 
        if(!file_exists($file)){ 
            return array("error"=>0,'message'=>'file not found!');
        }
        $objReader = \PHPExcel_IOFactory::createReader('Excel5');
        
        $objPHPExcel = $objReader->load($file,$encode='utf-8');
        
        $sheet = $objPHPExcel->getSheet(0);
        $highestRow = $sheet->getHighestRow(); // 取得總行數
        $highestColumn = $sheet->getHighestColumn(); // 取得總列數
        $j=0;
        for($i=2;$i<=$highestRow;$i++){
            
            $data[$j]['id']= $objPHPExcel->getActiveSheet()->getCell("A".$i)->getValue();
            
            $data[$j]['result_price']= $objPHPExcel->getActiveSheet()->getCell("B".$i)->getValue();
            $j++;
        }
        return $data;
    }

下面書寫控制器來上傳excel表格:此類方法不用把excel表格傳遞到服務器,直接寫入數據:

<?php
namespace Admin\Controller;
use Think\Controller;
use Think\Page;
use Home\Controller\IndexController;
use Common;
use Org\Util\Date;
use Home\Controller\ExcelController;

class MallController extends Controller {
            /*商品列表批量導入*/
public function updExcel(){
        $excel=new ExcelController();
        $goods=M('shop_goods_subinfo');
        
        if(isset($_FILES["import"]) && ($_FILES["import"]["error"] == 0)){
            $result = $excel->updExcel($_FILES["import"]["tmp_name"]);
            /*
             * 業務邏輯代碼
             * */
            $true="";
            $false="";
            foreach($result as $value){
                //$where=array('id'=>$value['id'],'status'=>0);
                $where=array(
                        'code'=>$value['id'],
                );
                $data=array(
                        'result_price'=>$value['result_price'],
                );
                $state=$goods->where($where)->save($data);
                if($state > 0){
                    $true .= ";".$value['id'];
                }else{
                    $false .= ";".$value['id'];
                }
                
            }
            echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />';
            echo "<script type='text/javascript'>";
            echo "alert('更改成功".$true.";更改失敗".$false."');window.location.href='".I('param.url')."';";
            echo "</script>";
            exit;
        }else{
            echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />';
            echo "<script type='text/javascript'>";
            echo "alert('文件讀取失敗');window.location.href='".I('param.url')."';";
            echo "</script>";
            exit;
        }
    }
}

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved