Add a file Form
<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "HTTP://WWW.W3.ORG/TR/HTML4/LOOSE.DTD" >
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 ">
<title> Add Excel Inbound </title>
<body>
<form action= "insert.php" enctype= "Multipart/form-data" method= "POST" >
<input type= "file" value= "" name= "File" >
<input type= "Submit" value= "Submission" >
</form>
</body>
PHP code
<?php
Header ("Content-type:text/html;charset=utf-8");
Import XLS processing flow: Select xls file, upload xls file to server, and resolve excel-> batch storage by Php-excelreader.
Include_once ("./reader.php"); Introduction of Php-excelreader
$tmp = $_files[' file ' [' Tmp_name '];
if (empty ($tmp)) {
Echo ' Please select the Excel file you want to import! ‘;
Exit
}
$DSN = "Mysql:host=localhost;dbname=test";
$db = new PDO ($dsn, ' root ', ' 123 ');
$count = $db->exec ("INSERT into XLS (ID,NAME,CONTENT,DECS) VALUES ()");
$save _path = "xls/";
$file _name = $save _path.date (' Ymdhis '). ". xls"; File save path and name after upload
if (copy ($tmp, $file _name)) {
$xls = new Spreadsheet_excel_reader ();
$xls->setoutputencoding (' utf-8 '); Set encoding
$xls->read ($file _name); Parsing files
echo $xls->sheets[0][' numrows ');d ie;
/*
for ($i =2; $i <= $xls->sheets[0][' numrows '); $i + +) {
$name = $xls->sheets[0][' cells ' [$i][0];
$sex = $xls->sheets[0][' cells ' [$i][1];
$age = $xls->sheets[0][' cells ' [$i][2];
$data _values. = "(' $name ', ' $sex ', ' $age '),";
}
*/
for ($i =1; $i <= $xls->sheets[0][' numrows '); $i + +) {
$id = $xls->sheets[0][' cells ' [$i][1];
$name = $xls->sheets[0][' cells ' [$i][2];
$content = $xls->sheets[0][' cells ' [$i][3];
$decs = $xls->sheets[0][' cells ' [$i][4];
@ $data _values. = "($id, ' $name ', ' $content ', ' $decs '),";
}
$data _values = substr ($data _values,0,-1); Remove the last comma
$query = $db->exec ("INSERT into XLS (XID,NAME,CONTENT,DECS) values $data _values");//BULK INSERT in Datasheet
if ($query) {
Echo ' Import succeeded! ‘;
}else{
echo ' Import failed! ‘;
}
}
?>
Example of writing Excel to MySQL database in PHP