嗨,有一种方法可以在 PHP 中编辑上传的 Excel (xlsx) 文档。文档非常简单(带有项目名称、价格、数量的表格)。我想 PHPExcel 只能读取文档但不能编辑?有什么建议吗?
问问题
16044 次
3 回答
5
您也可以使用 PHPExcel 来编辑文档,查看这些线程以获取更多信息:
于 2012-05-27T18:09:33.400 回答
2
于 2012-05-27T21:09:13.090 回答
0
请尝试这种方法。例如,我想编辑/更新工作表(sheet1)中的列(expiryDate)。
$localFilePath = "C:/xampp/htdocs/Projects/abc.xlsx";
$reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader('Xlsx');
$reader->setLoadSheetsOnly(["sheet1"]);
$spreadsheet = $reader->load($localFilePath);
$worksheet = $spreadsheet->setActiveSheetIndex(0);
$column = NULL;
foreach($worksheet->getRowIterator() as $index => $row) {
$cellIterator = $row->getCellIterator();
$cellIterator->setIterateOnlyExistingCells(FALSE); // This loops through all cells,
foreach($cellIterator as $subIndex => $cell) {
$value = $cell->getValue();
if($index == 1 && $value == "expiryDate"){
$column = $subIndex;
}
if((empty($value) || $value == "") && $column == $subIndex && $index != 1){
$cell->setValue("2050-12-31 00:00:00");
}
}
}
$writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, "Xlsx");
$writer->save($localFilePath);
于 2022-02-26T23:43:49.517 回答