Phpexcel Import and Export

Source: Internet
Author: User
Tags chr ord

//Import     Public functionExcelimport () {$mimes=Array(            ' Application/vnd.ms-excel ', ' Application/vnd.openxmlformats-officedocument.spreadsheetml.sheet '        ); $exts=Array(            ' xls ', ' xlsx '        ); $upload=NewUpload (Array(            ' Mimes ' =$mimes, ' exts ' =$exts, ' rootpath ' = '/public/', ' savepath ' and ' abcexcel/'.Date(' Ym '). " /", ' subname ' =Array(' Date ', ' d '),        )); $info=$upload->upload ($_files); if(!$info) {//Upload error message            $error=$upload-GetError (); Echo"<script>alert (' {$error} '); Window.history.go ( -1) </script> "; }Else{//Upload Successful            foreach($info  as $item) {                $filePath[] = __root__. " /public/".$item[' Savepath '].$item[' Savename ']; }            $ImgStr=implode("|",$filePath); $data=$this->excel ('. ').$ImgStr); if(!$data){                $this->success (' Import failed! ', ' index '); }            $res= M (' Assets ')->data ($data)->addall ($data); if(!$res){                $this->success (' Import failed! ', ' index '); }            $this->success (' Import successful! ', ' index '); }    }     Public functionExcel$files){        //Import the Phpexcel class library because Phpexcel does not use a namespace, only inport importImport ("Common.Vendor.Excel.PHPExcel"); //Create a Phpexcel object, note that there is no less \        $PHPExcel=New\phpexcel (); $name=substr(STRRCHR($files, '. '), 1); if($name= = ' xls ') {            //if the Excel file suffix name is. xls, import this classImport ("Common.Vendor.Excel.PHPExcel.Reader.Excel5"); $PHPReader=New\phpexcel_reader_excel5 (); }        if($name= = ' xlsx ') {            //if the Excel file suffix is named. xlsx, import this classImport ("Common.Vendor.Excel.PHPExcel.Reader.Excel2007"); $PHPReader=New\phpexcel_reader_excel2007 (); }        //Loading Files        $PHPExcel=$PHPReader->load ($files); $currentSheet=$PHPExcel->getsheet (0); $allColumn=$currentSheet-Gethighestcolumn (); $allRow=$currentSheet-Gethighestrow (); //Looping through data         for($currentRow= 2;$currentRow<=$allRow;$currentRow++){            $arr[' a '] =$PHPExcel->getactivesheet ()->getcell (' A ').$currentRow),GetValue (); $arr[' B '] =$PHPExcel->getactivesheet ()->getcell (' B ').$currentRow),GetValue (); $arr[' type '] =$PHPExcel->getactivesheet ()->getcell (' C ').$currentRow),GetValue (); $arr[' name '] =$PHPExcel->getactivesheet ()->getcell (' D ').$currentRow),GetValue (); $arr[' model '] =$PHPExcel->getactivesheet ()->getcell (' E ').$currentRow),GetValue (); $arr[' time '] =gmdate("Y-m-d h:i:s", \phpexcel_shared_date::exceltophp ($PHPExcel->getactivesheet ()->getcell (' F ').$currentRow),GetValue ()));$arr[' created_time '] =Date(' Y-m-d h:i:s ', Time());; $data[] =$arr; }        return $data; }    //Export     Public functionExcelexport () {$ids= I (' Param.ids '); if($ids){            $where[' a.id '] = [' in ',$ids]; }                $data= M (' abc ')->where ($where),Select (); $title=Array(' ID ', ' category ', ' name ', ' model ', ' supplier ')); $this->dataexport ($data,$title, ' This is the table name '.Date("Y-m-d", Time())); }    /** * Data export * @param string $fileName file name * @param array $HEADARR header data (one-dimensional) * @param array $data List data (two-dimensional) * @return bool*/     Public functionDataExport ($data=Array(),$HEADARR=Array(),$fileName="") {Import (' Common.Vendor.Excel.PHPExcel '); Import (' Common.Vendor.Excel.PHPExcel.IOFactory '); Import (' Common.Vendor.Excel.PHPExcel.Writer.Excel5 '); Import (' Common.Vendor.Excel.PHPExcel.Writer.Excel2007 '); if(Empty($HEADARR) &&!Is_array($HEADARR) &&Empty($data) &&!Is_array($data)) {            return false; }        $objPHPExcel=New\phpexcel (); //set up the table header        $tem _key= "A"; foreach($HEADARR  as $v){            if(strlen($tem _key) > 1) {                $arr _key=Str_split($tem _key); $colum= ' '; foreach($arr _key  as $ke=$va) {                    $colum.=CHR(Ord($va)); }            } Else {                $key=Ord($tem _key); $colum=CHR($key); }            $objPHPExcel->setactivesheetindex (0)->setcellvalue ($colum.‘ 1 ',$v); $tem _key++; }        $objActSheet=$objPHPExcel-Getactivesheet (); $border _end= ' A1 ';//Border End Position Initialize//write content        $column= 2; foreach($data  as $key=$rows){//get a row of data            $tem _span= "A"; foreach($rows  as $keyName=$value){//write a row of data                if(strlen($tem _span) > 1) {                    $arr _span=Str_split($tem _span); $j= ' '; foreach($arr _span  as $ke=$va) {                        $j.=CHR(Ord($va)); }                } Else {                    $span=Ord($tem _span); $j=CHR($span); }                $objActSheet->setcellvalue ($j.$column,$value); $border _end=$j.$column; $tem _span++; }            $column++; }        $fileName=Iconv("Utf-8", "gb2312",$fileName); //Set the activity Order index to the first table        $objPHPExcel->setactivesheetindex (0); Ob_end_clean();//clear buffer to avoid garbled characters        Header(' Content-type:application/vnd.ms-excel '); Header("Content-disposition:attachment;filename=$fileName. xls "); Header(' Cache-control:max-age=0 '); $objWriter= \phpexcel_iofactory::createwriter ($objPHPExcel, ' Excel5 '); $objWriter->save (' php://output ');//files are downloaded through the browser        Exit; }

Phpexcel Import and Export

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.