php通过PHPExcel读取excel中的内容到mysql数据库 1. [代码] [PHP]代码 ?phpheader("Content-Type:text/html;charset=utf-8");/** PHPExcel_IOFactory */require_once 'PHPExcel.php';if (!file_exists("mb.xlsx")) { exit("not found mb.xlsx
1. [代码][PHP]代码
<?php header("Content-Type:text/html;charset=utf-8"); /** PHPExcel_IOFactory */ require_once 'PHPExcel.php'; if (!file_exists("mb.xlsx")) { exit("not found mb.xlsx.\n"); } $reader = PHPExcel_IOFactory::createReader('Excel2007'); //设置以Excel5格式(Excel97-2003工作簿) $PHPExcel = $reader->load("mb.xlsx"); // 载入excel文件 $sheet = $PHPExcel->getSheet(0); // 读取第一個工作表 $highestRow = $sheet->getHighestRow(); // 取得总行数 $highestColumm = $sheet->getHighestColumn(); // 取得总列数 //1.导入配置文件 require("conf/dbconfig.php"); //2.连接MySQL、并选择数据库 $link = @mysql_connect(HOST, USER, PASS) or die("数据库连接失败!"); mysql_select_db(DBNAME, $link); //循环读取excel文件,读取一条,插入一条 for ($j = 2; $j <= $highestRow; $j++) { $name = $PHPExcel->getActiveSheet()->getCell("A" . $j)->getValue();//获取A列的值 $telephone = $PHPExcel->getActiveSheet()->getCell("B" . $j)->getValue();//获取B列的值 $fenzu = $PHPExcel->getActiveSheet()->getCell("C" . $j)->getValue();//获取C列的值 $school = $PHPExcel->getActiveSheet()->getCell("D" . $j)->getValue();//获取D列的值 $banji = $PHPExcel->getActiveSheet()->getCell("E" . $j)->getValue();//获取E列的值 $sql = "INSERT INTO `drtable`(`id`, `name`, `telephone`, `fenzu`, `school`, `banji`) VALUES (null,'{$name}','{$telephone}','{$fenzu}','{$school}','{$banji}')"; mysql_query($sql, $link); echo $sql; } ?>