1. First download the PHPexcel class library from github
or download the PHPexcel class library through the following link.
//m.sbmmt.com/xiazai/leiku/1491
2. After decompressing, copy it to extend
The controller code is as follows:
<?php /** * Created by PhpStorm. * User: luxiao * Date: 2017/5/8 * Time: 16:49 */ namespace app\index\controller; use think\Loader; use think\Controller; class Excel extends Controller { function excel() { $path = dirname(__FILE__); //找到当前脚本所在路径 Loader::import('PHPExcel.Classes.PHPExcel'); //手动引入PHPExcel.php Loader::import('PHPExcel.Classes.PHPExcel.IOFactory.PHPExcel_IOFactory'); //引入IOFactory.php 文件里面的PHPExcel_IOFactory这个类 $PHPExcel = new \PHPExcel(); //实例化 $PHPSheet = $PHPExcel->getActiveSheet(); $PHPSheet->setTitle("demo"); //给当前活动sheet设置名称 $PHPSheet->setCellValue("A1","姓名")->setCellValue("B1","分数");//表格数据 $PHPSheet->setCellValue("A2","张三")->setCellValue("B2","2121");//表格数据 $PHPWriter = \PHPExcel_IOFactory::createWriter($PHPExcel,"Excel2007"); //创建生成的格式 header('Content-Disposition: attachment;filename="表单数据.xlsx"'); //下载下来的表格名 header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'); $PHPWriter->save("php://output"); //表示在$path路径下面生成demo.xlsx文件 } }
You can generate a table by calling the excel method, and then write the code yourself according to your own needs.
PHPexcel table data is imported into the database city Table, before that, create the form yourself. This time I used the address data table for testing:
function inserExcel() { Loader::import('PHPExcel.Classes.PHPExcel'); Loader::import('PHPExcel.Classes.PHPExcel.IOFactory.PHPExcel_IOFactory'); Loader::import('PHPExcel.Classes.PHPExcel.Reader.Excel5'); //获取表单上传文件 $file = request()->file('excel'); $info = $file->validate(['ext' => 'xlsx'])->move(ROOT_PATH . 'public' . DS . 'uploads'); //上传验证后缀名,以及上传之后移动的地址 if ($info) { // echo $info->getFilename(); $exclePath = $info->getSaveName(); //获取文件名 $file_name = ROOT_PATH . 'public' . DS . 'uploads' . DS . $exclePath; //上传文件的地址 $objReader =\PHPExcel_IOFactory::createReader('Excel2007'); $obj_PHPExcel =$objReader->load($file_name, $encode = 'utf-8'); //加载文件内容,编码utf-8 echo "<pre class="brush:php;toolbar:false">"; $excel_array=$obj_PHPExcel->getsheet(0)->toArray(); //转换为数组格式 array_shift($excel_array); //删除第一个数组(标题); $city = []; foreach($excel_array as $k=>$v) { $city[$k]['Id'] = $v[0]; $city[$k]['code'] = $v[1]; $city[$k]['path'] = $v[2]; $city[$k]['pcode'] = $v[3]; $city[$k]['name'] = $v[4]; } Db::name('city')->insertAll($city); //批量插入数据 } else { echo $file->getError(); }
Front-end code:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <form action="http://localhost/chexian5.0/index.php/index/excel/intoexcel" enctype="multipart/form-data" method="post"> <input type="file" name="excel" /> <input type="submit" value="导入"> </form> </body> </html>
The above is the detailed content of TP5.0 PHPExcel data table export and import. For more information, please follow other related articles on the PHP Chinese website!