<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>读取学生成绩</title> </head> <body> <?php require_once 'reader.php'; $mydb = mysql_connect('localhost', 'root', '1234') or die("Could not connect to database.");//连接数据库 mysql_query("set names 'utf8'");//输出中文 mysql_select_db("score_db", $mydb); error_reporting(E_ALL ^ E_NOTICE); for ($n=1; $n<=3; $n++) { $data = new Spreadsheet_Excel_Reader(); $data->setOutputEncoding('utf-8');//设置在页面中输出的编码方式,而不是utf8 //$fileName是指要导入到mysql中的excel文件 $fileName = 'Book' . $n . '.xls'; $data->read($fileName); $tb_name = 'student_tb' . $n; $sql = "CREATE TABLE " . $tb_name . "( id int , name varchar(20), yuWen int, shuXue int, yingYu int, wuLi int, huaXue int, shengWu int, zongFen int, duanMing int )"; mysql_query($sql, $mydb); for ($i = 2; $i <= $data->sheets[0]['numRows']; $i++) { $d1 = $i-1;$d2 = $data->sheets[0]['cells'][$i][3];$d3 = $data->sheets[0]['cells'][$i][4]; $d4 = $data->sheets[0]['cells'][$i][5];$d5 = $data->sheets[0]['cells'][$i][6];$d6 = $data->sheets[0]['cells'][$i][7]; $d7 = $data->sheets[0]['cells'][$i][8];$d8 = $data->sheets[0]['cells'][$i][9];$d9 = $data->sheets[0]['cells'][$i][10];$d10 = $data->sheets[0]['cells'][$i][11]; //echo($d1 . " " . $d2 . " " . $d3 . " " . $d4 . " " . $d5 . " " . $d6 . " " . $d7 . " " . $d8 . " " . $d9 . " " . $d10. "<hr />"); mysql_query("INSERT INTO $tb_name (id, name, yuWen, shuXue,yingYu,wuLi,huaXue,shengWu,zongFen,duanMing) VALUES ($d1, '$d2', '$d3', '$d4', '$d5', '$d6', '$d7', '$d8', '$d9', '$d10')"); } $result = mysql_query("SELECT * FROM $tb_name ORDER BY id"); echo "<hr />"; while($row = mysql_fetch_array($result)) { echo $row['id']; echo " " . $row['name']; echo " " . $row['yuWen']; echo " " . $row['shuXue']; echo " " . $row['yingYu']; echo " " . $row['wuLi']; echo " " . $row['huaXue']; echo " " . $row['shengWu']; echo " " . $row['zongFen']; echo " " . $row['duanMing']; echo "<hr />"; } echo "<hr />"; echo "成功" . $n . "<br/>"; echo "<hr />"; } mysql_close($mydb); ?> </body> </html>
时间: 2024-11-08 05:10:22