Phpexcel import mysql

Source: Internet
Author: User
To download phpexcelreader, and then read and display the excel data, import the excel data to mysql and import the csv data to the mysql database using php. the PHP instance code is as follows: req... to download phpexcelreader, and then read and display the excel data, import the excel data to mysql and import the csv data to the mysql database using php.

The PHP instance code is as follows:

Require_once '. /shortdes/reader. php '; // excelfile ($ filename, $ encoding); $ data = new spreadsheet_excel_reader (); // set output encoding. $ data-> setoutputencoding ('gbk'); // "data.xls" indicates the excel file to be imported to mysql ($ data-> read('date.xls '); @ $ db = mysql_connect ('localhost', 'root', '000000') or die ("cocould not connect to database. "); // connect to the database mysql_query (" set names 'gbk' "); // output the Chinese mysql_select_db ('wenbaiedu'); // select the 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 ";// http://sourceforge.net/projects/phpexcelreader // The following is the import program, indicating that the mysql database structure should be the same as the xls Document Structure $ data = new spreadsheet_excel_reader (); $ data-> setoutputencoding ('utf-8'); // Set the 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) v Alues ('$ _ 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); // end of database insertion} // import csv data to 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') "; $ f Irst = 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, '20140901', ',') {insert_data ($ data [0], $ data [1], $ data [2], $ data [3], $ data [4]); echo "data imported successfully!

";}Fclose ($ fp); $ time_end = getmicrotime (); // open source code phprm.com $ time = $ time_end-$ time_start; echo" program execution time :". $ time. "Seconds ";


Link to this article:

Add to favorites ^ please keep the tutorial address.

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.