PHP Excel Import mysql_php Tutorial

Source: Internet
Author: User
Tags mysql tutorial php excel
PHP Excel import mysql first to download Phpexcelreader, and then look at a simple to read the Excel data and show that the example is to import Excel data into MySQL data, example three is the use of PHP import CSV to MySQL database.

PHP tutorial Excel import mysql Tutorial
First of all to download Phpexcelreader, and then look at a simple to read the Excel data and show that the example is to import Excel data into MySQL data, example three is the use of PHP import CSV to MySQL database tutorial.
*/

Require_once './includes/reader.php ';
Excelfile ($filename, $encoding);
$data = new Spreadsheet_excel_reader ();
Set output encoding.
$data->setoutputencoding (' GBK ');
"Data.xls" refers to an Excel file to import into MySQL ()
$data->read (' Date.xls ');
@ $db = mysql_connect (' localhost ', ' root ', ' 1234 ') or
Die ("Could not connect to database."); /Connection Database
mysql_query ("Set names ' GBK '");//Output Chinese
mysql_select_db (' wenhuaedu '); Select Database
Error_reporting (e_all ^ e_notice);
for ($i = 1; $i <= $data->sheets[0][' numrows '); $i + +) {
The following comments (for loop) print Excel table data

for ($j = 1; $j <= $data->sheets[0][' numcols '); $j + +) {
echo $data->sheets[0][' cells ' [$i] [$j]. ",";
}
echo "n";

Ttp://sourceforge.net/projects/phpexcelreader


The following is an importer, noting that the MySQL database structure to be the same as the structure of the XLS document.

$data = new Spreadsheet_excel_reader ();
$data->setoutputencoding (' utf-8 ');//Set Encoding
$data->read (' Test.xls ');//excel,csv file location
Error_ Reporting (e_all ^ e_notice);

for ($i = 2; $i <= $data->sheets[0][' numrows '); $i + +) {//Insert database start
$sql = "INSERT INTO {$table}kehu (userid,sortid,danwei,xingming,zuncheng,tel,mobile,fax,email,website,qq,address, Postcode,sex,beizhu,addtime) VALUES (' $_cookie[wecms_user_id] ', ' $sortid ',
'. $data->sheets[0][' cells ' [$i][1]. "',
'. $data->sheets[0][' cells ' [$i][2]. "',
'. $data->sheets[0][' cells ' [$i][3]. "',
'. $data->sheets[0][' cells ' [$i][4]. "',
'. $data->sheets[0][' cells ' [$i][5]. "',
'. $data->sheets[0][' cells ' [$i][6]. "',
'. $data->sheets[0][' cells ' [$i][7]. "',
'. $data->sheets[0][' cells ' [$i][8]. "',
'. $data->sheets[0][' cells ' [$i][9]. "',
'. $data->sheets[0][' cells ' [$i][10]. "',
'. $data->sheets[0][' cells ' [$i][11]. "',
'. $data->sheets[0][' cells ' [$i][12]. "',
'. $data->sheets[0][' cells ' [$i][13]. "',
' $nowtime ') ";
$res = $db->query ($sql);

Insert Database End

}

Import the CSV into the database.

function Getmicrotime () {

List ($usec, $sec) = Explode ("", Microtime ());

return (float) $usec + (float) $sec);
}

$time _start = Getmicrotime ();

Include ("connectdb.php");


function Insert_data ($id, $summary, $description, $additional _information, $category)

{

$my _query1 = "INSERT into mantis_bug_text_table (id,description,additional_information)

VALUES (' $id ', ' $description ', ' $additional _information ');

$first = mysql_query ($my _query1);

$my _query2 = "INSERT into mantis_bug_table (id,project_id,summary,bug_text_id) VALUES (' $id ', ' $category ', ' $summary ', ' $id ') ";

$second = mysql_query ($my _query2);

Return
}

$fp = fopen ("Test.csv", "R");

while ($data = Fgetcsv ($fp, ' 1000 ', ', ')) {

Insert_data ($data [0], $data [1], $data [2], $data [3], $data [4]);

echo "Data import successful!

";
}
Fclose ($FP);

$time _end = Getmicrotime ();

$time = $time _end-$time _start;

echo "Program execution Time:". $time. " Seconds ";

http://www.bkjia.com/PHPjc/630814.html www.bkjia.com true http://www.bkjia.com/PHPjc/630814.html techarticle php Excel import mysql first to download Phpexcelreader, and then look at a simple to read the Excel data and show that the example is to import Excel data into MySQL data, example three is the use of ph ...

  • 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.