Home > Backend Development > PHP Tutorial > php export data into excel table

php export data into excel table

藏色散人
Release: 2023-04-09 21:40:01
forward
8039 people have browsed it

This article introduces PHP to export the data into Excel table methods. It has a certain reference value. Friends who need it can refer to it. I hope it will be helpful to everyone.

php develops and exports excel tables. How to write the code? Today I want to share this with you. What we want to do is to export the data in the database into an excel table, and export it to what we want according to our rules. Let’s directly upload the source code to you,

php export data into excel table

This is the specific logic code

 $list = Db::table('form')->where('create_time', '>', $stat_time)->select()
      ->where(&#39;create_time&#39;,&#39;<&#39;,$end_time);
  if(empty($list)){
   echo "<script>alert(&#39;暂时无数据&#39;);window.history.back();</script>";
            exit();
  }
  //dump($list);die;
  foreach ($list as $key => $value) {
            $tuij=Db::table(&#39;form&#39;)->where(&#39;id&#39;,$value[&#39;id&#39;])->find();
            $arr[$key][&#39;username&#39;]=$tuij[&#39;username&#39;];
            $arr[$key][&#39;phone&#39;]=$tuij[&#39;phone&#39;];
            $arr[$key][&#39;source&#39;]=$tuij[&#39;source&#39;];
            $arr[$key][&#39;text&#39;]=$value[&#39;text&#39;];
            $arr[$key][&#39;create_time&#39;]=$value[&#39;create_time&#39;];
        }
        if(empty($list)){
            echo "<script>alert(&#39;暂时无数据&#39;);window.history.back();</script>";
            exit();
        }
        //$list为所需要导出的数据
        $header=array(&#39;姓名&#39;,&#39;电话&#39;,&#39;来源&#39;,&#39;留言&#39;,&#39;提交时间&#39;);
        $index=array(&#39;username&#39;,&#39;phone&#39;,&#39;source&#39;,&#39;text&#39;,&#39;create_time&#39;);
        $filename="表单落地页有效推广";
        $this->createtable($arr,$filename,$header,$index);
}
Copy after login

The last line of the above code mentions a method createtable. This is a public method. You can put it in a public class or directly. In this class, the following is the source code

/**
     * 导出公共方法
     *
     * @return \think\Response
     */
function createtable($list,$filename,$header,$index){ 
        header("Content-type:application/vnd.ms-excel"); 
        header("Content-Disposition:filename=".$filename.".xls"); 
        $teble_header = implode("\t",$header);
        $strexport = $teble_header."\r";
        foreach ($list as $row){ 
            foreach($index as $val){
                $strexport.=$row[$val]."\t";  
            }
            $strexport.="\r";

        } 
        $strexport=iconv(&#39;UTF-8&#39;,"GB2312//IGNORE",$strexport); 
        exit($strexport);
    }
``````php
Copy after login

[Recommended learning: PHP video tutorial]

The above is the detailed content of php export data into excel table. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:learnku.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