Files
shopxo/extend/base/Excel.php

524 lines
17 KiB
PHP
Raw Permalink Normal View History

2018-12-28 18:58:37 +08:00
<?php
// +----------------------------------------------------------------------
// | ShopXO 国内领先企业级B2C免费开源电商系统
// +----------------------------------------------------------------------
2021-03-16 10:34:52 +08:00
// | Copyright (c) 2011~2099 http://shopxo.net All rights reserved.
2018-12-28 18:58:37 +08:00
// +----------------------------------------------------------------------
2021-03-16 10:34:52 +08:00
// | Licensed ( https://opensource.org/licenses/mit-license.php )
2018-12-28 18:58:37 +08:00
// +----------------------------------------------------------------------
// | Author: Devil
// +----------------------------------------------------------------------
namespace base;
/**
* Excel驱动
* @author Devil
* @blog http://gong.gg/
* @version 0.0.1
* @datetime 2017-01-10T21:51:08+0800
*/
class Excel
{
private $filename;
private $file_type;
private $suffix;
private $data;
private $title;
private $jump_url;
private $msg;
2019-09-30 15:43:16 +08:00
private $horizontal_center;
private $vertical_center;
private $warap_text;
2026-06-02 16:15:31 +08:00
private $multi_sheets;
2018-12-28 18:58:37 +08:00
/**
2022-01-10 23:59:50 +08:00
* 构造方法
2018-12-28 18:58:37 +08:00
* @author Devil
* @blog http://gong.gg/
* @version 0.0.1
* @datetime 2017-01-10T15:09:17+0800
2019-09-30 15:43:16 +08:00
* @param [string] $params['filename'] [文件名称(追加当前时间)]
* @param [string] $params['suffix'] [文件后缀名默认xls]
* @param [string] $params['jump_url'] [出错跳转url地址默认上一个页面]
* @param [string] $params['msg'] [错误提示信息]
* @param [string] $params['file_type'] [导出文件类型默认excel]
* @param [array] $params['title'] [标题(二维数组)]
* @param [array] $params['data'] [数据(二维数组)]
2026-06-02 16:15:31 +08:00
* @param [array] $params['multi_sheets'] [多工作表:每项含 name、headers、rows配合 ExportMultiSheet]
2019-09-30 15:43:16 +08:00
* @param [int] $params['horizontal_center'] [是否水平居中 1]
* @param [int] $params['vertical_center'] [是否垂直居中 1]
* @param [int] $params['warap_text'] [是否内容自动换行 1]
2018-12-28 18:58:37 +08:00
*/
2019-09-30 15:43:16 +08:00
public function __construct($params = [])
2018-12-28 18:58:37 +08:00
{
// 文件名称
$date = date('YmdHis');
2019-09-30 15:43:16 +08:00
$this->filename = isset($params['filename']) ? $params['filename'].'-'.$date : $date;
2018-12-28 18:58:37 +08:00
// 文件类型, 默认excel
$type_all = array('excel' => 'vnd.ms-excel', 'pdf' => 'pdf');
2019-09-30 15:43:16 +08:00
$this->file_type = (isset($params['file_type']) && isset($type_all[$params['file_type']])) ? $type_all[$params['file_type']] : $type_all['excel'];
2018-12-28 18:58:37 +08:00
// 文件后缀名称
2019-09-30 15:43:16 +08:00
$this->suffix = empty($params['suffix']) ? 'xls' : $params['suffix'];
2018-12-28 18:58:37 +08:00
// 标题
2019-09-30 15:43:16 +08:00
$this->title = isset($params['title']) ? $params['title'] : [];
2018-12-28 18:58:37 +08:00
// 数据
2019-09-30 15:43:16 +08:00
$this->data = isset($params['data']) ? $params['data'] : [];
2018-12-28 18:58:37 +08:00
// 出错跳转地址
2022-01-11 00:16:08 +08:00
$this->jump_url = empty($params['jump_url']) ? (empty($_SERVER['HTTP_REFERER']) ? __MY_URL__ : htmlspecialchars($_SERVER['HTTP_REFERER'])) : $params['jump_url'];
2018-12-28 18:58:37 +08:00
// 错误提示信息
2019-09-30 15:43:16 +08:00
$this->msg = empty($params['msg']) ? 'title or data cannot be empty!' : $params['msg'];
// 水平,垂直居中
$this->horizontal_center = isset($params['horizontal_center']) ? intval($params['horizontal_center']) : 1;
$this->vertical_center = isset($params['vertical_center']) ? intval($params['vertical_center']) : 1;
// 内容自动换行
$this->warap_text = isset($params['warap_text']) ? intval($params['warap_text']) : 1;
2026-06-02 16:15:31 +08:00
// 多工作表xlsx/xls见 ExportMultiSheet
$this->multi_sheets = isset($params['multi_sheets']) ? $params['multi_sheets'] : null;
}
/**
* 多工作表 Excel 导出PhpSpreadsheet ExportExcel 同编码与响应头)
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2026-04-14
* @desc 构造参数 multi_sheets 每项name 工作表名、headers 表头一维数组、rows 二维行数据(与 headers 列序一致)
*/
public function ExportMultiSheet()
{
if(empty($this->multi_sheets) || !is_array($this->multi_sheets))
{
$this->title = [];
$this->data = [];
$this->msg = empty($this->msg) ? 'multi_sheets cannot be empty!' : $this->msg;
$this->IsErrorCheck();
}
$excel_charset = MyC('admin_excel_charset', 0);
$charset = MyConst('common_excel_charset_list')[$excel_charset]['value'];
$spreadsheet = new \PhpOffice\PhpSpreadsheet\Spreadsheet();
$is_first = true;
foreach($this->multi_sheets as $spec)
{
$sheet_name = isset($spec['name']) ? (string) $spec['name'] : 'Sheet';
$headers = isset($spec['headers']) && is_array($spec['headers']) ? $spec['headers'] : [];
$rows = isset($spec['rows']) && is_array($spec['rows']) ? $spec['rows'] : [];
$sheet = $is_first ? $spreadsheet->getActiveSheet() : $spreadsheet->createSheet();
$is_first = false;
$sheet->setTitle(mb_substr($sheet_name, 0, 31, 'UTF-8'));
$col_count = count($headers);
for($c = 0; $c < $col_count; $c++)
{
$h = isset($headers[$c]) ? $headers[$c] : '';
$value = ($excel_charset == 0) ? $h : mb_convert_encoding((string) $h, $charset, 'utf-8');
$sheet->setCellValueByColumnAndRow($c + 1, 1, $value);
$sheet->getStyleByColumnAndRow($c + 1, 1)->getFont()->setBold(true);
}
$rnum = 2;
foreach($rows as $row)
{
if(!is_array($row))
{
$row = [];
}
$row = array_values($row);
for($c = 0; $c < $col_count; $c++)
{
$cell = array_key_exists($c, $row) ? $row[$c] : '';
if(is_array($cell) || is_object($cell))
{
$cell = '';
}
$val = ($excel_charset == 0) ? (string) $cell : mb_convert_encoding((string) $cell, $charset, 'utf-8');
$sheet->setCellValueByColumnAndRow($c + 1, $rnum, $val);
}
$rnum++;
}
for($c = 1; $c <= $col_count; $c++)
{
$sheet->getColumnDimensionByColumn($c)->setAutoSize(true);
}
}
$spreadsheet->setActiveSheetIndex(0);
if(ob_get_length() > 0)
{
ob_clean();
}
header('Pragma: public');
header('Expires: 0');
header('Cache-Control:must-revalidate, post-check=0, pre-check=0');
header('Content-Type:application/force-download');
header('Content-Type:application/'.$this->file_type.';charset='.$charset);
header('Content-Type:application/octet-stream');
header('Content-Type:application/download');
header('Content-Disposition:attachment;filename='.$this->filename.'.'.$this->suffix);
header('Content-Transfer-Encoding:binary');
$writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, ucfirst($this->suffix));
$writer->save('php://output');
exit;
2018-12-28 18:58:37 +08:00
}
2022-06-22 22:14:24 +08:00
/**
2022-07-08 22:12:45 +08:00
* 文件导出
* @author Devil
* @blog http://gong.gg/
* @version 0.0.1
* @datetime 2017-01-10T15:12:01+0800
*/
public function Export()
{
// 错误校验
$this->IsErrorCheck();
2022-07-08 22:12:45 +08:00
// 导出类型
$export_type = MyC('common_excel_export_type', 0, true);
if($export_type == 0)
{
$this->ExportCsv();
} else {
$this->ExportExcel();
}
}
/**
* csv导出文件
2022-06-22 22:14:24 +08:00
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2022-06-22
* @desc description
*/
public function ExportCsv()
{
// 获取配置编码类型
$excel_charset = MyC('admin_excel_charset', 0);
2023-08-27 16:59:15 +08:00
$charset = MyConst('common_excel_charset_list')[$excel_charset]['value'];
2022-06-22 22:14:24 +08:00
// 拼接文件信息,这里注意两点
// 1、字段与字段之间用逗号分隔开
// 2、行与行之间需要换行符
// 3、英文逗号替换未中文逗号、避免与csv分隔符冲突
$csv_title = implode(',', array_map(function($v) {
return str_replace([',', "\n"], ['', ''], $v['name']);
}, $this->title));
2022-10-26 15:43:09 +08:00
$csv_content = (($excel_charset == 0) ? $csv_title : mb_convert_encoding($csv_title, $charset, 'utf-8'))."\n";
2022-06-22 22:14:24 +08:00
foreach($this->data as $v)
{
$temp = '';
$index = 0;
foreach($this->title as $tk=>$tv)
{
2024-07-03 22:46:04 +08:00
$temp .= ($index == 0 ? '' : ',').((array_key_exists($tk, $v) && !is_array($v[$tk]) && !empty($v[$tk])) ? str_replace([',', "\n"], [ '', ''], $v[$tk]) : '')."\t";
2022-06-22 22:14:24 +08:00
$index++;
}
2022-10-26 15:43:09 +08:00
$csv_content .= (($excel_charset == 0) ? $temp : mb_convert_encoding($temp, $charset, 'utf-8'))."\n";
2022-06-22 22:14:24 +08:00
}
// 头信息设置
header("Content-type:text/csv");
header("Content-Disposition:attachment;filename=" . $this->filename.'.csv');
header('Cache-Control:must-revalidate,post-check=0,pre-check=0');
header('Expires:0');
header('Pragma:public');
echo $csv_content;
exit;
}
2018-12-28 18:58:37 +08:00
/**
2022-01-10 23:59:50 +08:00
* 根据字段个数,设置表头排序字母
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2022-01-10
* @desc description
*/
public function GetLetterData()
{
$letter_str = '';
if(!empty($this->title) && is_array($this->title))
{
$count = count($this->title);
for($i='A',$k=0; $i<='Z'; $i++, $k++)
{
if($k == $count)
{
break;
}
// 最后一个取消逗号
if($k == ($count-1))
{
$letter_str .= $i;
} else {
$letter_str .= $i.',';
}
}
}
return explode(',', $letter_str);
}
/**
* excel文件导出
2018-12-28 18:58:37 +08:00
* @author Devil
* @blog http://gong.gg/
* @version 0.0.1
* @datetime 2017-01-10T15:12:01+0800
*/
2022-07-08 22:12:45 +08:00
public function ExportExcel()
2018-12-28 18:58:37 +08:00
{
// 获取配置编码类型
$excel_charset = MyC('admin_excel_charset', 0);
2023-08-27 16:59:15 +08:00
$charset = MyConst('common_excel_charset_list')[$excel_charset]['value'];
2018-12-28 18:58:37 +08:00
2022-01-10 23:59:50 +08:00
// 获取字母
$letter_data = $this->GetLetterData();
2019-09-30 15:43:16 +08:00
2022-01-10 23:59:50 +08:00
// excel对象
$spreadsheet = new \PhpOffice\PhpSpreadsheet\Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
2019-09-30 15:43:16 +08:00
2018-12-28 18:58:37 +08:00
// 标题
2022-01-10 23:59:50 +08:00
$temp_cum = 0;
$temp_row = 1;
2019-01-21 12:24:50 +08:00
foreach($this->title as $k=>$v)
2018-12-28 18:58:37 +08:00
{
2022-01-10 23:59:50 +08:00
if(array_key_exists($temp_cum, $letter_data))
{
$temp_letter = $letter_data[$temp_cum].$temp_row;
2022-10-26 15:43:09 +08:00
$value = ($excel_charset == 0) ? $v['name'] : mb_convert_encoding($v['name'], $charset, 'utf-8');
2022-01-10 23:59:50 +08:00
$sheet->setCellValue($temp_letter, $value);
$sheet->getStyle($temp_letter)->getFont()->setBold(true);
$temp_cum++;
}
2018-12-28 18:58:37 +08:00
}
2022-01-10 23:59:50 +08:00
2018-12-28 18:58:37 +08:00
// 内容
2022-01-10 23:59:50 +08:00
$temp_row = 2;
2018-12-28 18:58:37 +08:00
foreach($this->data as $k=>$v)
{
if(is_array($v) && !empty($v))
{
2022-01-10 23:59:50 +08:00
$temp_cum = 0;
2018-12-28 18:58:37 +08:00
foreach($this->title as $tk=>$tv)
{
2022-01-10 23:59:50 +08:00
if(array_key_exists($temp_cum, $letter_data))
2019-09-30 15:43:16 +08:00
{
2022-01-10 23:59:50 +08:00
$temp_letter = $letter_data[$temp_cum];
$height = isset($tv['height']) ? intval($tv['height']) : 0;
$width = isset($tv['width']) ? intval($tv['width']) : $height;
if($tv['type'] == 'images' && !empty($v[$tk]))
2022-01-10 23:59:50 +08:00
{
$drawing = new \PhpOffice\PhpSpreadsheet\Worksheet\Drawing();
$drawing->setPath($v[$tk]);
// 设置宽度高度
$number = empty($height) ? 50 : $height-10;
$drawing->setHeight($number);
$drawing->setWidth($number);
$drawing->setCoordinates($temp_letter.$temp_row);
// 图片偏移距离
$x = ($width > 0) ? (($width-$number)/2)+15 : 15;
$drawing->setOffsetX($x);
$drawing->setOffsetY(15);
$drawing->setWorksheet($spreadsheet->getActiveSheet());
} else {
2022-10-26 15:43:09 +08:00
$value = (array_key_exists($tk, $v) && !is_array($v[$tk])) ? (($excel_charset == 0) ? $v[$tk] : mb_convert_encoding($v[$tk], $charset, 'utf-8')) : '';
2022-01-10 23:59:50 +08:00
$sheet->setCellValueByColumnAndRow($temp_cum+1, $temp_row, $value);
}
// 单元格宽高
if($width > 0)
{
$spreadsheet->getActiveSheet()->getColumnDimension($temp_letter)->setWidth($width/5);
}
// 水平,垂直居中
if($this->horizontal_center == 1)
{
$spreadsheet->getActiveSheet()->getStyle($temp_letter)->getAlignment()->setHorizontal(\PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER);
}
if($this->vertical_center == 1)
{
$spreadsheet->getActiveSheet()->getStyle($temp_letter)->getAlignment()->setVertical(\PhpOffice\PhpSpreadsheet\Style\Alignment::VERTICAL_CENTER);
}
// 自动换行
if($this->warap_text == 1)
{
$sheet->getStyle($temp_letter)->getAlignment()->setWrapText(true);
}
$temp_cum++;
2019-09-30 15:43:16 +08:00
}
2022-01-10 23:59:50 +08:00
// 行高度
2019-09-30 15:43:16 +08:00
if($height > 0)
{
2022-01-10 23:59:50 +08:00
$spreadsheet->getActiveSheet()->getRowDimension($temp_row)->setRowHeight($height);
2019-09-30 15:43:16 +08:00
}
2018-12-28 18:58:37 +08:00
}
2022-01-10 23:59:50 +08:00
$temp_row++;
2018-12-28 18:58:37 +08:00
}
}
2020-11-23 22:34:25 +08:00
// 丢弃输出缓冲区中的内容
if(ob_get_length() > 0)
{
ob_clean();
}
2022-01-10 23:59:50 +08:00
// 头部
2018-12-28 18:58:37 +08:00
header('Pragma: public');
header('Expires: 0');
header('Cache-Control:must-revalidate, post-check=0, pre-check=0');
header('Content-Type:application/force-download');
2021-07-22 11:41:57 +08:00
header('Content-Type:application/'.$this->file_type.';charset='.$charset);
2018-12-28 18:58:37 +08:00
header('Content-Type:application/octet-stream');
header('Content-Type:application/download');
header('Content-Disposition:attachment;filename='.$this->filename.'.'.$this->suffix);
header('Content-Transfer-Encoding:binary');
2022-01-10 23:59:50 +08:00
$writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, ucfirst($this->suffix));
2018-12-28 18:58:37 +08:00
$writer->save('php://output');
}
/**
2022-01-10 23:59:50 +08:00
* excel文件导入
2018-12-28 18:58:37 +08:00
* @author Devil
* @blog http://gong.gg/
* @version 0.0.1
* @datetime 2017-04-06T18:18:55+0800
2025-09-23 21:22:38 +08:00
* @param [string] $file [文件位置,空则读取全局excel的临时文件]
* @param [array] $params [输入参数]
* @return [array] [数据列表]
2018-12-28 18:58:37 +08:00
*/
2025-09-23 21:22:38 +08:00
public function Import($file = '', $params = [])
2018-12-28 18:58:37 +08:00
{
// 文件为空则取全局文变量excel的临时文件
2020-09-08 23:36:30 +08:00
if(empty($file) && (empty($_FILES['file']) || empty($_FILES['file']['tmp_name'])))
2018-12-28 18:58:37 +08:00
{
2023-02-12 22:14:47 +08:00
return DataReturn(MyLang('common_extend.base.excel.file_empty_tips'), -1);
2018-12-28 18:58:37 +08:00
}
2020-09-08 23:36:30 +08:00
$file = empty($file) ? $_FILES['file']['tmp_name'] : $file;
2018-12-28 18:58:37 +08:00
2022-01-10 23:59:50 +08:00
// 取得文件基础数据及类型判断
$extension = empty($_FILES['file']['name']) ? 'xlsx' : substr($_FILES['file']['name'], strripos($_FILES['file']['name'], '.')+1);
if(!in_array($extension, ['csv', 'xls', 'xlsx']))
{
2023-02-12 22:14:47 +08:00
return DataReturn(MyLang('common_extend.base.excel.excel_format_error_tips'), -1);
2022-01-10 23:59:50 +08:00
}
if('csv' == $extension)
{
$reader = new \PhpOffice\PhpSpreadsheet\Reader\Csv();
} else if('xls' == $extension)
{
$reader = new \PhpOffice\PhpSpreadsheet\Reader\Xls();
} else {
$reader = new \PhpOffice\PhpSpreadsheet\Reader\Xlsx();
}
$spreadsheet = $reader->load($file);
2018-12-28 18:58:37 +08:00
2025-09-23 21:22:38 +08:00
// 是否指定工作空间
if(isset($params['space']))
{
// 获取工作空间
$space = $spreadsheet->getSheetNames();
// 不存在工作空间或者指定的不存在则默认第一个
if(empty($space) || !isset($space[$params['space']]))
{
$params['space'] = 0;
}
// 选择工作表
$sheet = $spreadsheet->getSheet($params['space']);
} else {
// 获取一个Excel文件中活跃的工作表
$sheet = $spreadsheet->getActiveSheet();
}
2018-12-28 18:58:37 +08:00
// 定义变量
2020-09-08 23:36:30 +08:00
$data = [];
$title = [];
2022-01-10 23:59:50 +08:00
foreach($sheet->getRowIterator(1) as $rk=>$row)
2018-12-28 18:58:37 +08:00
{
2022-01-10 23:59:50 +08:00
$tmp = [];
foreach($row->getCellIterator() as $cell)
2018-12-28 18:58:37 +08:00
{
2022-01-10 23:59:50 +08:00
$value = $cell->getFormattedValue();
if($rk == 1)
2018-12-28 18:58:37 +08:00
{
2020-09-08 23:36:30 +08:00
$title[] = $value;
2018-12-28 18:58:37 +08:00
} else {
2022-01-10 23:59:50 +08:00
$tmp[] = $value;
2018-12-28 18:58:37 +08:00
}
}
2022-01-10 23:59:50 +08:00
// 避免正行为空
if(count(array_filter($tmp)) > 0)
2018-12-28 18:58:37 +08:00
{
2022-01-10 23:59:50 +08:00
$data[] = $tmp;
2018-12-28 18:58:37 +08:00
}
}
2020-09-08 23:36:30 +08:00
$result = [
'title' => $title,
'data' => $data,
];
2023-02-12 22:14:47 +08:00
return DataReturn(MyLang('handle_success'), 0, $result);
2018-12-28 18:58:37 +08:00
}
/**
* 错误校验
* @author Devil
* @blog http://gong.gg/
* @version 1.0.0
* @date 2022-09-20
* @desc description
*/
public function IsErrorCheck()
{
// 是否有数据
if(empty($this->title) && empty($this->data))
{
die('<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
2023-02-12 22:14:47 +08:00
<title>'.MyLang('common_extend.base.excel.error_title').'</title>
</head>
<body style="text-align:center;">
<p style="color:#666;font-size:14px;margin-top:10%;margin-bottom:30px;">'.$this->msg.'</p>
2023-02-12 22:14:47 +08:00
<a href="javascript:;" onClick="WindowClose()" style="text-decoration:none;color:#fff;background:#f00;padding:5px 15px;border-radius:2px;font-size:12px;">'.MyLang('common_extend.base.excel.close_page_title').'</a>
</body>
<script type="text/javascript">
function WindowClose()
{
var user_agent = navigator.userAgent;
if(user_agent.indexOf("Firefox") != -1 || user_agent.indexOf("Chrome") != -1)
{
location.href = "about:blank";
} else {
window.opener = null;
window.open("", "_self");
}
window.close();
}
</script>
</html>');
}
}
2018-12-28 18:58:37 +08:00
}
?>