PHP Login (Ajax submission data and background check) tutorial

Source: Internet
Author: User

Implementation steps:

One: In http://phpexcel.codeplex.com/download the latest phpexcel put under vendor, pay attention to position: thinkphp\extend\vendor\phpexcel\phpexcel.php.

Two: Export Excel code implementation

/** Method **/
Functionindex () {
$this->display ();
}
Publicfunctionexportexcel ($expTitle, $expCellName, $expTableData) {
$xlsTitle = Iconv (' utf-8 ', ' gb2312 ', $expTitle);//File name
$fileName =$_session[' account '].date (' _ymdhis ');//or $xlsTitle file name can be set according to your own situation
$cellNum =count ($expCellName);
$dataNum =count ($expTableData);
Vendor ("Phpexcel.phpexcel");
$objPHPExcel =newphpexcel ();
$cellName =array (' A ', ' B ', ' C ', ' D ', ' E ', ' F ', ' G ', ' H ', ' I ', ') ', ' J ', ' K ', ' L ', ' M ', ' N ', ' O ', ' P ', ' Q ', ' R ', ' S ', ' T ', ' U ', ' V ', ' W ', ' X ', ' Y ', ' Z ', ' AA ', ' AB ', ' AC ', ' AD ', ' AE ', ' AF ', ' AG ', ' AH ', ' AI ', ' AJ ', ' AK ', ' AL ', ' AM ', ' an ', ' AO ', ' AP ', ' AQ ', ' AR ', ' as ', ' at ' , ' AU ', ' AV ', ' AW ', ' AX ', ' AY ', ' AZ ';
$objPHPExcel->getactivesheet (0)->mergecells (' A1: '. $cellName [$cellNum-1]. " 1 ');//Merge cells
$objPHPExcel->setactivesheetindex (0)->setcellvalue (' A1 ', $expTitle. ') Export time: '. Date (' y-m-d h:i:s '));
for ($i =0; $i < $cellNum; $i + +) {
$objPHPExcel->setactivesheetindex (0)->setcellvalue ($cellName [$i]. " 2 ', $expCellName [$i][1]);
}
Miscellaneous Glyphs, UTF-8
for ($i =0; $i < $dataNum; $i + +) {
for ($j =0; $j < $cellNum; $j + +) {
$objPHPExcel->getactivesheet (0)->setcellvalue ($cellName [$j]. ( $i +3), $expTableData [$i] [$expCellName [$j][0]]);
}
}
Header (' Pragma:public ');
Header (' Content-type:application/vnd.ms-excel;charset=utf-8;name= '. $xlsTitle. ') XLS "');
Header ("Content-disposition:attachment;filename= $fileName. xls");//attachment new window print inline this window print
$objWriter = Phpexcel_iofactory::createwriter ($objPHPExcel, ' Excel5 ');
$objWriter->save (' php://output ');
Exit
}
/**
*
* Export Excel
*/
Functionexpuser () {//Export Excel
$xlsName = "User";
$xlsCell =array (
Array (' ID ', ' account sequence '),
Array (' Truename ', ' name '),
Array (' Sex ', ' gender '),
Array (' res_id ', ' faculties '),
Array (' sp_id ', ' professional '),
Array (' Class ', ' class '),
Array (' Year ', ' Graduation Time '),
Array (' City ', ' location '),
Array (' company ', ' unit '),
Array (' Zhicheng ', ' title '),
Array (' Zhiwu ', ' title '),
Array (' Jibie ', ' Level '),
Array (' tel ', ' telephone '),
Array (' QQ ', ' QQ '),
Array (' email ', ' mailbox '),
Array (' Honor ', ' honor '),
Array (' remark ', ' remarks ')
);
$xlsModel = M (' member ');
$xlsData = $xlsModel->field (' Id,truename,sex,res_id,sp_id,class,year,city,company,zhicheng,zhiwu,jibie,tel,qq , Email,honor,remark ')->select ();
foreach ($xlsDataas $k=> $v)
{
$xlsData [$k] [' Sex ']=] $v [' Sex ']==1? '] Male ': ' Female ';
}
$this->exportexcel ($xlsName, $xlsCell, $xlsData);
}

Third: Import Excel data code

Functionimpuser () {
if (!empty ($_files)) {
Import ("@.org.uploadfile");
$config =array (
' Allowexts ' =>array (' xlsx ', ' xls '),
' Savepath ' => './public/upload/',
' Saverule ' => ' time ',
);
$upload =newuploadfile ($config);
if (! $upload->upload ()) {
$this->error ($upload->geterrormsg ());
}else{
$info = $upload->getuploadfileinfo ();
}
Vendor ("Phpexcel.phpexcel");
$file _name= $info [0][' Savepath ']. $info [0][' Savename '];
$objReader = Phpexcel_iofactory::createreader (' Excel5 ');
$objPHPExcel = $objReader->load ($file _name, $encode = ' utf-8 ');
$sheet = $objPHPExcel->getsheet (0);
$highestRow = $sheet->gethighestrow ()///Total number of rows obtained
$highestColumn = $sheet->gethighestcolumn ()//Total number of columns
for ($i =3; $i <= $highestRow; $i + +)
{
$data [' Account ']= $data [' truename '] = $objPHPExcel->getactivesheet ()->getcell ("B". $i)->getvalue ();
$sex = $objPHPExcel->getactivesheet ()->getcell ("C". $i)->getvalue ();
$data [' res_id '] = $objPHPExcel->getactivesheet ()->getcell ("D". $i)->getvalue ();
$data [' class '] = $objPHPExcel->getactivesheet ()->getcell ("E". $i)->getvalue ();
$data [' year '] = $objPHPExcel->getactivesheet ()->getcell ("F". $i)->getvalue ();
$data [' City ']= $objPHPExcel->getactivesheet ()->getcell ("G". $i)->getvalue ();
$data [' Company ']= $objPHPExcel->getactivesheet ()->getcell ("H". $i)->getvalue ();
$data [' Zhicheng ']= $objPHPExcel->getactivesheet ()->getcell ("I". $i)->getvalue ();
$data [' Zhiwu ']= $objPHPExcel->getactivesheet ()->getcell ("J". $i)->getvalue ();
$data [' Jibie ']= $objPHPExcel->getactivesheet ()->getcell ("K". $i)->getvalue ();
$data [' Honor ']= $objPHPExcel->getactivesheet ()->getcell ("L". $i)->getvalue ();
$data [' Tel ']= $objPHPExcel->getactivesheet ()->getcell ("M". $i)->getvalue ();
$data [' QQ ']= $objPHPExcel->getactivesheet ()->getcell ("N". $i)->getvalue ();
$data [' email ']= $objPHPExcel->getactivesheet ()->getcell ("O". $i)->getvalue ();
$data [' Remark ']= $objPHPExcel->getactivesheet ()->getcell ("P". $i)->getvalue ();
$data [' sex ']= $sex = = ' Male '? 1:0;
$data [' res_id '] = 1;
$data [' Last_login_time ']=0;
$data [' Create_time ']= $data [' last_login_ip ']=$_server[' remote_addr '];
$data [' Login_count ']=0;
$data [' Join ']=0;
$data [' Avatar ']= ';
$data [' Password ']=md5 (' 123456 ');
M (' member ')->add ($data);
}
$this->success (' Import success! ');
}else
{
$this->error ("Please select the uploaded file");
}
}

Four, template code

  
    ;  
  
  <body>
  <p><ahref= "{: U (' Index/expuser ')}" > Export data and Generate Excel</a></p><br/>
div>     <formaction= "{: U (' Index/impuser ')}" method= "Post" enctype= "Multipart/form-data" >
      <inputtype= "file" name= "import"/>
      <inputtype= "hidden" name= "table" value= "tablename"/>
 & Nbsp;    <inputtype= "Submit" value= "import"/>
    </form >
  </body>
   

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.