EXCEL导出方式之HEADER导出HTML

直接用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');
?>




  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值