LoadExcel.php PHP 利用 PHPexcel 导入数据库
PHP 利用 PHPexcel 导入数据库
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title> LoadExcel.php </title> <meta charset="UTF-8"> <meta name="Author" content=""> <meta name="Keywords" content=""> <meta name="Description" content=""> </head> <body> <?php error_reporting(E_ALL); //开启错误 set_time_limit(0); //脚本不超时 date_default_timezone_set('Asia/Shanghai'); //设置时间 $inc_path=get_include_path(); var_dump($inc_path); /** Include path **/ set_include_path($inc_path . PATH_SEPARATOR . dirname(dirname(__FILE__)) . '/Classes/');//设置环境变量 var_dump(get_include_path()); /** PHPExcel_IOFactory */ include 'PHPExcel/IOFactory.php'; //$inputFileType = 'Excel5'; //这个是读 xls的 $inputFileType = 'Excel2007';//这个是计xlsx的 //$inputFileName = './sampleData/example2.xls'; $inputFileName = './sampleData/book.xlsx'; echo 'Loading file ',pathinfo($inputFileName,PATHINFO_BASENAME),' using IOFactory with a defined reader type of ',$inputFileType,'<br />'; $objReader = PHPExcel_IOFactory::createReader($inputFileType); //var_dump($objReader); $objPHPExcel = $objReader->load($inputFileName); //var_dump($objPHPExcel); //$objWorksheet = $objPHPExcel->getActiveSheet();//取得总行数 //$highestRow = $objWorksheet->getHighestRow();//取得总列数 $objWorksheet = $objPHPExcel->getSheet(0); $highestRow = $objWorksheet->getHighestRow(); //取得总行数 $highestColumn = $objWorksheet->getHighestColumn(); //取得总列 //var_dump($objWorksheet); //var_dump($highestRow); //var_dump($highestColumn); echo 'highestRow='.$highestRow; echo "<br>"; $highestColumnCount = PHPExcel_Cell::columnIndexFromString($highestColumn);//总列数 echo "<h1>highestColumn=$highestColumn ; highestColumnCount=$highestColumnCount </h1>"; echo '<hr>'; $headtitle=array(); for ($row = 1;$row <= $highestRow;$row++) { $strs=array(); //注意highestColumnIndex的列数索引从0开始 for ($col = 0;$col < $highestColumnCount;$col++) { $strs[$col] =$objWorksheet->getCellByColumnAndRow($col, $row)->getValue(); } var_dump($strs); $info = array( 'word1'=>"$strs[0]", 'word2'=>"$strs[1]", 'word3'=>"$strs[2]", 'word4'=>"$strs[3]", ); //在这儿,你可以连接,你的数据库,写入数据库了 print_r($info); echo '<br />'; } ?> </body> </html>

 
 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号