HTML code
< HTMLlang= "en"> < Head> < Metacharset= "UTF-8"> < MetaName= "Generator"Content= "EditPlus?"> < MetaName= "Author"Content= ""> < MetaName= "Keywords"Content= ""> < MetaName= "Description"Content= ""> < title>document Title> Head> < Body> < Center> < BR>< BR>< BR> < BR>< BR>< BR> < formMethod= "POST"action= "a.php"Enctype= "Multipart/form-data"> < inputType= "button"Value= "Download"onclick= " Download() "> < inputType= ' button 'Value= "plus one."onclick= " Jia() ">< BR>< BRId= "J"> Upload:< inputType= "File"Name= ' file 'Id= ' file '>< BR>< BR> < inputType= ' Submit 'Value= "OK"> Form> Center> Body> < ScriptSrc= "./jq.js"> Script>< Script> var I = 1; function Jia(){ $( "#j"). After ( ' upload:
'); I ++; } function Download(){ Location. href = ' b.php '; } Script> Html>
PHP code upload
Header ("Content-type:text/html;charset=utf-8");//Set encoding$DSN ="Mysql:dbname=book;host=localhost";$db =newPDO ($dsn,' Root ',' Root ',Array(PDO::Mysql_attr_init_command=' SET NAMES\'UTF8\''));//Connect the database, set the encoding format of the data for the delivery database to UTF8//introduction of Excel Classinclude'./phpexcel.php '; //reference class fileinclude'./phpexcel/iofactory.php ';//referencing the appropriate class file/*echo $_files["file" ["Name"]. "
";echo $_files["file" ["type"]. "
";Echo ($_files["File" ["Size"]/1024). The Kb
";echo $_files["File" ["Tmp_name"]. "
";*/foreach($_files as $key=$val){$str =substr ($val[' name '],strrpos ($val[' name '],'.')+1);if($val["Error"] >0) {Echo $val[' name ']."Upload error occurred". "
";Continue; }if($str! =' xls '){Echo' must be an xls file '. "
";Continue; }if(File_exists (Iconv ("UTF-8","GB2312",'./xls/'. $val["Name"]))) {Echo $val[' name '] . "already exists". "
";Continue; }Else{Move_uploaded_file ($val["Tmp_name"],iconv ("UTF-8","GB2312",'./xls/'. $val["Name"]));Echo $val["Name"],' above success! '; }$xlsPath =Iconv ("UTF-8","GB2312",'./xls/'. $val["Name"]); //Specify the Exls path to read//$type = ' Excel2007 ';//Set the type of Excel to resolve Excel5 (2003 or later) or Excel2007$type =' Excel5 ';$xlsReader =phpexcel_iofactory::Createreader ($type);$Sheets = $xlsReaderLoad ($xlsPath);//Start reading$Sheet = $SheetsGetsheet (0) -ToArray ();//Read First worksheet (note number starting from 0) If reading multiple can do a loop 0,1,2,3 ....//Get a two-dimensional array, each of which is a row of Excel table content containing data for each column of this rowEcho'
'; //print_r ($Sheet);d ie; foreach($Sheet as $k= =$v) { $res = $dbexec ("INSERT into a (data) values ('". $v[ 1 ]. "')"); //write to Database if($res) {//print_r ($Sheet);//After successful execution, output data }}}?>
PHP code Download
Header ("Content-type:text/html;charset=utf-8");//Set encodingfunctionExportexcel ($filename,$content){Header ("Cache-control:must-revalidate, post-check=0, pre-check=0");Header ("Content-type:application/vnd.ms-execl");Header ("Content-type:application/force-download");Header ("Content-type:application/download");Header ("content-disposition:attachment; Filename= ". $filename);Header ("Content-transfer-encoding:binary");Header ("Pragma:no-cache");Header ("expires:0");Echo $content;}$DSN ="Mysql:dbname=book;host=localhost";$db =newPDO ($dsn,' Root ',' Root ',Array(PDO::Mysql_attr_init_command=' SET NAMES\'UTF8\''));//Instantiate PDO object$res = $dbQuery ("SELECT * from a");//Querying data from a database$str =the ID\ tData\t\n";$str =Iconv (' Utf-8 ',' gb2312 ',$str);//iconv () is a coded conversion function$row = $resFetchall ();foreach($row as $key=$val){$id =Iconv (' Utf-8 ',' gb2312 ',$val[' id ']);$data =Iconv (' Utf-8 ',' gb2312 ',$val[' Data ']);$str. = $id."\ t". $data."\t\n";}$filename =Date (' YMD ').'. xls ';Exportexcel ($filename,$str); //Generate Excel Table
The above describes the Excel upload and download, including the aspects of the content, I hope that the PHP tutorial interested in a friend helpful.