Thinkphp5+PHPExcel realizes the function of batch uploading table data_php example

韦小宝
Release: 2023-03-17 18:12:01
Original
2301 people have browsed it

This article mainly introduces the batch upload form data function of Thinkphp5+PHPExcel. Friends who need it can refer to the following

1. First, download PHPExcel. Go to the vendor folder, my path is: project/vendor/PHPExcel/, put the downloaded PHPExcel file here

2. Front-end code


<!DOCTYPE html>
<html>
<head>
  <title>批量导入数据</title>
</head>
<body>
<form action="{:url(&#39;/index/index/importExcel&#39;)}" method="post" enctype="multipart/form-data">
  <input type="file" name="myfile"><br/>
  <input type="submit" value="批量的导入">
</form>
</body>
</html>
Copy after login


3.Backend code


/**
  * 导入表格数据
  * 先把文件上传到服务器,然后再读取数据存到数据库
  */
  public function importExcel(){
    header("content-type:text/html;charset=utf-8");
    //上传excel文件
    $file = request()->file(&#39;myfile&#39;);
    //移到/public/uploads/excel/下
    $info = $file->move(ROOT_PATH.&#39;public&#39;.DS.&#39;uploads&#39;.DS.&#39;excel&#39;);
    //上传文件成功
    if ($info) {
      //引入PHPExcel类
      vendor(&#39;PHPExcel.PHPExcel.Reader.Excel5&#39;);
      //获取上传后的文件名
      $fileName = $info->getSaveName();
      //文件路径
      $filePath = &#39;public/uploads/excel/&#39;.$fileName;
      //实例化PHPExcel类
      $PHPReader = new \PHPExcel_Reader_Excel5();
      //读取excel文件
      $objPHPExcel = $PHPReader->load($filePath);
      //读取excel文件中的第一个工作表
      $sheet = $objPHPExcel->getSheet(0);
      $allRow = $sheet->getHighestRow();  //取得总行数
      //$allColumn = $sheet->getHighestColumn();  //取得总列数
      //从第二行开始插入,第一行是列名
      for ($j=2; $j <= $allRow; $j++) {
        $data[&#39;name&#39;] = $objPHPExcel->getActiveSheet()->getCell("A".$j)->getValue();
        $data[&#39;tel&#39;] = $objPHPExcel->getActiveSheet()->getCell("B".$j)->getValue();
        $data[&#39;addr&#39;] = $objPHPExcel->getActiveSheet()->getCell("C".$j)->getValue();
        $last_id = Db::table(&#39;users&#39;)->insertGetId($data);//保存数据,并返回主键id
        if ($last_id) {
          echo "第".$j."行导入成功,users表第:".$last_id."条!<br/>";
        }else{
          echo "第".$j."行导入失败!<br/>";
        }
      }
    }else{
      echo "上传文件失败!";
    }
  }
Copy after login


Output Result:


Note:

Introduce third-party libraries using vendor(); It is in the form of namespace. The underlying code will automatically replace "." with "/", so when using "/", use "." instead;

The above code can be copied and used directly, but the database related information must be changed to your own !

Summary

The above is the Thinkphp5+PHPExcel implementation introduced by the editor to implement the batch upload form data function. I hope it will be useful to everyone. Help, if you have any questions please ask in our community Q&A.

Related recommendations:

How to import data without refreshing using ThinkPHP, uploadify, upload, PHPExcel

How does phpexcel import excel to process big data code examples

Use PHPExcel to upload data in batches

The above is the detailed content of Thinkphp5+PHPExcel realizes the function of batch uploading table data_php example. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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!