设为首页 加入收藏

TOP

PHP导出数据到表格的实例(一)
2019-08-15 23:29:23 】 浏览:27
Tags:PHP 导出 数据 表格 实例

我发现最近的项目需要导出Excel表的页面非常的多,想来这个也是我们常用的功能了,现在我可以很熟练的导出了,但是记得当时自己第一次导出时还是绕了一些弯路的,那么现在我就来记录下我这次用exshop框架项目下的导出(其实在不同的框架下Excel的导出原理都是差不多的)


前端

 <a href="java script:;" id="export_all" class="coolbg">导出</a>

<script>
    //导出数据
    $('#export_all').click(function(){
        window.open('index.php?app=craft_order&act=export', '_blank');
    });
</script>

控制器
```
// 导出数据
public function export() {
$result = $this->_oaOrderModel->getAllOrderListForManager($this->store_id, $orderSn=null, $buyer_id=null, $buyer_name=null, $consignee=null, $phone=null, $company_name=null, $status=null, $s_time=null, $e_time=null, $page=null, $listRows=null, $execl=true); //这个是获得数据的代码-model里
$orderList = $result['orderList'];
if (!empty($orderList)) {
$j = 1;
$stmt = array();
foreach ($orderList as $val) {
$stmt[$j]['网站ID'] = $val['store_id'];
$stmt[$j]['订单信息'] = $val['order_sn'];
$stmt[$j]['商品信息'] = $val['inventory_sn_count_chinese'];
$stmt[$j]['工艺选择'] = $val['craft_count_chinese'];
$stmt[$j]['商品总数量'] = $val['real_goods_total_count'];
$stmt[$j]['提交日期'] = date("Y-m-d H:i:s",$val['add_time']);
$stmt[$j]['客户名称'] = $val['company_name'];
$stmt[$j]['联系人'] = $val['consignee'];
$stmt[$j]['联系方式'] = $val['phone_mob'];
$stmt[$j]['订单完成率'] = $val['percentage_complete'];
$stmt[$j]['订单状态'] = $val['statusChinese'];
$j++;
}
$current_path = dirname(FILE);
$home_path = dirname($current_path);
require_once ROOT_PATH . '/includes/libraries/PHPExcel.php';
require_once ROOT_PATH . '/includes/libraries/PHPExcel/IOFactory.php';
$objPHPExcel = new PHPExcel(); //这个方法自己下载放到公共方法里
$objPHPExcel->getProperties()->setCreator("Maarten Balliauw")
->setLastModifiedBy("Maarten Balliauw")
->setTitle("Office 2007 XLSX Test Document")
->setSubject("Office 2007 XLSX Test Document")
->setDescription("Document for Office 2007 XLSX, generated using PHP classes.")
->setKeywords("office 2007 openxml php")
->setCategory("Test result file");

        // 行高
        for ($i = 2; $i <= count($stmt); $i++) {
            $objPHPExcel->getActiveSheet()->getRowDimension($i)->setRowHeight(22);
        }
        foreach ($stmt as $fid => $fval) {
            if ($fid == 1) {
                $key = 0;
                foreach ($fval as $title => $first) {
                    //如果一级标题
                    $objPHPExcel->getActiveSheet()->setCellValue(chr($key + 65) . '1', $title);
                    $objPHPExcel->getActiveSheet()->getStyle(chr($key + 65) . '1')->getFont()->setBold(true);       // 加粗
                    $key ++;
                }
            }
            $cid = 0;
            $row_id = $fid + 1;
            foreach ($fval as $cval) {
                $objPHPExcel->getActiveSheet()->setCellValue(chr($cid + 65) . (string) ($row_id), $cval);
                $cid++;
            }
        }
        $objPHPExcel->setActiveSheetIndex(0);
        $objPHPExcel->getActiveSheet()->setTitle('Excel表');
        $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
        //$objWriter->save('订单列表详细.xls');
        //输出到浏览器
        header("Content-Type: application/force-download");
        header("Content-Type: application/octet-stream");
        header("Content-Type: application/download");
        header('Content-Disposition:inline;filename="订单列表.xls"');
        header("Content-Transfer-Encoding: binary");
        heade
首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇xhprof扩展安装与使用 下一篇PHP接口

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目