PHP操作Excel,一般用到 PhpOffice/PhpExcel 传送门: https://github.com/PHPOffice/PHPExcel
引用:
require_once "/PhpOffice/PhpExcel/PHPExcel.php";
引用以后就可以开始了
public static function importExcel($file_path){ $import_array = array(); //获取文件读取操作 $objReader = PhpOffice_PhpExcel_PHPExcel_IOFactory::load($file_path); //对象 $sheets = $objReader->getAllSheets(); $sheet = $sheets[0]; $allRow = $sheet->getHighestRow(); // 取得总行数 $allColumn = $sheet->getHighestColumn(); //取得总列数 //每行的数据 $val = array(); //第一行代变列头,这里做key值 $header = array(); for($currentColumn='A';$currentColumn<=$allColumn;$currentColumn++){ $header_address = $currentColumn."1"; $header_cell = $sheet->getCell($header_address); $header_cell = $header_cell->getValue(); $header[$currentColumn] = Ap_Util_String::htmlspecialchars($header_cell); //处理了一下字符串 } for($currentRow=2;$currentRow<=$allRow;$currentRow++){ for($currentColumn='A';$currentColumn<=$allColumn;$currentColumn++){ $address=$currentColumn.$currentRow;//数据坐标:A1,B1.... $cell =$sheet->getCell($address); $cell = $cell->getValue(); $val[$header[$currentColumn]] = Ap_Util_String::htmlspecialchars($cell); //处理了一下字符串 } $import_array[] = $val; } array_filter($import_array); return $import_array; }
表格数据:
导入的数据: