PHP Import Excel Method share

Source: Internet
Author: User
Tags format explode insert sql first row

Before using Phpexcel to export data to an Excel file, this article describes how to use Phpexcel to import Excel files into a database.

Here is the phpexcel, please download this class library file before doing the following, official website:

Http://www.codeplex.com/PHPExcel

The following is the first time to say the import of the Excel file data format, screenshot as follows:

The following is the specific code that imports the data from the Excel file into the database:

<?php require_once "classes/phpexcel.php";
Require_once "classes/phpexcel/iofactory.php";

Require_once "classes/phpexcel/reader/excel5.php"; $objReader =phpexcel_iofactory::createreader ("Excel5");//use excel2007 for 2007 format $objPHPExcel = $objReader-> Load ($file _url);//$file _url the path $sheet = Excel file = $objPHPExcel->getsheet (0);//get first worksheet $highestRow = $sheet-> Gethighestrow () and/or obtaining the total number $highestColumn = $sheet->gethighestcolumn ();
	Gets the total number of columns//loops read an Excel file, reads one, inserts a for ($j =2; $j <= $highestRow; $j + +) {//reads data from the first row $str = ""; for ($k = "A"; $k <= $highestColumn $k + +) {//Read data from column A//This method is simple, but it is not appropriate to combine an array with "\ \", and then split \ To insert a field value into the database, measured in Excel, if a cell's value package Data containing \ \ Import will be empty $str. = $objPHPExcel->getactivesheet ()->getcell ("$k $j")->getvalue (). "
	\ \ \/Read cell}//explode: Function splits strings into arrays.
	$strs =explode ("\", $STR); $sql = "INSERT into". Tb_prefix. "
	Business ' (' username ', ' password ', ' company ', ' Prov ', ' address ', ' btime ', ' phone ', ' email ', ' name ') VALUES ("{$strs [0]}", ' {$strs [1]} ', ' {$strs [2]} ', ' {$stRS[3]} ', ' {$strs [4]} ', ' {$strs [5]} ', ' {$strs [6]} ', ' {$strs [7]} ', ' {$strs [8]} ']; $db->query ($sql);//Here is the Insert database operation} unlink ($file _url); Delete Excel files

In addition, for how to export Excel files using Phpexcel, please refer to this site article:

PHP Export Excel File Summary



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.