Import an excel file in php and an excel file

Source: Internet
Author: User

Import an excel file in php and an excel file

The excel import and export functions are often used for website creation. The common practice is to use the phpexcel toolkit. The specific method is as follows:

Html code:

<Form action = "{: U ('mall/updExcel ')} "method =" POST "enctype =" multipart/form-data "> // submit the 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: 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 =" "//> // Add a hidden domain to pass the url </div>

</Form>

Write ExcelController tool class: this class is used for instantiation

<? Phpnamespace 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 function 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 (); // get the total number of rows $ highestColumn = $ sheet-> getHighestColumn (); // obtain the total number of columns $ j = 0; for ($ I = 2; $ I <= $ highestRow; $ I ++) {$ data [$ j] ['id'] = $ objPHPExcel-> getActiveSheet ()-> getCell ("". $ I)-> getValue (); $ data [$ j] ['result _ pric'] = $ objPHPExcel-> getActiveSheet ()-> getCell ("B ". $ I)-> getValue (); $ j ++;} return $ data ;}

Write the following controller to upload an excel table: This method does not need to pass an excel table to the server and write data directly:

<? Phpnamespace Admin \ Controller; use Think \ Page; use Home \ Controller \ IndexController; use Common; use Org \ Util \ Date; use Home \ Controller \ ExcelController; class MallController extends Controller {/* batch import of product lists */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 ('changed successfully ". $ true. "; failed to change ". $ 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 ;}}}

 

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.