Home > Backend Development > PHP Tutorial > Example of how the ThinkPHP framework implements exporting excel data

Example of how the ThinkPHP framework implements exporting excel data

jacklove
Release: 2023-04-01 18:50:01
Original
1747 people have browsed it

This article mainly introduces the method of exporting excel data in the ThinkPHP framework, and analyzes the related implementation techniques of thinkPHP adding org extension to export Excel data based on PHPExcel in the form of examples. Friends in need can refer to the following

The example in this article describes how the ThinkPHP framework implements exporting excel data. Share it with everyone for your reference, the details are as follows:

In the ThinkPHP framework, an example of how to export excel data:

Before operation, the ORG library should be added to the extension directory of the system framework. That is to include the ThinkPHP\Extend\Library\ORG\Util\PHPExcel.class.php file and its related supporting files.

<?php
header("Content-type: text/html; charset=utf-8");
class MesTestAction extends Action {
  //测试导出excel数据
  public function tpGetExcel() {
    //创建对象
    import("ORG.Util.PHPExcel"); //从PHPExcel目录导PHPExcel.php类文件
    $excel = new PHPExcel();
    $data = M()->query(&#39;SELECT userid,username,stepgoal FROM tp_data_user LIMIT 2775&#39;);
    //Excel表格式,这里简略写了3列
    $letter = array(&#39;A&#39;,&#39;B&#39;,&#39;C&#39;);
    //表头数组
    $tableheader = array(&#39;userid&#39;,&#39;用户名&#39;,&#39;目标步数&#39;);
    $count= count($data);//总的数据行数
    $listNum = 500;//每个sheet页最大数据行数
    $num = ceil($count/$listNum);//sheet页个数
    $MuitData = array_chunk($data,$listNum,false);//分割总的数据,每页最多$listNum行有效数据
  //var_dump($MuitData);//die(&#39;as&#39;);
  //缺省情况下,PHPExcel会自动创建第一个SHEET,其索引SheetIndex=0
  //设置 当前处于活动状态的SHEET 为PHPExcel自动创建的第一个SHEET
  $excel->setActiveSheetIndex(0); //objPHPExcel
  //设置sheet的title
  $excel->getActiveSheet()->setTitle(&#39;考核得分第&#39;.&#39;1&#39;.&#39;页&#39;);
  //设置sheet的列名称
    for($k = 0; $k < count($tableheader); ++$k) {
      $excel->getActiveSheet()->setCellValue("$letter[$k]".&#39;1&#39;,"$tableheader[$k]");//第一行数据
    }
  //填充表格信息 处理第1块数据
  $crrntSheetLineNo = count($MuitData[0]) + 1;
  for ( $j = 2; $j <= $crrntSheetLineNo; ++$j) { //遍历每一行
    $k = 0;
    foreach ( $MuitData[0][$j - 2] as $key => $value ) {//遍历具体行的某一列
      $excel->getActiveSheet()->setCellValue("$letter[$k]".$j,"$value");//第$k列 第$j行
      $k++;
    }
  }
  //后续的sheet页及数据块
    for ( $i = 1; $i <$num; ++$i) {
      //创建第$i个sheet
      $msgWorkSheet = new PHPExcel_Worksheet($excel, &#39;考核得分第&#39;.($i + 1).&#39;页&#39;); //创建一个工作表
      $excel->addSheet($msgWorkSheet); //插入工作表
      $excel->setActiveSheetIndex($i); //切换到新创建的工作表
      //设置sheet的列名称
      for($k = 0; $k < count($tableheader); ++$k) {
        $excel->getActiveSheet()->setCellValue("$letter[$k]1","$tableheader[$k]");//第一行数据
      }
      //填充表格信息 处理第$i块数据
    $crrntSheetLineNo = count($MuitData[$i]) + 1; //var_dump($crrntSheetLineNo);var_dump($MuitData[$i-1]);die(&#39;as&#39;);
      for ( $j = 2; $j <= $crrntSheetLineNo; ++$j) { //遍历每一行
        $k = 0;
        foreach ( $MuitData[$i-1][$j - 2] as $key => $value ) {//遍历具体行的某一列
          $excel->getActiveSheet()->setCellValue("$letter[$k]$j","$value");//第$k列 第$j行
          ++$k;
        }
      }
      usleep(100);
    }
    //创建Excel输出对象
    $filename = "大奖赛培训考核得分.xls";
    $write = new PHPExcel_Writer_Excel5($excel);
    ob_end_clean();//清除缓冲区,避免乱码
  /*
  //输出到本地
    $write->save( iconv(&#39;utf-8&#39;, &#39;gbk&#39;, $filename) );
  */
    //输出到浏览器
    header("Pragma: public");
    header("Expires: 0");
    header("Cache-Control:must-revalidate, post-check=0, pre-check=0");
    header("Content-Type:application/force-download");
    header("Content-Type:application/vnd.ms-execl");
    header("Content-Type:application/download");
    header(&#39;Content-Type:application/octet-stream&#39;);
    $encoded_filename = urlencode($filename);
    $encoded_filename = str_replace("+", "%20", $encoded_filename);
    $ua = $_SERVER["HTTP_USER_AGENT"];
    if (preg_match("/MSIE/", $ua)) {
      header(&#39;Content-Disposition: attachment; filename="&#39; . $encoded_filename . &#39;"&#39;);
    } else if (preg_match("/Firefox/", $ua)) {
      header(&#39;Content-Disposition: attachment; filename*="utf8\&#39;\&#39;&#39; . $filename . &#39;"&#39;);
    } else {
      header(&#39;Content-Disposition: attachment; filename="&#39; . $filename . &#39;"&#39;);
    }
    header("Content-Transfer-Encoding:binary");
    $write->save(&#39;php://output&#39;);
  }
}
?>
Copy after login

Articles you may be interested in:

Native JS implements Ajax through POST and PHP Interaction method example php skills

Laravel method to integrate Geetest verification code php example

Phpstorm Xdebug breakpoint How to debug PHP php instance

The above is the detailed content of Example of how the ThinkPHP framework implements exporting excel data. 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