Phpexcel Importing Excel tables

Source: Internet
Author: User

HTML code

<form action= "{: U (' Mall/updexcel ')}" method= "POST" enctype= "Multipart/form-data" >// Submit Form form to the Upexcel method under the mall controller
<div style= "float:left;width:41%;" >
<div style= "Float:left;" >
<input type= ' Submit ' value= ' Change price style= ' margin:0px 0px 7px 10px;cursor:pointer;background-color: #C30D23; border:0 Px;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= ""/>//Add a hidden domain delivery URL
</div>

</form>

Write Excelcontroller Tool class: This class is used to instantiate

<?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 {
Import Features
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 (); Total number of rows obtained
$highestColumn = $sheet->gethighestcolumn (); Total number of columns obtained
$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;
}

The following writing controller to upload Excel table: This kind of method does not have to pass Excel table to the server, writes the data directly:

<?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 {
/* Product List Bulk Import */
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"]);
/*
* Business logic Code
* */
$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 (' Change succeeded". $true. "; The change failed ". $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 (' file read failed '); window.location.href= '". I (' Param.url '). "';";
echo "</script>";
Exit
}
}
}

Phpexcel Importing Excel tables

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.