TP5.0 PHPExcel data table export and import

藏色散人
Release: 2023-04-07 06:42:02
forward
3561 people have browsed it

1. First download the PHPexcel class library from github

or download the PHPexcel class library through the following link.

http://www.php.cn/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(&#39;PHPExcel.Classes.PHPExcel&#39;);  //手动引入PHPExcel.php
        Loader::import(&#39;PHPExcel.Classes.PHPExcel.IOFactory.PHPExcel_IOFactory&#39;);  //引入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(&#39;Content-Disposition: attachment;filename="表单数据.xlsx"&#39;);  //下载下来的表格名
        header(&#39;Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet&#39;);
        $PHPWriter->save("php://output"); //表示在$path路径下面生成demo.xlsx文件
    }
}
Copy after login

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(&#39;PHPExcel.Classes.PHPExcel&#39;);
        Loader::import(&#39;PHPExcel.Classes.PHPExcel.IOFactory.PHPExcel_IOFactory&#39;);
        Loader::import(&#39;PHPExcel.Classes.PHPExcel.Reader.Excel5&#39;);
        //获取表单上传文件
        $file = request()->file(&#39;excel&#39;);
        $info = $file->validate([&#39;ext&#39; => &#39;xlsx&#39;])->move(ROOT_PATH . &#39;public&#39; . DS . &#39;uploads&#39;);  //上传验证后缀名,以及上传之后移动的地址
        if ($info) {
//            echo $info->getFilename();
            $exclePath = $info->getSaveName();  //获取文件名
            $file_name = ROOT_PATH . &#39;public&#39; . DS . &#39;uploads&#39; . DS . $exclePath;   //上传文件的地址
            $objReader =\PHPExcel_IOFactory::createReader(&#39;Excel2007&#39;);
            $obj_PHPExcel =$objReader->load($file_name, $encode = &#39;utf-8&#39;);  //加载文件内容,编码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][&#39;Id&#39;] = $v[0];
                $city[$k][&#39;code&#39;] = $v[1];
                $city[$k][&#39;path&#39;] = $v[2];
                $city[$k][&#39;pcode&#39;] = $v[3];
                $city[$k][&#39;name&#39;] = $v[4];
            }
            Db::name(&#39;city&#39;)->insertAll($city); //批量插入数据
        } else {
            echo $file->getError();
        }
Copy after login

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>
Copy after login

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!

Related labels:
source:cnblogs.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!