旗下导航:搜·么
当前位置:网站首页 > PHP框架 > ThinkPHP > 正文

TP5援用PHPExcel完成导入导出功用【ThinkPHP教程】,PHPExcel

作者:搜搜PHP网发布时间:2019-12-26分类:ThinkPHP浏览:83


导读:从ki4网下载PHPExcelPHPExcel将下载好的PHPExcel文件夹跟PHPExcel安排在框架中的Vendor目次下,注重目次的构造,根据我的代码来基本上直...
从ki4网下载PHPExcel

PHPExcel

将下载好的PHPExcel文件夹跟PHPExcel安排在框架中的Vendor目次下,注重目次的构造,根据我的代码来基本上直接援用就能够运用

前端代码

<html>
 <head></head>
 <body>
  <div class="panel-heading">
    Advanced Tables 
   <a href="/daochu" class="btn-succes">导出</a> 
   <form action="/daoru" method="post" enctype="multipart/form-data"> 
    <input name="upload[]" type="file" /> 
    <input type="submit" /> 
   </form> 
  </div>
 </body>
</html>

导出功用控制器的代码

$list = Db::table('product')->select();
        vendor("PHPExcel176.PHPExcel");
        $objPHPExcel = new \PHPExcel();
        $objPHPExcel->getProperties()->setCreator("ctos")
            ->setLastModifiedBy("ctos")
            ->setTitle("Office 2007 XLSX Test Document")
            ->setSubject("Office 2007 XLSX Test Document")
            ->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.")
            ->setKeywords("office 2007 openxml php")
            ->setCategory("Test result file");
        $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(8);
        $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(20);
        $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(10);
        $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(10);
        $objPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth(50);
        //设置行高度
        $objPHPExcel->getActiveSheet()->getRowDimension('1')->setRowHeight(22);
        $objPHPExcel->getActiveSheet()->getRowDimension('2')->setRowHeight(20);
        //set font size bold
        $objPHPExcel->getActiveSheet()->getDefaultStyle()->getFont()->setSize(10);
        $objPHPExcel->getActiveSheet()->getStyle('A2:E2')->getFont()->setBold(true);
        $objPHPExcel->getActiveSheet()->getStyle('A2:E2')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
        $objPHPExcel->getActiveSheet()->getStyle('A2:E2')->getBorders()->getAllBorders()->setBorderStyle(\PHPExcel_Style_Border::BORDER_THIN);
        //设置程度居中
        $objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
        $objPHPExcel->getActiveSheet()->getStyle('A')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->getActiveSheet()->getStyle('B')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->getActiveSheet()->getStyle('D')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->getActiveSheet()->getStyle('E')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        //兼并cell
        $objPHPExcel->getActiveSheet()->mergeCells('A1:J1');
        // set table header content
        $objPHPExcel->setActiveSheetIndex(0)
            ->setCellValue('A1', '定单数据汇总  时候:'.date('Y-m-d H:i:s'))
            ->setCellValue('A2', '定单ID')
            ->setCellValue('B2', '商品名称')
            ->setCellValue('C2', '价钱')
            ->setCellValue('D2', '库存')
            ->setCellValue('E2', '图片');
        // Miscellaneous glyphs, UTF-8
        for($i=0;$i<count($list)-1;$i++){
            $objPHPExcel->getActiveSheet(0)->setCellValue('A'.($i+3), $list[$i]['id']);
            $objPHPExcel->getActiveSheet(0)->setCellValue('B'.($i+3), $list[$i]['name']);
            $objPHPExcel->getActiveSheet(0)->setCellValue('C'.($i+3), $list[$i]['price']);
            $objPHPExcel->getActiveSheet(0)->setCellValue('D'.($i+3), $list[$i]['stock']);
            $objPHPExcel->getActiveSheet(0)->setCellValue('E'.($i+3), $list[$i]['main_img_url']);
            //$objPHPExcel->getActiveSheet()->getStyle('A'.($i+3).':J'.($i+3))->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
            //$objPHPExcel->getActiveSheet()->getStyle('A'.($i+3).':J'.($i+3))->getBorders()->getAllBorders()->setBorderStyle(\PHPExcel_Style_Border::BORDER_THIN);
            $objPHPExcel->getActiveSheet()->getRowDimension($i+3)->setRowHeight(16);
        }
        //  sheet定名
        $objPHPExcel->getActiveSheet()->setTitle('定单汇总表');
        // Set active sheet index to the first sheet, so Excel opens this as the first sheet
        $objPHPExcel->setActiveSheetIndex(0);
        // excel头参数
        header('Content-Type: application/vnd.ms-excel');
        header('Content-Disposition: attachment;filename="商品表('.date('Ymd-His').').xls"');  //日期为文件名后缀
        header('Cache-Control: max-age=0');
        $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');  //excel5为xls花样,excel2007为xlsx花样
        $objWriter->save('php://output');

导入功用控制器的代码

public function daoru(){
        $file = $_FILES['upload']['tmp_name'][0];
        $data = $this->import_excel($file);
        var_dump($data);
    }
    private function import_excel($file){
        // 推断文件是什么花样
        $type = pathinfo($file);
        $type = strtolower($type["extension"]);
        $type=$type==='csv' ? $type : 'Excel5';
        ini_set('max_execution_time', '0');
        Vendor('PHPExcel176.PHPExcel');
        // 推断运用哪一种花样
        $objReader = \PHPExcel_IOFactory::createReader($type);
        $objPHPExcel = $objReader->load($file);
        $sheet = $objPHPExcel->getSheet(0);
        // 取得总行数
        $highestRow = $sheet->getHighestRow();
        // 取得总列数
        $highestColumn = $sheet->getHighestColumn();
        //轮回读取excel文件,读取一条,插进去一条
        $data=array();
        //从第一行入手下手读取数据
        for($j=3;$j<=$highestRow;$j++){
            //从A列读取数据
            for($k='A';$k<=$highestColumn;$k++){
                // 读取单元格
                $data[$j][]=$objPHPExcel->getActiveSheet()->getCell("$k$j")->getValue();
            }
        }
        return $data;
    }

这里取得excel文件的数据内容,能够轮回插进去数据库中

以上就是TP5援用PHPExcel完成导入导出功用的细致内容,更多请关注ki4网别的相干文章!

标签:PHPExcel