phpexcel 1.8 在thinkphp下的应用1

来源:互联网 发布:阿里云邮箱网盘升级 编辑:程序博客网 时间:2024/06/07 22:02

<span style="white-space:pre"> </span>在网上查询了一下,发现phpexcel并没有相关正式或全面的,由浅入深的介绍,所以小白只能自己边查边探索了。今天先实现一下phpexcel最基本最简单的功能,希望日后可以慢慢补齐。


<?phpnamespace app\index\controller;use think\Controller;class index extends Controller {    public function _initialize() {        $objPHPExcel  = new \PHPExcel();//这里要注意‘\’ 要有这个。因为版本是3.1.2了。        $objWriter    = new \PHPExcel_Writer_Excel5($objPHPExcel);//设置保存版本格式        $objLoad    = new \PHPExcel_Reader_Excel5();//设置保存版本格式        $this->excel  = $objPHPExcel;        $this->writer = $objWriter;        $this->Load = $objLoad;    }    public function Index() {        return $this->fetch();    }<span style="white-space:pre"></span>//这里是读取数据库,存成execl。    public function save() {        $objPHPExcel = $this->excel;        $objWriter   = $this->writer;        //接下来就是写数据到表格里面去        $list = db('user')->select();        $objPHPExcel->getActiveSheet()->setCellValue('A' . '1', 'userid');//这里是设置A1单元格的内容        $objPHPExcel->getActiveSheet()->setCellValue('B' . '1', 'username');////这里是设置B1单元格的内容        $objPHPExcel->getActiveSheet()->setCellValue('C' . '1', 'userpass');////这里是设置B1单元格的内容        $objPHPExcel->getActiveSheet()->setCellValue('D' . '1', 'st');////这里是设置B1单元格的内容        foreach ($list as $key => $value) {            $i = $key + 2;//表格是从1开始的            $objPHPExcel->getActiveSheet()->setCellValue('A' . $i, $value['userid']);//这里是设置A1单元格的内容            $objPHPExcel->getActiveSheet()->setCellValue('B' . $i, $value['username']);////这里是设置B1单元格的内容            $objPHPExcel->getActiveSheet()->setCellValue('C' . $i, $value['userpass']);////这里是设置B1单元格的内容            $objPHPExcel->getActiveSheet()->setCellValue('D' . $i, $value['st']);////这里是设置B1单元格的内容            //以此类推,可以设置C D E F G看你需要了。        }        //接下来当然是下载这个表格了,在浏览器输出就好了        header("Pragma: public");        header("Expires: 0");        header("Cache-Control:must-revalidate, post-check=0, pre-check=0");        header("Content-Type:application/force-download");        header("Content-Type:application/vnd.ms-execl");        header("Content-Type:application/octet-stream");        header("Content-Type:application/download");;        header('Content-Disposition:attachment;filename=文件名称.xls');        header("Content-Transfer-Encoding:binary");        $objWriter->save('文件名称.xls');        $objWriter->save('php://output');    }<span style="white-space:pre"></span>//这里是读取excel表格到数组。需要注意的点在于,需要 先将表格载入,使用phpexcel里的Reader功能。然后再进行读取工作。表格的工作表要指定。    public function toarray() {        $file=$_FILES;        $file=$file['file']['tmp_name'];        $res =$this->push($file);            dump($res);exit;    }    public function push($data,$name='Excel'){        $objPHPExcel=$this->excel;        $objLoad=$this->Load;        $data=$objLoad->load($data);        $currentSheet = $data->getSheet(0);       //**读取excel文件中的指定工作表*/        $allColumn = $currentSheet->getHighestColumn();        //**取得最大的列号*/        $allRow = $currentSheet->getHighestRow();        //**取得一共有多少行*/        $data = array();        for($rowIndex=1;$rowIndex<=$allRow;$rowIndex++){        //循环读取每个单元格的内容。注意行从1开始,列从A开始            for($colIndex='A';$colIndex<=$allColumn;$colIndex++){                $addr = $colIndex.$rowIndex;                $cell = $currentSheet->getCell($addr)->getValue();                if($cell instanceof PHPExcel_RichText){ //富文本转换字符串                    $cell = $cell->__toString();                }                $data[$rowIndex][$colIndex] = $cell;            }        }        return $data;    }}


0 0