使用新的excel库
parent
10ef323352
commit
74e46b7ab1
|
|
@ -32,7 +32,7 @@ class Excel
|
||||||
private $writer_type;
|
private $writer_type;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* [__construct 构造方法]
|
* 构造方法
|
||||||
* @author Devil
|
* @author Devil
|
||||||
* @blog http://gong.gg/
|
* @blog http://gong.gg/
|
||||||
* @version 0.0.1
|
* @version 0.0.1
|
||||||
|
|
@ -88,7 +88,40 @@ class Excel
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* [Export excel文件导出]
|
* 根据字段个数,设置表头排序字母
|
||||||
|
* @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文件导出
|
||||||
* @author Devil
|
* @author Devil
|
||||||
* @blog http://gong.gg/
|
* @blog http://gong.gg/
|
||||||
* @version 0.0.1
|
* @version 0.0.1
|
||||||
|
|
@ -104,98 +137,98 @@ class Excel
|
||||||
die;
|
die;
|
||||||
}
|
}
|
||||||
|
|
||||||
// excel对象
|
|
||||||
$excel = new \PHPExcel();
|
|
||||||
|
|
||||||
// 操作第一个工作表
|
|
||||||
$excel->setActiveSheetIndex(0);
|
|
||||||
|
|
||||||
// 文件输出类型
|
|
||||||
switch($this->file_type)
|
|
||||||
{
|
|
||||||
// PDF
|
|
||||||
case 'pdf':
|
|
||||||
$writer = PHPExcel_IOFactory::createWriter($excel, 'PDF');
|
|
||||||
$writer->setSheetIndex(0);
|
|
||||||
break;
|
|
||||||
|
|
||||||
// 默认EXCEL
|
|
||||||
default:
|
|
||||||
$writer = \PHPExcel_IOFactory::createWriter($excel, $this->writer_type);
|
|
||||||
}
|
|
||||||
|
|
||||||
// 获取配置编码类型
|
// 获取配置编码类型
|
||||||
$excel_charset = MyC('admin_excel_charset', 0);
|
$excel_charset = MyC('admin_excel_charset', 0);
|
||||||
$charset = MyConst('common_excel_charset_list')[$excel_charset]['value'];
|
$charset = MyConst('common_excel_charset_list')[$excel_charset]['value'];
|
||||||
|
|
||||||
// 水平,垂直居中
|
// 获取字母
|
||||||
if($this->horizontal_center == 1)
|
$letter_data = $this->GetLetterData();
|
||||||
{
|
|
||||||
$excel->getActiveSheet()->getDefaultStyle()->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
|
|
||||||
}
|
|
||||||
if($this->vertical_center == 1)
|
|
||||||
{
|
|
||||||
$excel->getActiveSheet()->getDefaultStyle()->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
|
|
||||||
}
|
|
||||||
|
|
||||||
//设置自动换行
|
// excel对象
|
||||||
if($this->warap_text == 1)
|
$spreadsheet = new \PhpOffice\PhpSpreadsheet\Spreadsheet();
|
||||||
{
|
$sheet = $spreadsheet->getActiveSheet();
|
||||||
$excel->getActiveSheet()->getDefaultStyle()->getAlignment()->setWrapText(true);
|
|
||||||
}
|
|
||||||
|
|
||||||
// 标题
|
// 标题
|
||||||
$temp_key = 0;
|
$temp_cum = 0;
|
||||||
|
$temp_row = 1;
|
||||||
foreach($this->title as $k=>$v)
|
foreach($this->title as $k=>$v)
|
||||||
{
|
{
|
||||||
$col = \PHPExcel_Cell::stringFromColumnIndex($temp_key).'1';
|
if(array_key_exists($temp_cum, $letter_data))
|
||||||
$excel->getActiveSheet()->setCellValue($col, ($excel_charset == 0) ? $v['name'] : iconv('utf-8', $charset, $v['name']));
|
{
|
||||||
$temp_key++;
|
$temp_letter = $letter_data[$temp_cum].$temp_row;
|
||||||
|
$value = ($excel_charset == 0) ? $v['name'] : iconv('utf-8', $charset, $v['name']);
|
||||||
|
$sheet->setCellValue($temp_letter, $value);
|
||||||
|
$sheet->getStyle($temp_letter)->getFont()->setBold(true);
|
||||||
|
$temp_cum++;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 内容
|
// 内容
|
||||||
|
$temp_row = 2;
|
||||||
foreach($this->data as $k=>$v)
|
foreach($this->data as $k=>$v)
|
||||||
{
|
{
|
||||||
$i = $k+2;
|
|
||||||
if(is_array($v) && !empty($v))
|
if(is_array($v) && !empty($v))
|
||||||
{
|
{
|
||||||
$temp_key = 0;
|
$temp_cum = 0;
|
||||||
foreach($this->title as $tk=>$tv)
|
foreach($this->title as $tk=>$tv)
|
||||||
{
|
{
|
||||||
$height = isset($tv['height']) ? intval($tv['height']) : 0;
|
if(array_key_exists($temp_cum, $letter_data))
|
||||||
$width = isset($tv['width']) ? intval($tv['width']) : $height;
|
|
||||||
$col = \PHPExcel_Cell::stringFromColumnIndex($temp_key);
|
|
||||||
if($tv['type'] == 'images')
|
|
||||||
{
|
{
|
||||||
$drawing = new \PHPExcel_Worksheet_Drawing();
|
$temp_letter = $letter_data[$temp_cum];
|
||||||
$drawing->setPath($v[$tk]);
|
$height = isset($tv['height']) ? intval($tv['height']) : 0;
|
||||||
|
$width = isset($tv['width']) ? intval($tv['width']) : $height;
|
||||||
|
if($tv['type'] == 'images')
|
||||||
|
{
|
||||||
|
$drawing = new \PhpOffice\PhpSpreadsheet\Worksheet\Drawing();
|
||||||
|
$drawing->setPath($v[$tk]);
|
||||||
|
|
||||||
// 设置宽度高度
|
// 设置宽度高度
|
||||||
$number = empty($height) ? 50 : $height-10;
|
$number = empty($height) ? 50 : $height-10;
|
||||||
$drawing->setHeight($number);
|
$drawing->setHeight($number);
|
||||||
$drawing->setWidth($number);
|
$drawing->setWidth($number);
|
||||||
$drawing->setCoordinates($col.$i);
|
$drawing->setCoordinates($temp_letter.$temp_row);
|
||||||
|
|
||||||
// 图片偏移距离
|
// 图片偏移距离
|
||||||
$x = ($width > 0) ? (($width-$number)/2)+15 : 15;
|
$x = ($width > 0) ? (($width-$number)/2)+15 : 15;
|
||||||
$drawing->setOffsetX($x);
|
$drawing->setOffsetX($x);
|
||||||
$drawing->setOffsetY(15);
|
$drawing->setOffsetY(15);
|
||||||
$drawing->setWorksheet($excel->getActiveSheet());
|
$drawing->setWorksheet($spreadsheet->getActiveSheet());
|
||||||
} else {
|
} else {
|
||||||
$excel->getActiveSheet()->setCellValueExplicit($col.$i, ($excel_charset == 0) ? $v[$tk] : iconv('utf-8', $charset, $v[$tk]), \PHPExcel_Cell_DataType::TYPE_STRING);
|
$value = ($excel_charset == 0) ? $v[$tk] : iconv('utf-8', $charset, $v[$tk]);
|
||||||
|
$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++;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 单元格宽高
|
// 行高度
|
||||||
if($width > 0)
|
|
||||||
{
|
|
||||||
$excel->getActiveSheet()->getColumnDimension($col)->setWidth($width/5);
|
|
||||||
}
|
|
||||||
if($height > 0)
|
if($height > 0)
|
||||||
{
|
{
|
||||||
$excel->getActiveSheet()->getRowDimension($i)->setRowHeight($height);
|
$spreadsheet->getActiveSheet()->getRowDimension($temp_row)->setRowHeight($height);
|
||||||
}
|
}
|
||||||
$temp_key++;
|
|
||||||
}
|
}
|
||||||
|
$temp_row++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -205,7 +238,7 @@ class Excel
|
||||||
ob_clean();
|
ob_clean();
|
||||||
}
|
}
|
||||||
|
|
||||||
// 头部
|
// 头部
|
||||||
header('Pragma: public');
|
header('Pragma: public');
|
||||||
header('Expires: 0');
|
header('Expires: 0');
|
||||||
header('Cache-Control:must-revalidate, post-check=0, pre-check=0');
|
header('Cache-Control:must-revalidate, post-check=0, pre-check=0');
|
||||||
|
|
@ -215,11 +248,12 @@ class Excel
|
||||||
header('Content-Type:application/download');
|
header('Content-Type:application/download');
|
||||||
header('Content-Disposition:attachment;filename='.$this->filename.'.'.$this->suffix);
|
header('Content-Disposition:attachment;filename='.$this->filename.'.'.$this->suffix);
|
||||||
header('Content-Transfer-Encoding:binary');
|
header('Content-Transfer-Encoding:binary');
|
||||||
|
$writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, ucfirst($this->suffix));
|
||||||
$writer->save('php://output');
|
$writer->save('php://output');
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* [Import excel文件导入]
|
* excel文件导入
|
||||||
* @author Devil
|
* @author Devil
|
||||||
* @blog http://gong.gg/
|
* @blog http://gong.gg/
|
||||||
* @version 0.0.1
|
* @version 0.0.1
|
||||||
|
|
@ -236,44 +270,44 @@ class Excel
|
||||||
}
|
}
|
||||||
$file = empty($file) ? $_FILES['file']['tmp_name'] : $file;
|
$file = empty($file) ? $_FILES['file']['tmp_name'] : $file;
|
||||||
|
|
||||||
// 取得文件基础数据
|
// 取得文件基础数据及类型判断
|
||||||
$reader = \PHPExcel_IOFactory::createReader($this->writer_type);
|
$extension = empty($_FILES['file']['name']) ? 'xlsx' : substr($_FILES['file']['name'], strripos($_FILES['file']['name'], '.')+1);
|
||||||
$excel = $reader->load($file);
|
if(!in_array($extension, ['csv', 'xls', 'xlsx']))
|
||||||
|
{
|
||||||
// 取得总行数
|
return DataReturn('无效的excel类型文件', -1);
|
||||||
$worksheet = $excel->getActiveSheet();
|
}
|
||||||
|
if('csv' == $extension)
|
||||||
// 取得总列数
|
{
|
||||||
$highest_row = $worksheet->getHighestRow();
|
$reader = new \PhpOffice\PhpSpreadsheet\Reader\Csv();
|
||||||
|
} else if('xls' == $extension)
|
||||||
// 取得最高的列
|
{
|
||||||
$highest_column = $worksheet->getHighestColumn();
|
$reader = new \PhpOffice\PhpSpreadsheet\Reader\Xls();
|
||||||
|
} else {
|
||||||
// 总列数
|
$reader = new \PhpOffice\PhpSpreadsheet\Reader\Xlsx();
|
||||||
$highest_column_index = \PHPExcel_Cell::columnIndexFromString($highest_column);
|
}
|
||||||
|
$spreadsheet = $reader->load($file);
|
||||||
|
|
||||||
// 定义变量
|
// 定义变量
|
||||||
$data = [];
|
$data = [];
|
||||||
$title = [];
|
$title = [];
|
||||||
|
$sheet = $spreadsheet->getActiveSheet();
|
||||||
// 读取数据
|
foreach($sheet->getRowIterator(1) as $rk=>$row)
|
||||||
for($row=1; $row<=$highest_row; $row++)
|
|
||||||
{
|
{
|
||||||
// 注意 highest_column_index 的列数索引从0开始
|
$tmp = [];
|
||||||
$info = [];
|
foreach($row->getCellIterator() as $cell)
|
||||||
for($col = 0; $col < $highest_column_index; $col++)
|
|
||||||
{
|
{
|
||||||
$value = trim($worksheet->getCellByColumnAndRow($col, $row)->getFormattedValue());
|
$value = $cell->getFormattedValue();
|
||||||
if($row == 1)
|
if($rk == 1)
|
||||||
{
|
{
|
||||||
$title[] = $value;
|
$title[] = $value;
|
||||||
} else {
|
} else {
|
||||||
$info[] = $value;
|
$tmp[] = $value;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if($row > 1)
|
// 避免正行为空
|
||||||
|
if(count(array_filter($tmp)) > 0)
|
||||||
{
|
{
|
||||||
$data[] = $info;
|
$data[] = $tmp;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
$result = [
|
$result = [
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue