ThinkPHP conflicts with PHPExcel _ PHP Tutorial

Source: Internet
Author: User
ThinkPHP conflicts with PHPExcel. I have known a class called PHPExcel (official website) that can be used to operate Excel. I have never had a chance to try it. today, I found it very powerful, after downloading the source code package, I have known a class called PHPExcel (official website) that can be used to operate Excel. I have never had a chance to try it. I found it very powerful today, the downloaded source code package contains detailed documentation, which allows you to perform almost all the functions that Excel can perform manually.
An example of reading an Excel file is as follows:

The code is as follows:


$ InputFileType = 'excel2007 ';
$ InputFileName = './public/files/import_user_template.xlsx ';
$ Sheetname = 'sheet1 ';
// Specify the Excel type and create a reader
$ ObjReader = PHPExcel_IOFactory: createReader ($ inputFileType );
// Set to read data only, excluding formulas and formats
$ ObjReader-> setReadDataOnly (true );
// Read only the specified sheet
$ ObjReader-> setLoadSheetsOnly ($ sheetname );
$ ObjPHPExcel = $ objReader-> load ($ inputFileName );
$ CurSheet = $ objPHPExcel-> getSheet (0 );
// Maximum column containing data
$ AllColumn = $ curSheet-> getHighestColumn ();
// The largest row containing data
$ AllRow = $ curSheet-> getHighestRow ();
For ($ currentRow = 1; $ currentRow <= $ allRow; $ currentRow ++ ){
For ($ currentCol = 'a'; $ currentCol <= $ allColumn; $ currentCol ++ ){
Echo $ curSheet-> getCell ($ currentCol. $ currentRow)-> getValue (). "\ t ";
}
Echo "\ r \ n ";
}


To be used in ThinkPHP, copy the Classes directory in the source code package to the vender directory of ThinkPHP, change the name to PHPExcel, and then call the Vendor method to load

The code is as follows:


Vendor ('phpexcel. PHPExcel ');


However, after reading the Excel file, you can call the M or D method to instantiate the Model class and the Model class cannot be found. after research, we found that the automatic loader system conflict should be resolved, use the spl_autoload_register function to re-register the autoloader class before calling the M or D method.

The code is as follows:


Spl_autoload_register (array ('think', 'autoload '));


Solution to PHPExcel call in ThinkPHP
When PHPExcel is called in ThinkPHP, the data can be fully read, but the next step D, M or template call will cause an error. (I don't know if I encountered this problem by myself ?)
After research, we finally found a solution. Share with you. Haha!
1. first download the PHPExcel package and place it under ThinkPHP/Vendor/(that is, the third-party class library directory of Think.
2. call a function.

The code is as follows:


Protected function Import_Execl ($ file ){
If (! File_exists ($ file )){
Return array ("error" => 1 );
}
Vendor ("PHPExcel. PHPExcel ");
$ PHPExcel = new PHPExcel ();
$ PHPReader = new PHPExcel_Reader_Excel2007 ();
If (! $ PHPReader-> canRead ($ file )){
$ PHPReader = new PHPExcel_Reader_Excel5 ();
If (! $ PHPReader-> canRead ($ file )){
Return array ("error" => 2 );
}
}
$ PHPExcel = $ PHPReader-> load ($ file );
$ SheetCount = $ PHPExcel-> getSheetCount ();
For ($ I = 0; $ I <$ SheetCount; $ I ++ ){
$ CurrentSheet = $ PHPExcel-> getSheet ($ I );
$ AllColumn = $ this-> ExcelChange ($ currentSheet-> getHighestColumn ());
$ AllRow = $ currentSheet-> getHighestRow ();
$ Array [$ I] ["Title"] = $ currentSheet-> getTitle ();
$ Array [$ I] ["Cols"] = $ allColumn;
$ Array [$ I] ["Rows"] = $ allRow;
$ Arr = array ();
For ($ currentRow = 1; $ currentRow <= $ allRow; $ currentRow ++ ){
$ Row = array ();
For ($ currentColumn = 0; $ currentColumn <$ allColumn; $ currentColumn ++ ){
$ Row [$ currentColumn] = $ currentSheet-> getCellByColumnAndRow ($ currentColumn, $ currentRow)-> getValue ();
}
$ Arr [$ currentRow] = $ row;
}
$ Array [$ I] ["Content"] = $ arr;
}
Spl_autoload_register (array ('think', 'autoload'); // required, otherwise ThinkPHP and PHPExcel will conflict
Unset ($ currentSheet );
Unset ($ PHPReader );
Unset ($ PHPExcel );
Unlink ($ file );
Return array ("error" => 0, "data" => $ array );
}
Protected function ExcelChange ($ str) {// function used with Execl batch import
$ Len = strlen ($ str)-1;
$ Num = 0;
For ($ I = $ len; $ I >=0; $ I --){
$ Num + = (ord ($ str [$ I])-64) * pow (26, $ len-$ I );
}
Return $ num;
}


3. call.

The code is as follows:


Public function import (){
If (isset ($ _ FILES ["import"]) & ($ _ FILES ["import"] ["error"] = 0 )){
$ Result = $ this-> Import_Execl ($ _ FILES ["import"] ["tmp_name"]);
If ($ this-> Execl_Error [$ result ["error"] = 0 ){
$ Execl_data = $ result ["data"] [0] ["Content"];
Unset ($ execl_data [1]);
$ Data = D ("Data ");
Foreach ($ execl_data as $ k => $ v ){
$ D ["serial_no"] = $ v [0];
$ D ["check_no"] = $ v [1];
$ D ["work_no"] = $ v [2];
$ D ["class_name"] = $ v [3];
$ D ["user_name"] = $ v [4];
$ D ["new_class"] = $ v [5];
$ D ["error_level"] = $ v [6];
$ Data-> data ($ d)-> add ();
}
$ This-> success ($ this-> Execl_Error [$ result ["error"]);
} Else {
$ This-> error ($ this-> Execl_Error [$ result ["error"]);
}
} Else {
$ This-> error ("file Upload failed ");
}
}


4. error data:

The code is as follows:


Protected $ Execl_Error = array ("data imported successfully", "File Not Found", "incorrect Execl file format ");

Ghost (official website) can be used to operate Excel and has never had a chance to try it. Today, the trial is incredibly powerful. the downloaded source code package contains details...

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.