Home > Backend Development > PHP Tutorial > How to use PHPExcel to batch export data into excel tables

How to use PHPExcel to batch export data into excel tables

巴扎黑
Release: 2023-03-10 13:38:01
Original
1439 people have browsed it

The following editor will bring you an article on how to use PHPExcel to batch export data into excel tables (a must-read). The editor thinks it’s pretty good, so I’ll share it with you now and give it as a reference. Let’s follow the editor and take a look.

First you need to download the PHPExecel class file. For help documents, please refer to the PHPExcel Chinese Help Manual|How to use PHPExcel.

The example below is a simple batch export data to excel that I wrote myself

The front page is relatively simple, that is A hyperlink jumps to the processing page. The hyperlink can also be followed by some parameters (depending on the requirements)!


<a href="./Process1.php" rel="external nofollow" >导出excel表格</a>
Copy after login

Backend Process.php page


/**
* 批量导出数据
* @param $arr 从数据库查询出来,即要导出的数据
*  $name excel表歌名
*/
function expExcel($arr,$name){
 
 require_once &#39;PHPExcel.php&#39;;
 //实例化
 $objPHPExcel = new PHPExcel();
 /*右键属性所显示的信息*/
  $objPHPExcel->getProperties()->setCreator("zxf")  //作者
       ->setLastModifiedBy("zxf")  //最后一次保存者
       ->setTitle(&#39;数据EXCEL导出&#39;)  //标题
       ->setSubject(&#39;数据EXCEL导出&#39;) //主题
       ->setDescription(&#39;导出数据&#39;)  //描述
       ->setKeywords("excel")   //标记
       ->setCategory("result file");  //类别


 //设置当前的表格 
 $objPHPExcel->setActiveSheetIndex(0);
 // 设置表格第一行显示内容
 $objPHPExcel->getActiveSheet()
  ->setCellValue(&#39;A1&#39;, &#39;业主姓名&#39;)
  ->setCellValue(&#39;B1&#39;, &#39;密码&#39;)
  ->setCellValue(&#39;C1&#39;, &#39;手机号码&#39;)
  ->setCellValue(&#39;D1&#39;, &#39;地址&#39;)
  //设置第一行为红色字体
  ->getStyle(&#39;A1:D1&#39;)->getFont()->getColor()->setARGB(PHPExcel_Style_Color::COLOR_RED);

 $key = 1;
 /*以下就是对处理Excel里的数据,横着取数据*/
 foreach($arr as $v){

 //设置循环从第二行开始
 $key++;
  $objPHPExcel->getActiveSheet()

     //Excel的第A列,name是你查出数组的键值字段,下面以此类推
     ->setCellValue(&#39;A&#39;.$key, $v[&#39;name&#39;]) 
     ->setCellValue(&#39;B&#39;.$key, $v[&#39;pwd&#39;])
     ->setCellValue(&#39;C&#39;.$key, $v[&#39;phone&#39;])
     ->setCellValue(&#39;D&#39;.$key, $v[&#39;address&#39;]);

 }
 //设置当前的表格 
 $objPHPExcel->setActiveSheetIndex(0);
   ob_end_clean();  //清除缓冲区,避免乱码
  header(&#39;Content-Type: application/vnd.ms-excel&#39;); //文件类型
  header(&#39;Content-Disposition: attachment;filename="&#39;.$name.&#39;.xls"&#39;); //文件名
  header(&#39;Cache-Control: max-age=0&#39;);
  header(&#39;Content-Type: text/html; charset=utf-8&#39;); //编码
  $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, &#39;Excel5&#39;);  //excel 2003
  $objWriter->save(&#39;php://output&#39;); 
  exit;

}

/***********调用**********************/
header("Content-type:text/html;charset=utf-8");

//链接数据库
$link = @mysql_connect(&#39;localhost&#39;,&#39;root&#39;,&#39;&#39;) or die(&#39;连接数据库失败&#39;);
mysql_select_db(&#39;test&#39;,$link);
mysql_query(&#39;set names utf8&#39;);

//先获取数据
$sql = "select * from house";
$res = mysql_query($sql);
$arr = array();
//把$res=>$arr,把结果集内容转移到一个数组中
while ($row = mysql_fetch_assoc($res)){
 $arr[] = $row;
}

//excel表格名
$name = "用户表";

//调用
expExcel($arr,$name)
Copy after login

This completes the use of PHPExcel to export data. For information on using PHPExcel to import data into the database, please refer to Using PHPExcel to batch upload data to the database.

The above is the detailed content of How to use PHPExcel to batch export data into excel tables. 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