composer require phpoffice/phpspreadsheet
# excel
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
use PhpOffice\PhpSpreadsheet\IOFactory;
# 实例化 Spreadsheet 对象
$spreadsheet = new Spreadsheet();
# 获取活动工作薄
$sheet = $spreadsheet->getActiveSheet();
# 赋值
$sheet->setCellValue('A1','10');
$sheet->setCellValue('B1','15');
$sheet->setCellValue('C1','20');
$sheet->setCellValue('A2', '总数:');
# 公式,SUM(A1:C1),就是计算A1到C1的合
$sheet->setCellValue('B2', '=SUM(A1:C1)');
# 行列
$num = 4;
// 批量赋值
$sheet->setCellValue('A3','ID');
$sheet->setCellValue('B3','姓名');
$sheet->setCellValue('C3','年龄');
$sheet->setCellValue('D3','身高');
$sheet->fromArray(
[
[1,'欧阳克','18岁','188cm'],
[2,'黄蓉','17岁','165cm'],
[3,'郭靖','21岁','180cm']
],
$num, # 数组索引,设置几,从第几行开始显示。尽量与参数三一致。也可设置为null(遇到奇怪BUG,参数二设置几,导出的excel,就会不显示数字几,很奇怪。设置null的意思是取消’默认行为‘。所以设置null,所有数字就可以正常显示了)
"B{$num}" # 数据从哪列哪行开始打印,比如B3就是从B列的第3行开始
);
// 合并单元格 合并A1到B1后赋值,赋值只能给A1,开始的坐标。
$sheet->mergeCells('A1:B1');
$sheet->getCell('A1')->setValue('欧阳克');
# Xlsx类 将电子表格保存到文件
# 文件名
$typeName = "demo";
# 这一段是把excel存在本地
$writer = new Xlsx($spreadsheet);
$writer->save("1.xlsx");
// 客户端文件下载
header('Content-Type:application/vnd.ms-excel');
header("Content-Disposition:attachment;filename={$typeName}.xls");
header('Cache-Control:max-age=0');
$writer = IOFactory::createWriter($spreadsheet, 'Xls');
$writer->save('php://output');
比如,A1行,内容为姓名、证件、电话。
A2开始,为相应的数据。$num的设定:假如$num设置为1,那么这时候参数2和参数3的num值均为1。将参数3的num前的列(字母)设置为B,导出成功之后,会自动隐藏A1行,同时留下A1行的A1下的数据,并从B1开始向下罗列数据。
期待结果:
姓名 | 证件 | 电话 |
张三 | xxxxx | 137xxxxxx |
李四 | xxxxx | 137xxxxxx |
实际结果:
姓名 | 张三 | xxxxxx | 137xxxxxx |
李四 | xxxxxx | 137xxxxxx | |
因此,此时将$num的值设置为2,同时将参数3的B改为A,结果就变成了从A2行向下罗列每组数据,从A2行第一个方格,开始向右罗列组内数据,从而得到期待的结果。
# 注,excel格式需为.xlsx,若不是,另存为一下即可
$uploadfile = 'F:\gongsi\__weiting_think\public\1.xlsx';
$reader = IOFactory::createReader('Xlsx');
$PHPExcel = $reader->load($uploadfile); // 载入excel文件
$sheet = $PHPExcel->getSheet(0); // 读取第一個工作表
$highestRow = $sheet->getHighestRow(); // 取得总行数
$highestColumm = $sheet->getHighestColumn(); // 取得总列数,从A开始
$data = [];
for ($row = 1; $row <= $highestRow; $row++) //行号从1开始
{
$data[$row]["A{$row}"] = $sheet->getCellByColumnAndRow(1,$row)->getValue();
$data[$row]["B{$row}"] = $sheet->getCellByColumnAndRow(2,$row)->getValue();
$data[$row]["C{$row}"] = $sheet->getCellByColumnAndRow(3,$row)->getValue();
}
dump($data);
array:11 [▼
1 => array:3 [▼
"A1" => "为了避免被母体发现,叶凡没有... ▶"
"B1" => "叶凡找到空间压缩球,并进入其中... ▶"
"C1" => "秦灵云一现身,就将三大邪神之一,... ▶"
]
2 => array:3 [▼
"A2" => "其它联盟,得知冰地联盟作为,纷...▶"
"B2" => "叶凡称,你要全力一战,尚有取胜机...▶"
"C2" => "叶凡与宇宙之主的投影见面,得知世界正... ▶"
]
]
以下示例为,根据学校区别学生请假条
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
use PhpOffice\PhpSpreadsheet\IOFactory;
public function DaochuDay () {
# 实例化 Spreadsheet 对象
$spreadsheet = new Spreadsheet();
# 获取活动工作薄
#$sheet = $spreadsheet->getActiveSheet();
# 获取学校
$school = Db::table("school")
->select()->toArray();
foreach($school as $key=>$school){
# 创建sheet
$spreadsheet->createSheet();
# 设置当前的活动sheet
$objActSheet = $spreadsheet->setActiveSheetIndex($key);
# 设置标签页名称
$spreadsheet->getActiveSheet($key)->setTitle($school["name"]);
# 数据筛选
$qingjia = Db::table("chufang_qingjia")
->where("start_time", "<=", getTime3()." 00:00:00")
->where("end_time", ">=", getTime3()." 23:59:59")
->where("is_del", "=", "No")
->where("school_name", "=", $school["name"])
->select()->toArray();
$excel = [];
foreach($qingjia as $key=>$item){
$excel[$key]['name'] = $item['child_name'];
$excel[$key]['id_card'] = "`".$item['id_card'];
$excel[$key]['school'] = $item['school_name'];
$excel[$key]['nianji'] = $item['nianji_name'];
$excel[$key]['banji'] = $item['banji_name'];
$excel[$key]['exp'] = $item['exp'];
$excel[$key]['start_time'] = $item['start_time'];
$excel[$key]['end_time'] = $item['end_time'];
}
# 赋值
$objActSheet->setCellValue('A1','姓名');
$objActSheet->setCellValue('B1','身份证');
$objActSheet->setCellValue('C1','学校');
$objActSheet->setCellValue('D1','年级');
$objActSheet->setCellValue('E1','班级');
$objActSheet->setCellValue('F1','请假理由');
$objActSheet->setCellValue('G1','请假开始时间');
$objActSheet->setCellValue('H1','请假结束时间');
# 行列
$num = 2;
$objActSheet->fromArray($excel,
null,
"A{$num}" # 数据从哪列哪行开始打印,比如B3就是从B列的第3行开始
);
}
# Xlsx类 将电子表格保存到文件
# 文件名
//$dangqian = str_replace("-", "", getTime());
$typeName = getTime3()."请假信息";
// 客户端文件下载
header('Content-Type:application/vnd.ms-excel');
header("Content-Disposition:attachment;filename={$typeName}.xls");
header('Cache-Control:max-age=0');
$writer = IOFactory::createWriter($spreadsheet, 'Xls');
$writer->save('php://output');
}