Example of writing Excel to MySQL database in PHP

Source: Internet
Author: User
Tags bulk insert dsn

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.