PHP导出多少到表格的实例

发布时间:2019-02-02  栏目:Python  评论:0 Comments


首先,是一段javascript脚本:

自己发现以来的项目须求导出Excel表的页面极度的多,想来那些也是大家常用的功力了,现在自家得以很熟谙的导出了,可是记得当时自己首先次导出时依然绕了一些弯路的,那么现在我就来记录下我这一次用exshop框架项目下的导出(其实在分歧的框架下Excel的导出原理都是大抵的)

复制代码 代码如下:


/**
 * 
 * 
 * @param table_id 表格的id
 * @param container_id 容器的id
 * @param form_id 提交表单的id
 * @param title 文件名
 * @param rownumbers
 */ 
function getXlsFromTbl(table_id, container_id ,form_id, title,
rownumbers) { 
    try { 
        var content = “”; 

前端

 
        if (table_id != null && table_id != “” && table_id != “null”)

        <SPAN style=”WHITE-SPACE: pre”> </SPAN>content =
getTblData($(‘#’ + table_id), $(‘#’ + container_id), rownumbers); 
        } 
        if (content == “”) { 
            alert(“表格不存在”); 
            return; 
        } 
        var fileName = getExcelFileName(title); 

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

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

        doFileExport($(‘#’ + form_id), fileName, content); 
    } 
    catch (e) { 
        alert(“导出很是:” + e.name + “->” + e.description + “!”); 
    } 

function getTblData(tableobj, containerobj, rownumbers) { 

控制器
“`
// 导出多少
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”);

 
    var outStr = “”; 
    if (tableobj != null) { 
        var rowdata = tableobj.getRowData(); 
        var Lenr = 1; 

        // 行高
        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");
        header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
        header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
        header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
        header("Pragma: no-cache");
        $objWriter->save('php://output');
    }
}

```

 
        for (i = 0; i < Lenr; i++) { 
            //var Lenc = curTbl.rows(i).cells.length;  
            var th; 
            if (rownumbers == false) { 
                th = containerobj.find(‘TH:not(:first-child)’); 
            } 
            else { 
                th = containerobj.find(‘TH’); 
            } 
            th.each(function(index, element) { 
                var j = index + 1; 
                var content = $(element).text(); 
                content = content.replace(/(^\s*)|(\s*$)/g,
“”);//去掉空格  
                outStr += content + “,”; 
            }); 
            outStr += “+nl+”; 
        } 
        var tmp = “”; 
        for (i = 0; i < rowdata.length; i++) { 
            var row = eval(rowdata[i]); 
            for (each in row) { 
            <SPAN style=”WHITE-SPACE: pre”>   </SPAN>var
temp = $(row[each]).text(); 
            <SPAN style=”WHITE-SPACE: pre”>  
</SPAN>if($(row[each]).text() == null || $(row[each]).text()
== “”){ 
            <SPAN style=”WHITE-SPACE: pre”>      
</SPAN>if(row[each].charAt(0) != ‘<‘) 
            <SPAN style=”WHITE-SPACE: pre”>          
</SPAN>outStr += row[each] + “,”; 
            <SPAN style=”WHITE-SPACE: pre”>       </SPAN> 
            <SPAN style=”WHITE-SPACE: pre”>   </SPAN>} 
            <SPAN style=”WHITE-SPACE: pre”>   </SPAN>else 
            <SPAN style=”WHITE-SPACE: pre”>      
</SPAN>outStr += $(row[each]).text() + “,”; 
            } 
            outStr += “+nl+”; 
        } 
    } 
    else { 
        outStr = null; 
        alert(inTbl + ” null!”); 
    } 
    return outStr; 

function getExcelFileName(title) { 
    var d = new Date(); 
    var curYear = d.getYear(); 
    var curMonth = “” + (d.getMonth() + 1); 
    var curDate = “” + d.getDate(); 
    var curHour = “” + d.getHours(); 
    var curMinute = “” + d.getMinutes(); 
    var curSecond = “” + d.getSeconds(); 
    if (curMonth.length == 1) { 
        curMonth = “0” + curMonth; 
    } 
    if (curDate.length == 1) { 
        curDate = “0” + curDate; 
    } 
    if (curHour.length == 1) { 
        curHour = “0” + curHour; 
    } 
    if (curMinute.length == 1) { 
        curMinute = “0” + curMinute; 
    } 
    if (curSecond.length == 1) { 
        curSecond = “0” + curSecond; 
    } 
    var fileName = title + “_” + curYear + curMonth + curDate + “_” 
            + curHour + curMinute + curSecond + “.csv”; 

只顾:PHPExcel(); //那些方法自己下载放到公共措施里

 
    return fileName; 

function doFileExport(formobj, filename, content) { 
<SPAN style=”WHITE-SPACE: pre”>
</SPAN>formobj.html(“<input id=’filename’ name=’filename’
type=’text’ style=’display: none’><input id=’content’
name=’content’ type=’text’ style=’display: none’>”); 
    $(“#filename”).val(filename); 
    $(“#content”).val(content); 
    formobj.submit(); 
}  

成果图

继而是页面调用的javascript:

图片 1

复制代码 代码如下:

图片 2

<form id=”download_form” method=”post”  target=”_blank”
action=”downLoadTableDataAction”> 

图片 3

复制代码 代码如下:


<div id=”table_container”> 
<SPAN style=”WHITE-SPACE: pre”> </SPAN><table
id=”keyword_detail”></table> 
<SPAN style=”WHITE-SPACE: pre”> </SPAN><div
id=”footer”></div> 
</div> 

心得

复制代码 代码如下:

有时候蒙受那个标题可以多想想,多看看它的原理,原理了然了下次做其他的也是会的,但最根本的是要清楚做笔录,大家的记得并从未想像的那么好

//下载  
$(‘#download_file’).click(function() { 
    getXlsFromTbl(‘keyword_detail’, ‘table_container’
,’download_form’, ‘关键词详细数据’, true) 
}); 

注:小说来源雨中笑记录实习期遇到的难点与经验,转发请表达原文

您可能感兴趣的稿子:

留下评论

网站地图xml地图