I have known for a long time that there is a class called PHPExcel (official website) that can be used to operate Excel, but I have never had a chance to try it. I tried it today and found that it is extremely powerful. The downloaded source code package has detailed documentation, and it can almost achieve manual operation of Excel. all functions.
A simple example of reading Excel is as follows:
Copy code 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 only read data, excluding formulas and formats
$objReader->setReadDataOnly(true);
//Only read the specified sheet
$objReader->setLoadSheetsOnly($sheetname);
$objPHPExcel = $objReader->load($inputFileName);
$curSheet = $objPHPExcel->getSheet(0);
//Contains The largest column of 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 "rn";
}
To use it in ThinkPHP, put Copy the Classes directory in the source code package to the Vendor directory of ThinkPHP, rename it to PHPExcel, and then call the Vendor method to load
Copy the code The code is as follows:
vendor('PHPExcel.PHPExcel');
But after reading Excel and calling the M or D method to instantiate the model class, the Model class cannot be found. Error. After research, it was found that it is a conflict with the automatic loading mechanism. To resolve the conflict, you need to use the spl_autoload_register function to re-register the autoloader class before calling the M or D method
Copy code The code is as follows:
spl_autoload_register(array('Think','autoload'));
Solution to the problem of calling PHPExcel in ThinkPHP
When calling PHPExcel in ThinkPHP, the data can be read completely, but an error will occur when D, M or calling the template in the next step. (I wonder if I am the only one who encounters this problem?)
After research, I finally found a solution. Share it with everyone. hehe!
1. First download the PHPExcel package and place it under ThinkPHP/Vendor/ (that is, Think’s third-party library directory).
2. Call the function.
Copy code 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'));//Must, 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 to cooperate 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.
Copy code 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("上传文件失败");
}
}
4,错误数据:
复制代码 代码如下:
protected $Execl_Error = array("数据导入成功","找不到文件","Execl文件格式不正确");
http://www.bkjia.com/PHPjc/324208.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/324208.htmlTechArticle很早之前就知道有一个叫做PHPExcel的类(官方网站)可以用来操作Excel,一直没有机会尝试,今天试用发现无比强大,下载后的源码包里有详...