tp5 PHPExcel下載匯出Excel檔案
1、HTML程式碼
<table class="table table-border table-bordered table-bg">
<thead>
<tr>
<th scope="col" colspan="7">資料列表<a href="__ROOT__/admin.php/Import/excel_data" style="margin-left: 30px;">匯出</a></th>
</tr>
<tr class="text-c">
<!-- <th width="25"><input type="checkbox" value="" id="CheckAll" name='CheckAll'></th> -->
<th width="40">ID</th>
<th width="150">姓名</th>
<th width="150">電話</th>
<th width="150">批次</th>
<th width="130">工號</th>
<th width="150">備註</th>
<th width="100">操作</th>
</tr>
</thead>
<tbody>
{if condition="!empty($list)"}
{foreach $list as $vo}
<tr class="text-c" id="{$vo.id}">
<!-- <td><input type="checkbox" value="{$vo.id}" id='Check[]' name='Check[]'></td> -->
<td>{$vo.id}</td>
<td>{$vo.uname}</td>
<td>{$vo.utel1}</td>
<td>{$vo.upc}</td>
<td>{$vo.workno}</td>
<td>{$vo.uremark}</td>
<td class="td-manage">
<a title="編輯" href="javascript:;"style="text-decoration:none">檢視</a>|<a title="刪除" href="javascript:;" class="ml-5" style="text-decoration:none">刪除
</a>
</td>
</tr>
{/foreach}
{else /}
<tr class="text-c"><td colspan="9">還沒有新增資料</td></tr>
{/if}
</tbody>
</table>
{$page}
2、PHP程式碼
/**
* 匯出excel檔案
* @return mixed
*/
public function excel_data(){
vendor("PHPExcel.PHPExcel");
$objPHPExcel = new \PHPExcel();
// $objReader = \PHPExcel_IOFactory::createReader('Excel2007');
// $objReader = \PHPExcel_IOFactory::createReader('Excel5');
$PHPSheet = $objPHPExcel->getActiveSheet();
// $PHPSheet->setTitle("資料匯出"); //給當前活動sheet設定名稱
$PHPSheet->setCellValue("A1", "ID")
->setCellValue("B1", "姓名")
->setCellValue("C1", "電話")
->setCellValue("D1", "批次")
->setCellValue("E1", "工號");
$PHPSheet->getColumnDimension('C')->setWidth(15);//設定寬度
$PHPSheet->getColumnDimension('D')->setWidth(15);
// //設定水平居中
// $PHPSheet->getStyle('A1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
// $PHPSheet->getStyle('B2')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
// //垂直居中
// $PHPSheet->getStyle('A1')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
// $PHPSheet->getStyle('B2')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
//左對齊
$PHPSheet->getStyle('A')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_JUSTIFY);
$PHPSheet->getStyle('E')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_JUSTIFY);
//右對齊
// $PHPSheet->getStyle('A')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
$where['entid'] = 1;
$data = Db::table('cti_phone')
->where($where)
->order('id desc')
->select();
// dump($data);die;
$d = 2;
foreach($data as $key=>$vo){
$PHPSheet->setCellValue("A".$d,$vo['id'])
->setCellValue("B".$d,$vo['uname'])
->setCellValue("C".$d,$vo['utel1'])
->setCellValue("D".$d,$vo['upc'])
->setCellValue("E".$d,$vo['workno']);
$d++;
}
$PHPWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, "Excel2007");
ob_end_clean(); // Added by me
ob_start(); // Added by me
header('Content-Disposition: attachment;filename="表單資料.xlsx"');
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
$PHPWriter->save("php://output"); //表示在$path路徑下面生成demo.xlsx檔案
}
3、效果圖