無限極分類+商品匯出excel(Thinkphp5,資料讀取無限極,個人限制匯出5級)

來源:互聯網
上載者:User

標籤:common   價格   count   測試   abc   tac   lsm   list   amp   

分類表欄位: id(主鍵),name(分類名稱), pid(空為頂級,不為空白為上級id)

產品表: no,name,spec ...根據自己的產品表填寫

關聯表:id(主鍵),domelamp(分類表id), domelamp_part(產品表id)

 

HTML代碼

<button onclick="ExportExcel()" class="btn btn-danger" data-toggle="dropdown" style="margin-left: 30px;"> </span>匯出資料到XLS</button>

JS代碼

function ExportExcel(){
location.href = "{:url(‘Shopmodel/ExportExcel‘)}";
}

php 代碼 

class ShopmodelController extends BaseController

{

private $smallarr = array();
private $classnumber = array();
private $alllist = array();


/*
*
* 查看資料格式(excel匯出)
*
* */

public function ExportExcelview(){
$filename = "售後商品資料格式_" . date("Y_m_d", time()) . ".xls"; //名稱
$header = array(‘一級菜單‘,‘二級菜單‘,‘三級菜單‘,‘四級菜單‘,‘五級菜單‘,‘配件名稱‘,‘配件編號‘,‘規格說明‘,‘配件單位‘,‘配件顏色‘,‘配件材質‘,‘銷售價格‘);
$data = [
[‘測試‘,‘test‘],
[‘名稱‘,‘name‘]
];
excelExport($filename,$header,$data,‘format‘);
}

/*
*
* 匯出所有資料到xls
*
* */

public function ExportExcel(){

$data = $this->getlist();
$filename = "售後商品資料_" . date("Y_m_d", time()) . ".xls";
$header = array(‘一級菜單‘,‘二級菜單‘,‘三級菜單‘,‘四級菜單‘,‘五級菜單‘,‘配件名稱‘,‘配件編號‘,‘規格說明‘,‘配件單位‘,‘配件顏色‘,‘配件材質‘,‘銷售價格‘);
excelExport($filename,$header,$data,‘allinfo‘);

}


/*
*
* 匯出xls 查詢所有配件資訊
*
* */
public function getalllist($cid){
$info = Db::name(‘DomelampBind‘)
->alias(‘a‘)
->join(‘DomelampNode b‘,‘a.domelamp = b.id‘)
->join(‘DomelampPart c‘,‘a.domelamp_part = c.no‘)
->where(‘a.domelamp‘,‘in‘,$cid)
->field(‘c.code,c.name,c.spec,c.unit,c.type,c.category,c.price‘)
->select();
if(isset($this->number[$cid][‘contentlist‘])){//配件數量
$this->contentnumber[$cid] = count($info);
}else{
$this->contentnumber[$cid]= count($info);
}
foreach($info as $k=>$v){
$this->alllist[] = array_merge($this->smallarr[$cid],$v);
}
unset($this->smallarr[$cid]);
}

/*
*
* 匯出xls 擷取最底層資料
*
* */
public function getexcelsmallarr($pid = 0){
if($pid == 0){
$where = ‘pid is null‘;
}else{
$where = ‘pid = ‘.$pid;
}
$arr = Db::name(‘DomelampNode‘)->where($where)->select();
if(empty($arr)){
$info = Db::name(‘DomelampNode‘)->where(‘id‘,$pid)->find();

if($pid != 0)$this->smallarr[$info[‘id‘]][‘class‘][] = $info[‘name‘];
if(!isset($this->classnumber[$info[‘id‘]])){//分類數量
$this->classnumber[$info[‘id‘]]=1;
}
$this->getallarr($info[‘id‘],$info[‘pid‘]);
$this->getalllist($info[‘id‘]); //配件資訊
}else{
foreach($arr as $k=>$v){
$this->getexcelsmallarr($v[‘id‘]);
}
}
}

/*
*
* 匯出xls 擷取完整資料
*
* */
public function getallarr($k,$id = 0){
$info = Db::name(‘DomelampNode‘)->where(‘id‘,$id)->find();
if(isset($this->classnumber[$k])){//分類數量
$this->classnumber[$k]++;
}
array_unshift($this->smallarr[$k][‘class‘],$info[‘name‘]);
if($info[‘pid‘]>0){
$this->getallarr($k,$info[‘pid‘]);
}
}

/*
*
* 匯出xls 擷取方式(調用方法)
*
* */
public function getlist(){
$this->smallarr = array();
$this->getexcelsmallarr();
return [‘max_classnumber‘=> max($this->classnumber),‘list‘=>$this->alllist];
}


}


