phpexcel匯入excel表格

來源:互聯網
上載者:User

標籤:save   post   files   商品列表   turn   state   text   info   sts   

html代碼

<form action="{:U(‘Mall/updExcel‘)}" method="POST" enctype="multipart/form-data"> //提交form表單到Mall控制器下的upExcel方法中
<div style="float:left;width:41%;">
<div style="float:left;">
<input type=‘submit‘ value="更改價格" style="margin: 0px 0px 7px 10px;cursor: pointer;background-color:#C30D23;border:0px;color:#FFFFFF;width:90px;border-radius:5px;padding:3px 0;font-size:13px;"/>
</div>
<div style="float:left;width:45%">
<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;
}
}
}

 

phpexcel匯入excel表格

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.