由于PHPExcel已经不再维护,PhpSpreadsheet是PHPExcel的下一个版本。PhpSpreadsheet是一个用纯PHP编写的库,并引入了命名空间,PSR规范等。这里简单介绍下PhpSpreadsheet库的表格读取数据功能。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | <?php $excel_file = './demo.xlsx' ; //表格文件 $ext = strtolower ( pathinfo ( $excel_file ,PATHINFO_EXTENSION)); if (!in_array( $ext , array ( 'xls' , 'xlsx' ))){ echo '表格格式不正确,请先转换为xls或xlsx格式' ; die ; } if (! file_exists ( $excel_file )){ echo '表格文件不存在!' ; die ; } require_once __DIR__ . '/vendor/autoload.php' ; $spreadsheet = \PhpOffice\PhpSpreadsheet\IOFactory::load( $excel_file ); $sheet = $spreadsheet ->getSheet(0); // 读取第一個工作表 $highestRow = $sheet ->getHighestRow(); // 取得总行数 $highestColumm = $sheet ->getHighestColumn(); // 取得总列数,类似 B、AA这种格式 $highestColumm_num =\PhpOffice\PhpSpreadsheet\Cell\Coordinate::columnIndexFromString( $highestColumm ); //将列数字符转换为对应的数字 $dataset = array (); //循环读取每个单元格的数据 for ( $row = 1; $row <= $highestRow ; $row ++){ //行数是以第1行开始 for ( $j =0; $j <= $highestColumm_num ; $j ++) { //列数是以A列开始 $curval =@trim( $sheet ->getCellByColumnAndRow( $j , $row )->getValue()); //$curval=$sheet->getCellByColumnAndRow($j,$row)->getCalculatedValue(); //获取公式计算之后的值 $dataset [ $row ][ $j ]= $curval ; } } var_dump( $dataset ); |
友情提示:垃圾评论一律封号...