PHP Example: Use PHPExcel to import Excel2003 documents and Excel2007 documents into the MySQL database.
If you want to use phpExcelReader to import Excel data into the mysql database, please click on this article to view it.
Use phpExcelReader to import Excel data into mysql database.
Below we introduce another method to import Excel to MySQL database.
1. Go to the official website http://phpexcel.codeplex.com/ to download the PHPExcel class library. I am currently using version 1.7.9. As shown in the picture:
2. Upload the code directly.
(1), conn.php file (no need to introduce this, you know it):
PHP CodeCopy content to clipboard
- $mysql=mysql_connect("localhost","root","root");
- mysql_select_db("test",$mysql);
- mysql_query("set names GBK");
(2), HTML page part: index.php file (form submission page):
XML/HTML CodeCopy content to clipboard
-
form name="form2" method="post" enctype="multipart/form-data" action="upload_excel.php">
input type="hidden" name="leadExcel" value="true">
-
table align="center" width="90% " border="0"> ;
-
tr>
td
- >
input
- type="file" name="inputExcel">input
type=- "submit" name="import" value="Import data">
td>
tr- >
table- >
form- >
(3), form processing handler part: upload_excel.php file:
PHP CodeCopy content to clipboard
- include("conn.php");
- include("function.php");
-
- if($_POST ['import']=="Import data"){
-
- $leadExcel=$_POST['leadExcel'];
-
- if($leadExcel == "true")
- {
-
-
- $filename = $HTTP_POST_FILES['inputExcel'] ['name'];
-
- $tmp_name = $_FILES ['inputExcel']['tmp_name'];
-
- $msg = uploadFile($filename,$tmp_name);
- echo $msg;
- }
- }
(4), function part: function.php file:
PHP CodeCopy content to clipboard
- function uploadFile($file,$filetempname)
- {
-
- $filePath = 'upFile/';
- $str = "";
-
- set_include_path('.'.PATH_SEPARATOR .'E:phpAppServwww91ctcStudyPHPExcelImportSQl2 PHPExcel' .PATH_SEPARATOR .get_include_path());
-
- require_once 'PHPExcel.php';
- require_once 'PHPExcelIOFactory.php';
-
- require_once 'PHPExcelReaderExcel2007.php';
-
- $filename=explode(".",$file);
- $time=date("y-m-d-H-i- s");
- $filename [0]=$time;
- $name=implode (".",$filename);
- $uploadfile=$filePath.$name;
-
-
-
- $result=move_uploaded_file($filetempname,$uploadfile);
- if($result)
- {
-
- $objReader = PHPExcel_IOFactory::createReader('Excel2007');
-
- $objPHPExcel = PHPExcel_IOFactory::load($uploadfile);
-
- $sheet = $objPHPExcel- >getSheet(0);
- $highestRow = $sheet- >getHighestRow();
- $highestColumn = $sheet- >getHighestColumn();
-
-
- for($j=2;$j$highestRow;$j)
-
- for($k='A';$k$highestColumn;$k )
-
$str .= iconv(- 'utf-8','gbk',$objPHPExcel->getActiveSheet()->getCell("$k $j")->getValue()).'\';//Read cell
- //explode: Function splits a string into an array.
- $strs =& nbsp;explode("\",$str);
-
//var_dump ($strs); -
//die(); -
$sql =&n bsp;"INSERT INTO z_test_importexcel(duty_date,name_am,name_pm) VALUES ('- ".$strs[0]."','".$ strs[1]."','".$strs[2]."')"; 🎜>
//echo $ sql;
mysql_query ("set names GBK");//This is the specified database character set, usually placed after connecting to the database
-
if(! mysql_query($sql)){
-
& nbsp; return false;
-
$str =&n bsp;"
"; -
- }
-
unlink ($uploadfile); //Delete the uploaded excel file
-
$msg = "Import successful!
"; -
- }else{
- $msg = "Import failed!";
}
- return $msg;
-
}
- Regarding this function, I refer to the blogger’s article at http://blog.csdn.net/grassroots20 11/article/details/8104604. However,
I think there is a problem with the blogger’s writing method- , at least, what I use
$objPHPExcel = $objReader->load ($uploadfile);
- This sentence will appear when running:
Perhaps the PHPExcel class library has been officially upgraded later, and the calling method needs to be corrected. The author has not studied the details.
3. Through the above steps, readers can prepare an xls and xlsx document respectively. The system operation effect is:
Attachment download: Complete DEMO download (with PHPExcel class library) Attachment download: Complete DEMO download (with PHPExcel class library)
http://www.bkjia.com/PHPjc/1048745.html
www.bkjia.com
true
http: //www.bkjia.com/PHPjc/1048745.html
TechArticle
PHP Example: Use PHPExcel to import Excel2003 documents and Excel2007 documents into the MySQL database. If you want to use phpExcelReader to import Excel data To the mysql database, please click on this article...
Statement:The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn