Import execel [xls] data tables to mysql using php

Source: Internet
Author: User

<Style type = "text/css">
<! --
@ Import url ("../style/admin.css ");
. STYLE1 {
Color: # FF0000;
Font-weight: bold;
}
. STYLE2 {color: # 0033FF}
-->
</Style>
<Script>
Function import_check (){
Var f_content = form1.file. value;
Var fileext = f_content.substring (f_content.lastIndexOf ("."), f_content.length)
Fileext = fileext. toLowerCase ()
If (fileext! Using '.xls ')
    {
Alert ("Sorry, the imported data format must be xls files. Please adjust the format and upload it again. Thank you! ");
Return false;
    }
 }
</Script>

<Table width = "98%" border = "0" align = "center" style = "margin-top: 20px; border: 1px solid #9 abcde;">
<Form id = "form1" name = "form1" enctype = "multipart/form-data" method = "post" action = "">
 
<Tr>
<Td height = "28" colspan = "2" background = ".. /skins/top_bg.gif "> <label> & nbsp; <strong> <a href = "#"> Bonus Card points member data import </a> </strong> </label> </td>
</Tr>
<Tr>
<Td width = "18%" height = "50"> & nbsp; select the data table you want to import </td>
<Td width = "82%"> <label>
<Input name = "file" type = "file" id = "file" size = "50"/>
</Label>
<Label>
<Input name = "button" type = "submit" class = "nnt_submit" id = "button" value = "import data" onclick = "import_check ();"/>
</Label>
& Nbsp; </td>
</Tr>
<Tr>
<Td colspan = "2" bgcolor = "# DDF0FF"> & nbsp; [<span class = "STYLE1"> note </span>] data import format description: </td>
</Tr>
<Tr>
<Td colspan = "2"> 1. Others. the imported data table file must be in the <strong> execel </strong> File format {. <span class = "STYLE2"> xls </span>} is the extension. </td>
</Tr>
<Tr>
<Td colspan = "2"> & nbsp; 2. The data import sequence of the execel file must be as follows: member name | membership card number | ID card | product | registration time | expiration date | contact phone number as shown in the following figure: </td>
</Tr>
<Tr>
<Td colspan = "2"> & nbsp; </td>
</Tr> </form>
</Table>

<? Php
Error_reporting (E_ALL ^ E_NOTICE );
If ($ _ POST ){
$ Import_TmpFile = $ _ FILES ['file'] ['tmp _ name'];
Require_once '../inc/connect. Php ';
Require_once 'Excel/reader. Php ';
$ Data = new Spreadsheet_Excel_Reader ();
$ Data-> setOutputEncoding ('gb2312 ');
$ Data-> read ($ Import_TmpFile );
$ Array = array ();
 
For ($ I = 1; $ I <= $ data-> sheets [0] ['numrows ']; $ I ++ ){
For ($ j = 1; $ j <= $ data-> sheets [0] ['numcols']; $ j ++ ){
$ Array [$ I] [$ j] = $ data-> sheets [0] ['cells '] [$ I] [$ j];
  }
 }
Sava_data ($ array );

}
Function sava_data ($ array ){
$ Count = 0;
$ Total = 0;
Foreach ($ array as $ tmp ){
$ Isql = "Select inte_card from gx_integral where inte_card = '". $ tmp [2]. "'";
$ SQL = "Insert into gx_integral (inte_name, inte_card, inte_status, inte_integral, inte_date, inte_date2, inte_tel) value (";
$ SQL. = "'". $ tmp [1]. "','". $ tmp [2]. "','". $ tmp [3]. "','". $ tmp [4]. "','". ttodd ($ tmp [5]). "','". ttodd ($ tmp [6]). "','". $ tmp [7]. "')";
If (! Mysql_num_rows (mysql_query ($ Isql ))){
If (mysql_query ($ SQL )){
$ Count ++;
     }
    }
$ Total ++;
  }
Echo "<script> alert ('total". $ total. "data entries, import". $ count. "data entries succeeded '); </script> ";
  
 }
 
Function ttodd ($ text ){
$ Jd1900 = GregorianToJD (1, 1, 1900)-2;
$ MyJd = $ text + $ jd1900;
$ MyDate = JDToGregorian ($ myJd );
$ MyDate = explode ('/', $ myDate );
$ MyDateStr = str_pad ($ myDate [2], 4, '0', STR_PAD_LEFT ). "-". str_pad ($ myDate [0], 2, '0', STR_PAD_LEFT ). "-". str_pad ($ myDate [1], 2, '0', STR_PAD_LEFT );
Return $ myDateStr;
  }
?>

The Spreadsheet_Excel_Reader component is used here, so I will not attach the read. Php file. Let's get it next by myself. Finally, I declare that the original site is Reprinted. Please note: www.111cn.net
 

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.