// 引入包,利用composer安装PhpSpreadsheet到项目目录
composer require phpoffice/phpspreadsheet
// 读取
$file = $_FILES['file'];
$inputFileName = $file['tmp_name']; // 临时文件/永久文件 都行 多使用上传文件
$reader = new \PhpOffice\PhpSpreadsheet\Reader\Xlsx();
$spreadsheet = $reader->load($inputFileName);
dump($spreadsheet->getSheet(0)->toArray());
exit;
// $sheetCount = $spreadsheet->getSheetCount();
// // 将Excel文件的每个sheet的数据转化为数组
// $datas = [];
// for($j = 0;$j<$sheetCount;$j++){
// $datas[] = $spreadsheet->getSheet($j)->toArray();
// }
// echo "<pre>";
// var_export($datas);
// 导出
<?php
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
use PhpOffice\PhpSpreadsheet\Style\Alignment;
include_once "./vendor/autoload.php";
/**
* 数字转字母(类似excel列标)
* @param $index
* @param int $start
* @return string
*/
function intToChr($index, int $start = 65): string
{
$str = '';
if (floor($index / 26) > 0) {
$str .= intToChr(floor($index / 26) - 1);
}
return $str . chr($index % 26 + $start);
}
/**
* 每个sheet设置
* @param $spreadsheet
* @param $sheetIndex
* @param $data
*/
function handlingMultipleSheet($spreadsheet, $sheetIndex, $data)
{
// 创建sheet
$spreadsheet->createSheet();
// 设置当前的活动sheet
$activeSheet = $spreadsheet->setActiveSheetIndex($sheetIndex);
// 设置每个sheet中的名称 title
$activeSheet->setCellValue('A1', $data['title']);
// 这是你的数据键名
$rows = $data['rows'][0];
// 计算你所占的列数
$columnsCount = count($rows);
// 求k-v值的所占行数
$infoNum = ceil(count($data['info']) / 2);
// 下面的详细信息的开始行数
$infoStart = $infoNum + 2;
$cellName = [];
for ($column = 0; $column < $columnsCount; $column++) {
array_push($cellName, intToChr($column));
}
// 设置标题栏的名称
$sheet = $spreadsheet->getActiveSheet($sheetIndex)->setTitle($data['info']['姓名:']);
// 合并标题栏单元格
$spreadsheet->getActiveSheet($sheetIndex)->mergeCells('A1:' . $cellName[$columnsCount - 1] . '1');
// 设置标题栏的字体大小
$spreadsheet->getActiveSheet($sheetIndex)->getStyle('A1')->getFont()->setSize(20);
// 中部数据居中
$spreadsheet->getActiveSheet($sheetIndex)->getStyle('A1')->getAlignment()->setHorizontal(Alignment::HORIZONTAL_CENTER);
$spreadsheet->getActiveSheet($sheetIndex)->getStyle('A')->getAlignment()->setHorizontal(Alignment::HORIZONTAL_CENTER);
$spreadsheet->getActiveSheet($sheetIndex)->getStyle('B')->getAlignment()->setHorizontal(Alignment::HORIZONTAL_CENTER);
$spreadsheet->getActiveSheet($sheetIndex)->getStyle('C')->getAlignment()->setHorizontal(Alignment::HORIZONTAL_CENTER);
$spreadsheet->getActiveSheet($sheetIndex)->getStyle('D')->getAlignment()->setHorizontal(Alignment::HORIZONTAL_CENTER);
$spreadsheet->getActiveSheet($sheetIndex)->getStyle('E')->getAlignment()->setHorizontal(Alignment::HORIZONTAL_CENTER);
$spreadsheet->getActiveSheet($sheetIndex)->getStyle('F')->getAlignment()->setHorizontal(Alignment::HORIZONTAL_CENTER);
$spreadsheet->getActiveSheet($sheetIndex)->getStyle('G')->getAlignment()->setHorizontal(Alignment::HORIZONTAL_CENTER);
// 低部数据居中
$spreadsheet->getActiveSheet($sheetIndex)->getStyle($infoStart)->getAlignment()->setHorizontal(Alignment::HORIZONTAL_CENTER);
// 低部数据标题栏加粗
$spreadsheet->getActiveSheet($sheetIndex)->getStyle($infoStart)->getFont()->setBold(true);
// 低部数据信息循环
foreach ($data['rows'] as $key => $item) {
//循环设置单元格:
//$key+$infoStart,因为第一行是表头,所以写到表格时 从第数据行开始写
for ($i = 0; $i < $columnsCount; $i++) {
$sheet->setCellValue(intToChr($i) . ($key + $infoStart), $item[$i]);
// 固定列宽
$spreadsheet->getActiveSheet($sheetIndex)->getColumnDimension(intToChr($i))->setWidth(20);
}
}
// 中闻数据处理
$rowNumber = 1;
$infoIndex = 0;
foreach ($data['info'] as $key => $value) {
if ($infoIndex % 2 == 0) {
$rowNumber++;
$infoCellName1 = 'A' . $rowNumber;
$infoCellMegreRange = 'B' . $rowNumber . ':C' . $rowNumber;
$infoCellName2 = 'B' . $rowNumber;
} else {
$infoCellName1 = 'D' . $rowNumber;
$infoCellMegreRange = 'E' . $rowNumber . ':F' . $rowNumber;
$infoCellName2 = 'E' . $rowNumber;
}
$spreadsheet->setActiveSheetIndex($sheetIndex)->setCellValue($infoCellName1, $key);
$spreadsheet->getActiveSheet($sheetIndex)->mergeCells($infoCellMegreRange);
$spreadsheet->setActiveSheetIndex($sheetIndex)->setCellValue($infoCellName2, $value);
$infoIndex++;
}
}
/**
* 多sheet的导出
* @param $datas
* @throws \PhpOffice\PhpSpreadsheet\Writer\Exception
*/
function exportExecl($datas)
{
$name = '完成情况-' . date("Y-m-d", time());
$spreadsheet = new Spreadsheet();
foreach ($datas as $key => $data) {
handlingMultipleSheet($spreadsheet, $key, $data);
}
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="' . $name . '.xlsx"');
header('Cache-Control: max-age=0');
$writer = new Xlsx($spreadsheet);
$writer->save('php://output');
//删除清空:
$spreadsheet->disconnectWorksheets();
unset($spreadsheet);
exit;
}
// 要导出的数据
$datas = [
[
'title' => '张三完成情况',
'info' => [
'姓名:' => '张三',
'所在地:' => '北京市',
'收货地址:' => '河北',
'联系方式:' => '13900000000',
'日期:' => '2022-04-20',
'备注1:' => '优秀',
'备注2:' => '100分'
],
'rows' => [
[
'编号', '姓名', '性别', '手机', '备注3', '备注4', '备注5'
],
[
"AAA", "BBB", "CCC", "DDD", "EEE", "FFF", "GGG"
],
[
"AA", "BB", "CC", "DD", "EE", "FF", "GG"
]
]
], [
'title' => '李四完成情况',
'info' => [
'姓名:' => '李四',
'所在地:' => '北京市',
'收货地址:' => '河北',
'联系方式:' => '13900000000',
'日期:' => '2022-04-20',
'备注1:' => '优秀',
'备注2:' => '100分'
],
'rows' => [
[
'编号', '姓名', '性别', '手机', '备注3', '备注4', '备注5'
],
[
"1", "2", "3", "4", "5", "6", "7"
],
[
"11", "22", "33", "44", "55", "66", "77"
]
]
]
];
exportExecl($datas);
// 原文连接 https://www.cnblogs.com/fuqian/p/16170303.html
网友评论