您的位置:首页 > Web前端 > HTML

EXCEL导出方式之HEADER导出HTML

2016-12-09 08:53 417 查看
直接用header导出html,指定样式的excel   

<form method="get" id="stock-form">
<div class="form-group">
<input placeholder="开始时间" type="text" class="laydate-icon form-control" style="width:240px;" name="start" id="start" value="<?php if (isset($param['start'])) { echo $param['start'];} ?>" readonly>
</div>
<div class="form-group">
<input placeholder="结束时间" type="text" class="laydate-icon form-control" style="width:240px; margin-top:5px;" name="end" id="end" value="<?php if (isset($param['end'])) { echo $param['end'];} ?>" readonly>
</div>
<div class="form-group sear_rt">
<button type="submit" id="search" class="btn btn-success">搜索</button>
</div>
<div class="clear"></div>
<div class="form-group sear_rt">
<button type="button" id="export" class="btn btn-success">导出</button>
</div>
</form>

<script src="http://code.jquery.com/jquery-latest.js"></script>
<script language="javascript">
$(document).ready(function () {

$('#search').click(function(){
$('#stock-form').attr('action','');
});
$('#export').on('click',function(){
$('#stock-form').attr('action','cars/export');
$('#stock-form').submit();
});
});
</script>
<?php
$param = array();
#####***********时间查询**************######
$param['start'] = $_GET['start'];
$param['end'] = $_GET['end'];
#####***********时间查询**************######

*********************其他代码部分************************
//$arrayList   结果集:获取满足当前条件的数据

//创建展示表格
$strTable ='<table width="100%" border="1">';
$strTable .= '<tr>';
$strTable .= '<td style="text-align:center;font-size:14px;" height="30" width="80">模拟EXCEL表格A</td>';
$strTable .= '<td style="text-align:center;font-size:14px;" height="30" width="80">模拟EXCEL表格B</td>';
$strTable .= '<td style="text-align:center;font-size:14px;" height="30" width="80">模拟EXCEL表格C</td>';
$strTable .= '</tr>';

foreach($arrayList as $k=>$val){
$strTable .= '<tr>';
$strTable .= '<td style="text-align:center;font-size:14px;" height="30" width="80">'.$val['excelA'].'</td>';
$strTable .= '<td style="text-align:center;font-size:14px;" height="30" width="80">'.$val['excelB'].'</td>';
$strTable .= '<td style="text-align:center;font-size:14px;" height="30" width="80">'.$val['excelC'].'</td>';
$strTable .= '</tr>';
}

$strTable .='</table>';
unset($arrayList);
header("Content-type: application/vnd.ms-excel");
header("Content-Type: application/force-download");
header("Content-Disposition: attachment; filename=表格名称_".date('Y-m-d h:i:s').".xls");
header('Expires:0');
header('Pragma:public');
echo $strTable;
exit();
?>

附PHPEXCEL模式导出
<?php
$param = array();
#####***********时间查询**************######
$param['start'] = $_GET['start'];
$param['end'] = $_GET['end'];
#####***********时间查询**************######

*********************其他代码部分************************
//$arrayList 结果集:获取满足当前条件的数据

require ./phpexcel/Classes/PHPExcel.php';

//创建对象
$excel = new \PHPExcel();
//Excel表格式,这里简略写了4列
$letter = array('A','B','C','D');
//表头数组
$tableheader = array('模拟EXCEL表格A','模拟EXCEL表格B','模拟EXCEL表格C','模拟EXCEL表格D');//第一行表头
//填充表头信息
for($i = 0;$i < count($tableheader);$i++) {
$excel->getActiveSheet()->setCellValue("$letter[$i]1","$tableheader[$i]");
}
//填充表格信息
for ($i = 2;$i <= count($arrayList) + 1;$i++) {//第二行开始
$excel->getActiveSheet()->setCellValue("$letter[0]$i",$arrayList[$i-2]['excelA']);
$excel->getActiveSheet()->setCellValue("$letter[1]$i",$arrayList[$i-2]['excelB']);
$excel->getActiveSheet()->setCellValue("$letter[2]$i",$arrayList[$i-2]['excelC']);
$excel->getActiveSheet()->setCellValue("$letter[3]$i",$arrayList[$i-2]['excelD']);
}

//创建Excel输入对象
$write = new \PHPExcel_Writer_Excel5($excel);
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/octet-stream");
header("Content-Type:application/download");
header("Content-Disposition: attachment; filename=表格名称_".date('Y-m-d h:i:s').".xls");
header("Content-Transfer-Encoding:binary");
$write->save('php://output');
?>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  EXCEL导出 HEADER