大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。
Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺
php用PhpSpreadsheet对Excel进行读取、写入、修改十分便捷,下面将我在工作中用到过的操作进行总结。文档参考百度搜索和原始文档:https://phpspreadsheet.readthedocs.io/en/latest/(只有英文版)
首先要引入php模块: composer require phpoffice/phpspreadsheet
1、读取Excel:见文章PhpSpreadsheet读取单元格内容的坑
2、修改Excel:示例代码(只保留了基础代码):
$this->view->title = '填写Excel';
$abs_excel_path = $_SERVER['DOCUMENT_ROOT'] . '/data_be_writen.xlsx';
$spreadsheet = \PhpOffice\PhpSpreadsheet\IOFactory::load($abs_excel_path);
$count = 0;
// 三个sheet都进行修改
for ($sheetIndex=0; $sheetIndex<3; $sheetIndex++) {
$worksheet = $spreadsheet->getSheet($sheetIndex);
$highestRow = $worksheet->getHighestRow(); // 总行数1, 2,3
for ($rowIndex = 1; $rowIndex <= $highestRow; $rowIndex++) {
$count++;
// 写入的数据只是个示例,具体项目中根据需求获取数值写入
$worksheet->getCellByColumnAndRow(1, $rowIndex)->setValue($rowIndex);
$worksheet->getCellByColumnAndRow(2, $rowIndex)->setValue($rowIndex * 10);
}
}
echo "填写了${count}条数据";
$writer = new \PhpOffice\PhpSpreadsheet\Writer\Xlsx($spreadsheet);
$writer->save($abs_excel_path);
3、写入Excel,下面的代码提现了一个导出账号的过程,是基于ThinkPHP5.0的:
$head = ['部门', '姓名', '职位', '手机号', '邮箱'];
$count = count($head);
$spreadsheet = new \PhpOffice\PhpSpreadsheet\Spreadsheet();
$worksheet = $spreadsheet->getActiveSheet();
$worksheet->getColumnDimension('A')->setWidth(20);
$worksheet->getColumnDimension('B')->setWidth(12);
$worksheet->getColumnDimension('C')->setWidth(12);
$worksheet->getColumnDimension('D')->setWidth(18);
$worksheet->getColumnDimension('E')->setWidth(20);
$rowIndex = 1;
foreach ($head as $colIndex => $title) {
$worksheet->getCellByColumnAndRow($colIndex + 1, $rowIndex)->setValue($title);
}
$admins = $this->order('id asc')->select();
foreach ($admins as $admin) {
$rowIndex++;
$worksheet->getCellByColumnAndRow(1, $rowIndex)->setValue(model('Department')->get_name_by_id($admin['department_id']));
$worksheet->getCellByColumnAndRow(2, $rowIndex)->setValue($admin['username']);
$worksheet->getCellByColumnAndRow(3, $rowIndex)->setValue($admin['position']);
$worksheet->getCellByColumnAndRow(4, $rowIndex)->setValue($admin['phone']);
$worksheet->getCellByColumnAndRow(5, $rowIndex)->setValue($admin['email']);
}
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="账号.xlsx"');
header('Cache-Control: max-age=0');
$writer = new \PhpOffice\PhpSpreadsheet\Writer\Xlsx($spreadsheet);
$writer->save('php://output');
//删除清空:
$spreadsheet->disconnectWorksheets();
unset($spreadsheet);
exit;
4、修改单元格样式的,代码进行了精简,具体要标记哪个,根据项目实际判断:
$this->view->title = '填写Excel';
$abs_excel_path = $_SERVER['DOCUMENT_ROOT'] . '/2.xlsx';
$styleArray = [
'borders' => [
'outline' => [
'borderStyle' => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THICK,
'color' => ['argb' => 'FFFF0000'],
],
],
];
$spreadsheet = \PhpOffice\PhpSpreadsheet\IOFactory::load($abs_excel_path);
$count = 0;
for ($sheetIndex=0; $sheetIndex<3; $sheetIndex++) {
$worksheet = $spreadsheet->getSheet($sheetIndex);
$highestRow = $worksheet->getHighestRow(); // 总行数1, 2,3
$excel_data = new \app\admin\model\ExcelData2;
for ($rowIndex = 2; $rowIndex <= $highestRow; $rowIndex++) {
$worksheet->getStyle("B{
$rowIndex}")->applyFromArray($styleArray);
}
}
echo "发现了${count}条数据";
$writer = new \PhpOffice\PhpSpreadsheet\Writer\Xlsx($spreadsheet);
$writer->save($_SERVER['DOCUMENT_ROOT'] . '/2标记.xlsx');
注意:对比2、4可以发现:读取了Excel以后,如果要改写,只需要两行代码,在save的时候填写的绝对路径跟读取的一样,则为改写,如果不同,则为新建。
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/192799.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...