Import and Export TP5.0 excel, tp5.0excel

Source: Internet
Author: User

Import and Export TP5.0 excel, tp5.0excel

Import the third-party phpexcel class Library to ThinkPHP \ Library \ Vendor \ demo, and create your own folder demo.

Put the Excel. class under ThinkPHP \ Library \ Org \ class, and create your own folder class.

Controller:

<? Phpnamespace Admin \ Controller; use Think \ Controller; class ExcelController extends Controller {public function excelList () {$ this-> display () ;}// import public function import () {if (! Empty ($ _ FILES ['file _ stu'] ['name']) {$ tmp_file = $ _ FILES ['file _ stu '] ['tmp _ name']; // temporary file name $ file_types = explode ('. ', $ _ FILES ['file _ stu'] ['name']); // split the file name $ file_type = $ file_types [count ($ file_types)-1]; // file type/* determine whether the file is an excel file */if ($ file_type = 'xls '| $ file_type = 'xlsx' | $ file_type = 'csv') {// applicable type/* upload service */$ Upload = new \ Think \ upload (); $ upload-> maxSize = 3145728; $ upload-> exts = arr Ay ('xls ', 'csv', 'xlsx'); $ upload-> rootPath = '. /Public '; $ upload-> savePath ='/Excel/'; $ upload-> saveName = date ('ymdhis'); $ info = $ upload-> upload (); if (! $ Info) {// upload error message $ this-> error ($ upload-> getError ());} else {// uploaded successfully // read the file $ filename = '. /Public '. $ info ['file _ stu'] ['savepath']. $ info ['file _ stu'] ['savename']; import ("Org. yufan. excelReader "); vendor ('phpexcel. PHPExcel '); $ reader = \ PHPExcel_IOFactory: createReader ('excel2007'); // set to Excel5 format (Excel97-2003 workbook) $ PHPExcel = $ reader-> load ($ filename); // load an excel file $ sheet = $ PHPExcel-> getSheet (0 );// Read the first worksheet $ highestRow = $ sheet-> getHighestRow (); // obtain the total number of rows var_dump ($ highestRow); $ highestColumm = $ sheet-> getHighestColumn (); // obtain the total number of columns/** read the data of each cell cyclically */$ data = array (); for ($ row = 2; $ row <= $ highestRow; $ row ++) {// The number of rows starts with 1st rows. if ($ column = 'A') {$ data ['name'] = $ sheet-> getCell ($ column. $ row)-> getValue ();} if ($ column = 'B') {$ data ['account'] = $ sheet-> getCell ($ column. $ row)-> getValue ();} if ($ column = 'C') {$ data ['Password'] = $ sheet-> getCell ($ column. $ row)-> getValue ();} M ('data')-> add ($ data);} $ this-> success ('database imported successfully ', U ('excel/show ');} else {// non-conforming business type $ this-> error (' is not an Excel file, please upload it again... ') ;}} else {$ this-> error (' (⊙ o ⊙ )~ Import without data transfer ');} // export public function export () {import ("ORG. yufan. excel "); $ list = M ('data')-> select (); if ($ list = null) {$ this-> error ('database information is blank... ',__ APP __. '/Admin/Excel/show');} else {$ row = array (); $ row [0] = array ('Platform name', 'account ', 'Password'); $ I = 1; foreach ($ list as $ v) {$ row [$ I] ['name'] = $ v ['name']; $ row [$ I] ['account'] = $ v ['account']; $ row [$ I] ['Password'] = $ v ['Password']; $ I ++ ;}$ xls = new \ Excel_XML ('utf-8', false, 'datalist'); $ xls-> addArray ($ row ); $ xls-> generateXML (date ('ymdhis ');} public function show () {$ m = M ('data '); $ data = $ m-> select (); $ this-> assign ('data', $ data); $ this-> display ();}}

View

ExcelList.html
<! DOCTYPE html>

View

Show.html <! DOCTYPE html> 

 

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.