<?php
namespace app\index\controller;
use app\common\controller\Frontend;
use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
class Exl extends Frontend
{
protected $noNeedLogin = '*';
protected $noNeedRight = '*';
protected $layout = '';
public function index()
{
// 加载现有的Excel文件
$spreadsheet = IOFactory::load(ROOT_PATH .'(206)固定资产投资项目情况.xls');
// 获取活动工作表
$worksheet = $spreadsheet->getActiveSheet();
// 遍历工作表中的所有单元格
foreach ($worksheet->getRowIterator() as $row) {
$cellIterator = $row->getCellIterator();
$cellIterator->setIterateOnlyExistingCells(false);
foreach ($cellIterator as $cell) {
// 替换单元格中的内容
if ($cell !== null) {
$value = $cell->getValue();
$newValue = str_replace('文 号:国统字(2023) 88号', '文 号:国统字(2023) 99号', $value);
$newValue = str_replace('一、项目基本情况', '一、项目基本情况!', $newValue);
$newValue = str_replace('登记注册统计类别', '登记注册统计类别:', $newValue);
$cell->setValue($newValue);
}
}
}
// 保存修改后的Excel文件
$writer = IOFactory::createWriter($spreadsheet, 'Xlsx');
$writer->save('example_modified.xlsx');
}
}
快捷键:exltihuan
源:
sublime_text格式
<snippet>
<content><![CDATA[
<?php
namespace app\index\controller;
use app\common\controller\Frontend;
use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
class Exl extends Frontend
{
protected \$noNeedLogin = '*';
protected \$noNeedRight = '*';
protected \$layout = '';
public function index()
{
// 加载现有的Excel文件
\$spreadsheet = IOFactory::load(ROOT_PATH .'(206)固定资产投资项目情况.xls');
// 获取活动工作表
\$worksheet = \$spreadsheet->getActiveSheet();
// 遍历工作表中的所有单元格
foreach (\$worksheet->getRowIterator() as \$row) {
\$cellIterator = \$row->getCellIterator();
\$cellIterator->setIterateOnlyExistingCells(false);
foreach (\$cellIterator as \$cell) {
// 替换单元格中的内容
if (\$cell !== null) {
\$value = \$cell->getValue();
\$newValue = str_replace('文 号:国统字(2023) 88号', '文 号:国统字(2023) 99号', \$value);
\$newValue = str_replace('一、项目基本情况', '一、项目基本情况!', \$newValue);
\$newValue = str_replace('登记注册统计类别', '登记注册统计类别:', \$newValue);
\$cell->setValue(\$newValue);
}
}
}
// 保存修改后的Excel文件
\$writer = IOFactory::createWriter(\$spreadsheet, 'Xlsx');
\$writer->save('example_modified.xlsx');
}
}
]]></content>
<tabTrigger>exltihuan(Excel遍历所有单元格替换内容,xls替换,表格替换内容)</tabTrigger>
<scope></scope>
</snippet>
发表评论 取消回复