/*
*匯出excel方法 個人定義在common檔案下面
 * Excel表格匯出全部資料 可規定格式
* data = allinfo 全部分類資料匯出,已修改資料格式
* data = format 一般資料匯出, 資料為二維數組即可
* EXCEL 列如ABC 寬度個數 可根據實際情況設定
* */
function excelExport($fileName = ‘‘, $headArr = [], $data = [] ,$format = ‘‘) {

vendor (‘PHPExcel.PHPExcel‘);
$objPHPExcel = new \PHPExcel();

$objPHPExcel->getProperties();

$key = ord("A"); // 設定表頭

foreach ($headArr as $v) {

$colum = chr($key);

$objPHPExcel->setActiveSheetIndex(0)->setCellValue($colum . ‘1‘, $v);

$objPHPExcel->setActiveSheetIndex(0)->setCellValue($colum . ‘1‘, $v);

//設定列寬 新加
$objPHPExcel->getActiveSheet()->getColumnDimension(‘A‘)->setWidth(‘20‘);
$objPHPExcel->getActiveSheet()->getColumnDimension(‘B‘)->setWidth(‘20‘);
$objPHPExcel->getActiveSheet()->getColumnDimension(‘C‘)->setWidth(‘20‘);
$objPHPExcel->getActiveSheet()->getColumnDimension(‘D‘)->setWidth(‘20‘);
$objPHPExcel->getActiveSheet()->getColumnDimension(‘E‘)->setWidth(‘20‘);
$objPHPExcel->getActiveSheet()->getColumnDimension(‘F‘)->setWidth(‘20‘);
$objPHPExcel->getActiveSheet()->getColumnDimension(‘G‘)->setWidth(‘20‘);
$objPHPExcel->getActiveSheet()->getColumnDimension(‘H‘)->setWidth(‘20‘);

//下面注釋的這行代碼是讓表頭擁有篩選功能,根據需要取消注釋即可

//$objPHPExcel->getActiveSheet()->setAutoFilter($objPHPExcel->getActiveSheet()->calculateWorksheetDimension());

$key += 1;

}

$column = 2;

$objActSheet = $objPHPExcel->getActiveSheet();

if($format == ‘allinfo‘){
$classMax = $data[‘max_classnumber‘];
$list = $data[‘list‘];
foreach ($list as $k=>$row){
$class = $row[‘class‘];
$number = count($class);
if($number<$classMax){
$need = $classMax - $number;
for($i=0;$i<$need;$i++){
array_push($class,‘‘);
}
}
unset($row[‘class‘]);

$list[$k] = array_merge($class,$row);

$span = ord("A");

foreach ($list[$k] as $keyName => $value) { // 列寫入
$objActSheet->setCellValue(chr($span) . $column, $value);

$span++;

}

$column++;
}
}

if($format == ‘format‘){
foreach ($data as $key => $rows) { // 行寫入

$span = ord("A");

foreach ($rows as $keyName => $value) { // 列寫入

$objActSheet->setCellValue(chr($span) . $column, $value);

$span++;

}

$column++;

}
}


$fileName = iconv("utf-8", "gb2312", $fileName); // 重新命名表

$objPHPExcel->setActiveSheetIndex(0); // 設定活動單指數到第一個表,所以Excel開啟這是第一個表

header(‘Content-Type: application/vnd.ms-excel‘);

header("Content-Disposition: attachment;filename=‘$fileName‘");

header(‘Cache-Control: max-age=0‘);

$objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, ‘Excel5‘);

$objWriter->save(‘php://output‘); // 檔案通過瀏覽器下載

exit();

}


無限極分類+商品匯出excel(Thinkphp5,資料讀取無限極,個人限制匯出5級)

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.