当前位置:网站首页>PHP excel export function encapsulation (based on phpexcel class)
PHP excel export function encapsulation (based on phpexcel class)
2022-06-30 14:10:00 【Zhiqiu·】
1. Encapsulation method
<?php
namespace app\service;
class Excel
{
/** Excel export
* @param $list_arr Exported data
* @param $title_arr title ( One dimensional array )
* @param $width_arr The width of the title ( One dimensional array , and $title_arr Sequence correspondence )
* @param $field_arr The title corresponds to the field name ( One dimensional array , and $title_arr Sequence correspondence )
* @param $excel_title Export title
* @param $excel_total Final total
* @param $count_all_arr Each vertical line counts
*/
function set_excel($list_arr,$title_arr,$width_arr=array(),$field_arr,$excel_title,$excel_total="",$count_all_arr=''){
if(empty($list_arr) || empty($title_arr)){
die(' Parameter error ');
}
$num = array('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z',
'AA','AB','AC','AD','AE','AF','AG','AH','AI','AJ','AK','AL','AM','AN','AO','AP','AQ','AR','AS','AT','AU','AV','AW','AX','AY','AZ',
'BA','BB','BC','BD','BE','BF','BG','BH','BI','BJ','BK','BL','BM','BN','BO','BP','BQ','BR','BS','BT','BU','BV','BW','BX','BY','BZ'
);
require_once Env::get('ROOT_PATH')."vendor/PHPExcel/PHPExcel.php";
require_once Env::get('ROOT_PATH')."vendor/PHPExcel/PHPExcel/Worksheet/Drawing.php";
require_once Env::get('ROOT_PATH')."vendor/PHPExcel/PHPExcel/Writer/Excel2007.php";
$objPHPExcel = new \PHPExcel();
$objActSheet = $objPHPExcel->getActiveSheet();
for($i=0;$i<count($title_arr);$i++){
$objPHPExcel->setActiveSheetIndex(0)->getStyle($num[$i])->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$objPHPExcel->getActiveSheet()->getColumnDimension($num[$i])->setWidth($width_arr[$i]?$width_arr[$i]:30);
$objActSheet->setCellValue($num[$i].'1', $title_arr[$i]);
}
$objPHPExcel->getActiveSheet()->getStyle('A1:'.$num[count($title_arr)-1].'1')->applyFromArray(array('font' => array('bold' => true)));
foreach ($list_arr as $k=>$v){
$k += 2;
for($j=0;$j<count($title_arr);$j++){
$objActSheet->setCellValue($num[$j] . $k, $v[$field_arr[$j]]);
}
}
if($count_all_arr){
for($l=0;$l<count($count_all_arr);$l++){
$number = count($list_arr)+2;
$objActSheet->setCellValue($num[$l] . $number, $count_all_arr[$l]);
}
}
if($excel_total){
$objActSheet->setCellValue('A'.(count($list_arr)+2), " total ");
$cell = "B".(count($list_arr)+2).":D".(count($list_arr)+2);
$objActSheet->setCellValue('B'.(count($list_arr)+2), $excel_total)->mergeCells($cell);
}
$date = date("Y-m-d H:i:s", time());
$fileName = iconv("utf-8", "gb2312", $excel_title . "_{$date}.xls");
$objPHPExcel->setActiveSheetIndex(0);
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'); // The file is downloaded through the browser
}
}
2. Calling method
<?php
function excel(){
$title_arr = [' The order no. ',' The name of the restaurant ',' Dimension type ',' Ledger user ',' Amount incurred ',' Sub Ledger amount ',' Current balance ',' state ',' Trading hours ',' Split time '];
$width_arr = ['25','25','15','15','10','10','10','10','20','20'];
$field_arr = ['order_id','restaurant_name','fz_type_name','username','order_money','fz_money','current_balance','state_name','created_time','use_time'];
$excel = new Excel();
$excel->set_excel($list, $title_arr, $width_arr, $field_arr, ' Ledger records ','');
}边栏推荐
- mysql拒绝访问、管理员身份打开的
- 从控制层返回到js的json数据带“\”转译符,怎么去掉
- Meaning of while (~scanf ("%d%d", & A, & B))
- 优思学院:六西格玛不只是统计!
- 深入理解.Net中的线程同步之构造模式(二)内核模式3.内核模式构造物Mutex
- 【观察】智能产业加速,为何AI算力要先行?
- Introduction to the renewal of substrate source code: the pallet alliance is incorporated into the main line,
- Numpy creates an empty array data = np empty(shape=[1, 64,64,3])
- Configuration of headquarters dual computer hot standby and branch infrastructure for firewall Foundation
- Deep understanding Net (2) kernel mode 2 Kernel mode construct semaphone
猜你喜欢

提权扫描工具

What is erdma as illustrated by Coptic cartoon?

Jetpack Compose 实现完美屏幕适配
![[KALI] KALI系统、软件更新(附带镜像源)](/img/ac/43a3f81d50ab6866271b500b142252.png)
[KALI] KALI系统、软件更新(附带镜像源)
![[scientific research data processing] [basic] category variable frequency analysis chart, numerical variable distribution chart and normality test (including lognormal)](/img/70/8bf226964118efb324ca4d339df654.png)
[scientific research data processing] [basic] category variable frequency analysis chart, numerical variable distribution chart and normality test (including lognormal)

Deep understanding Net (2) kernel mode 2 Kernel mode construct semaphone

The programming competition is coming! B station surrounding, senior members and other good gifts to you!

MFQE 2.0: A New Approach for Multi-FrameQuality Enhancement on Compressed Video

There is no utf8 option for creating tables in Navicat database.

Heavyweight: the domestic ide was released, developed by Alibaba, and is completely open source!
随机推荐
Intelligent operation and maintenance: visual management system based on BIM Technology
[redis series] redis learning 16. Redis Dictionary (map) and its core coding structure
SQL attendance statistics monthly report
Why can't the database table be written into data
(8) JMeter component detailed once only controller
Directory related commands
MFQE 2.0: A New Approach for Multi-FrameQuality Enhancement on Compressed Video
[Title brushing] heater
More than 20 years after Hong Kong's return, Tupu digital twin Hong Kong Zhuhai Macao Bridge has shocked
About the problems encountered when using the timer class to stop with a button (why does the QPushButton (for the first time) need to be clicked twice to respond?)
Google Earth Engine(GEE)——GHSL:全球人类住区层,建成网格 1975-1990-2000-2015 (P2016) 数据集
Service online governance
How does MySQL merge columns?
目录相关命令
优思学院:六西格玛不只是统计!
随着产业互联网的发展,有关互联网的落地和应用也就变得宽阔了起来
Unity 频繁切换分支 结果模型出现莫名其妙的错误
Go common lock mutex and rwmutex
Step by step | help you easily submit Google play data security form
Prometheus 2.29.0 new features