php excel 操作
作者:互联网
<?php
class Excel {
public static function exportExcel($expTitle, $expCellName, $expTableData, $topData, $otherData) {
$xlsTitle = iconv('utf-8', 'gb2312', $expTitle);//文件名称
$fileName = $xlsTitle;//or $xlsTitle 文件名称可根据自己情况设定
$cellNum = count($expCellName);
$dataNum = count($expTableData);
$topNum = count($topData);
Vendor('PHPExcel.PHPExcel');
$objPHPExcel = new \PHPExcel();
$cellName = 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');
// zb 设置行高 宽度
$objPHPExcel->getActiveSheet()->getDefaultRowDimension()->setRowHeight(20);
foreach ($cellName as $cv){
$objPHPExcel->getActiveSheet()->getColumnDimension($cv)->setWidth(30);//#设置单元格宽度
}
$objPHPExcel->getActiveSheet(0)->mergeCells('A1:' . $cellName[$cellNum - 1] . '1');//合并单元格
$objPHPExcel->setActiveSheetIndex()->setCellValue('A1', $expTitle);
$objPHPExcel->getActiveSheet()->getStyle()->getFont()->setBold(true);
$objPHPExcel->getActiveSheet(0)->mergeCells('A2:' . $cellName[$cellNum - 1] . '2');
$objPHPExcel->setActiveSheetIndex()->setCellValue('A2', $otherData);
for ($i = 0, $iMax = count($topData); $i < $iMax; $i++) {
for ($j = 0, $jMax = count($topData[$i]); $j < $jMax; $j++) {
$objPHPExcel->setActiveSheetIndex(0)->setCellValue($cellName[$j] . ($i + 3), $topData[$i][$j]);
}
}
for ($i = 0; $i < $cellNum; $i++) {
$objPHPExcel->setActiveSheetIndex(0)->setCellValue($cellName[$i] . ($topNum + 3), $expCellName[$i][1]);
$objPHPExcel->getActiveSheet()->getStyle($cellName[$i] . ($topNum + 3))->getFont()->setBold(true);
}
// Miscellaneous glyphs, UTF-8
for ($i = 0; $i < $dataNum; $i++) {
for ($j = 0; $j < $cellNum; $j++) {
$objPHPExcel->getActiveSheet(0)->setCellValue($cellName[$j] . ($i + $topNum + 4), $expTableData[$i][$expCellName[$j][0]]);
}
}
header('pragma:public');
header('Content-type:application/vnd.ms-excel;charset=utf-8;name="' . $xlsTitle . '.xls"');
header("Content-Disposition:attachment;filename=$fileName.xls");//attachment新窗口打印inline本窗口打印
// Vendor('PHPExcel_IOFactory');
$objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');
exit;
}
/***
* 导出订单内容,其中一行合并几个单元格,由订单有几个商品绝定~~
* $expTitle '标题-导出excel表格文件名称,第一行标题内容~'
* $expCellName 'excel架构每栏目标题列表'
* $expTableData '表格数据内容'
* $topData '表格内容一些汇总内容---在附标题下~'
* $otherData '附属标题'
* $mergeParse '数据以某一字段,合并数列单元格'
*/
![](https://www.icode9.com/i/l/?n=20&i=blog/1202393/202005/1202393-20200518165731250-490828530.png)
public static function exportOrderExcel($expTitle, $expCellName, $expTableData, $topData, $otherData,$mergeParse = '') {
$xlsTitle = iconv('utf-8', 'gb2312', $expTitle);//文件名称
$fileName = $xlsTitle;//or $xlsTitle 文件名称可根据自己情况设定
$cellNum = count($expCellName);
$dataNum = count($expTableData);
$topNum = count($topData);
Vendor('PHPExcel.PHPExcel');
$objPHPExcel = new \PHPExcel();
$cellName = 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');
// zb 设置行高 宽度
$objPHPExcel->getActiveSheet()->getDefaultRowDimension()->setRowHeight(20);
foreach ($cellName as $cv){
$objPHPExcel->getActiveSheet()->getColumnDimension($cv)->setWidth(30);//#设置单元格宽度
}
$objPHPExcel->getActiveSheet(0)->mergeCells('A1:' . $cellName[$cellNum - 1] . '1');//合并单元格
$objPHPExcel->setActiveSheetIndex()->setCellValue('A1', $expTitle);
$objPHPExcel->getActiveSheet()->getStyle()->getFont()->setBold(true);
$objPHPExcel->getActiveSheet(0)->mergeCells('A2:' . $cellName[$cellNum - 1] . '2');
$objPHPExcel->setActiveSheetIndex()->setCellValue('A2', $otherData);
for ($i = 0, $iMax = count($topData); $i < $iMax; $i++) {
for ($j = 0, $jMax = count($topData[$i]); $j < $jMax; $j++) {
$objPHPExcel->setActiveSheetIndex(0)->setCellValue($cellName[$j] . ($i + 3), $topData[$i][$j]);
}
}
// 水平,垂直居中
$objPHPExcel
->getDefaultStyle()
->getAlignment()
->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER)
->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
for ($i = 0; $i < $cellNum; $i++) {
$objPHPExcel->setActiveSheetIndex(0)->setCellValue($cellName[$i] . ($topNum + 3), $expCellName[$i][1]);
$objPHPExcel->getActiveSheet()->getStyle($cellName[$i] . ($topNum + 3))->getFont()->setBold(true);
}
// Miscellaneous glyphs, UTF-8
for ($i = 0; $i < $dataNum; $i++)
{
for ($j = 0; $j < $cellNum; $j++)
{
$cell_count = count($expTableData[$i][$mergeParse]) ?: 1;
if($cell_count > 1){
if($expCellName[$j][0] == $mergeParse){
foreach($expTableData[$i][$mergeParse] as $k => $v){
$objPHPExcel->getActiveSheet(0)->setCellValue($cellName[$j] . ($i + $topNum + 4 + $k), $v['p_name'] . ' --' . $v['pnum'] . '件');
}
} else {
$merge_str = $cellName[$j] . ($i + $topNum + 4) . ':' . $cellName[$j] . ($i + $topNum + 4 + $cell_count - 1);
// setlog([$merge_str],[],'','gg.log');
$objPHPExcel->getActiveSheet(0)->mergeCells($merge_str);
$objPHPExcel->getActiveSheet(0)->setCellValue($cellName[$j] . ($i + $topNum + 4), $expTableData[$i][$expCellName[$j][0]]);
}
} else {
if($expCellName[$j][0] == $mergeParse)
{
foreach($expTableData[$i][$mergeParse] as $k => $v)
{
$objPHPExcel->getActiveSheet(0)->setCellValue($cellName[$j] . ($i + $topNum + 4 + $k), $v['p_name'] . ' --' . $v['pnum'] . '件');
}
} else {
$objPHPExcel->getActiveSheet(0)->setCellValue($cellName[$j] . ($i + $topNum + 4), $expTableData[$i][$expCellName[$j][0]]);
}
}
}
$topNum += $cell_count - 1;
}
header('pragma:public');
header('Content-type:application/vnd.ms-excel;charset=utf-8;name="' . $xlsTitle . '.xls"');
header("Content-Disposition:attachment;filename=$fileName.xls");//attachment新窗口打印inline本窗口打印
// Vendor('PHPExcel_IOFactory');
$objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');
exit;
}
![](https://www.icode9.com/i/l/?n=20&i=blog/1202393/202005/1202393-20200518170007929-1645668286.png)
/**
* 对比老的方法,数据头部标题,占据2行,或者1行,列有合并,行也有合并情况~~~
* 第六类,报告导出结果操作~~~~!!!
* $expTitle '标题-导出excel表格文件名称,第一行标题内容~'
* $expCellName 'excel架构每栏目标题列表'
* $expTableData '表格数据内容'
* $topData '表格内容一些汇总内容---在附标题下~'
* $otherData '附属标题'
*/
public static function exportExcelNew($expTitle, $expCellName, $expTableData, $topData, $otherData = '') {
$xlsTitle = iconv('utf-8', 'gb2312', $expTitle);//文件名称
$fileName = $xlsTitle; //or $xlsTitle 文件名称可根据自己情况设定
$cellNum = count($expCellName);
$dataNum = count($expTableData);
$topNum = count($topData);
Vendor('PHPExcel.PHPExcel');
$objPHPExcel = new \PHPExcel();
$cellName = 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');
// zb 设置行高 宽度
$objPHPExcel->getActiveSheet()->getDefaultRowDimension()->setRowHeight(20);
foreach ($cellName as $cv){
$objPHPExcel->getActiveSheet()->getColumnDimension($cv)->setWidth(30);//#设置单元格宽度
}
// 第一行
$objPHPExcel->getActiveSheet(0)->mergeCells('A1:' . $cellName[$cellNum - 1] . '1');//合并单元格
$objPHPExcel->setActiveSheetIndex()->setCellValue('A1', $expTitle);
$objPHPExcel->getActiveSheet()->getStyle()->getFont()->setBold(true);
// 第二行:二级标题,合并单元格;
if($otherData){
$objPHPExcel->getActiveSheet(0)->mergeCells('A2:' . $cellName[$cellNum - 1] . '2');
$objPHPExcel->setActiveSheetIndex()->setCellValue('A2', $otherData);
} else {
$topNum--;
}
// 标题下汇总消息~
for ($i = 0, $iMax = count($topData); $i < $iMax; $i++) {
for ($j = 0, $jMax = count($topData[$i]); $j < $jMax; $j++) {
$objPHPExcel->setActiveSheetIndex(0)->setCellValue($cellName[$j] . ($i + 3), $topData[$i][$j]);
}
}
// 水平,垂直居中
$objPHPExcel
->getDefaultStyle()
->getAlignment()
->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER)
->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
// dump($expCellName);
// exit;
// 表格每列黑色加粗标题内容~~~除非特殊说明:占据2列; 有要求,合并行...
for ($i = 0; $i < $cellNum; $i++)
{
if(isset($expCellName[$i][2])){
if($expCellName[$i][2] > 1){
$objPHPExcel->getActiveSheet(0)->mergeCells($cellName[$i] . ($topNum + 3) .':' . $cellName[$i + $expCellName[$i][2] - 1] . ($topNum + 3));
// big
$objPHPExcel->setActiveSheetIndex(0)->setCellValue($cellName[$i] . ($topNum + 3), $expCellName[$i][3]);
$objPHPExcel->getActiveSheet()->getStyle($cellName[$i] . ($topNum + 3))->getFont()->setBold(true);
// small
$objPHPExcel->setActiveSheetIndex(0)->setCellValue($cellName[$i] . ($topNum + 4), $expCellName[$i][1]);
$objPHPExcel->getActiveSheet()->getStyle($cellName[$i] . ($topNum + 4))->getFont()->setBold(true);
} else {
// small
$objPHPExcel->setActiveSheetIndex(0)->setCellValue($cellName[$i] . ($topNum + 4), $expCellName[$i][1]);
$objPHPExcel->getActiveSheet()->getStyle($cellName[$i] . ($topNum + 4))->getFont()->setBold(true);
}
} else {
$objPHPExcel->getActiveSheet(0)->mergeCells($cellName[$i] . ($topNum + 3) .':' . $cellName[$i] . ($topNum + 4));
$objPHPExcel->setActiveSheetIndex(0)->setCellValue($cellName[$i] . ($topNum + 3), $expCellName[$i][1]);
$objPHPExcel->getActiveSheet()->getStyle($cellName[$i] . ($topNum + 3))->getFont()->setBold(true);
}
}
// 表格数据主体内容~~~~Miscellaneous glyphs, UTF-8
for ($i = 0; $i < $dataNum; $i++) {
for ($j = 0; $j < $cellNum; $j++) {
$objPHPExcel->getActiveSheet(0)->setCellValue($cellName[$j] . ($i + $topNum + 5), $expTableData[$i][$expCellName[$j][0]]);
}
}
header('pragma:public');
header('Content-type:application/vnd.ms-excel;charset=utf-8;name="' . $xlsTitle . '.xls"');
header("Content-Disposition:attachment;filename=$fileName.xls");
// attachment新窗口打印inline本窗口打印
// Vendor('PHPExcel_IOFactory');
$objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');
exit;
}
//上传图片到本地服务器
public static function upload_pem($file, $wechat_type){
if($wechat_type == 1){
$tail = 'public/';
}else if($wechat_type == 2){
$tail = 'open/';
}
//得到文件名称
$name = $file['name'];
$type = strtolower(substr($name,strrpos($name,'.')+1));
$allow_type = array('pem');
if(!in_array($type, $allow_type)){
return false;
}
if(!is_uploaded_file($file['tmp_name'])){
return false;
}
$upload_path = realpath(__ROOT__) . "/site_upload/wxpay/" . $tail;
if (!is_dir($upload_path)){
@mkdir($upload_path, 0777, true);
}
if(move_uploaded_file($file['tmp_name'], $upload_path . $name)){
return "/site_upload/wxpay/" . $tail .$name;
}else{
return false;
}
}
}
标签:expCellName,objPHPExcel,excel,cellName,setCellValue,操作,php,getActiveSheet,topNum 来源: https://www.cnblogs.com/pansidong/p/12911604.html