如何只写入 active sheet 的 xlsx 文件 PHPSpreadsheet
How to write only active sheet of xlsx file PHPSpreadsheet
我想将 xlsx 文件显示为 html,但它总是 return 文件的第一个 sheet。我想要的是让作者仅在有多个 sheet 时显示一个活动的 sheet(sheet 的名称和顺序是随机的)。
这是我的代码:
public function view_excel($path){
$file = realpath(FCPATH)."/uploads/PKB/".$path;
$reader = IOFactory::createReader('Xlsx');
$spreadsheet = $reader->load($file);
// $spreadsheet = $spreadsheet->getActiveSheet();
$writer = IOFactory::createWriter($spreadsheet, 'Html');
$message = $writer->save('php://output');
}
有办法吗?谢谢
我成功做到了:
public function view_excel($path){
$file = realpath(FCPATH)."/uploads/PKB/".$path;
$reader = IOFactory::createReader('Xlsx');
$spreadsheet = $reader->load($file);
$active_sheet = $spreadsheet->getActiveSheet()->getTitle();
$reader->setLoadSheetsOnly($active_sheet); //only load active sheet
$worksheet = $reader->load($file);
$writer = IOFactory::createWriter($worksheet, 'Html');
$message = $writer->save('php://output');
}
不得不加载文件两次
我想将 xlsx 文件显示为 html,但它总是 return 文件的第一个 sheet。我想要的是让作者仅在有多个 sheet 时显示一个活动的 sheet(sheet 的名称和顺序是随机的)。 这是我的代码:
public function view_excel($path){
$file = realpath(FCPATH)."/uploads/PKB/".$path;
$reader = IOFactory::createReader('Xlsx');
$spreadsheet = $reader->load($file);
// $spreadsheet = $spreadsheet->getActiveSheet();
$writer = IOFactory::createWriter($spreadsheet, 'Html');
$message = $writer->save('php://output');
}
有办法吗?谢谢
我成功做到了:
public function view_excel($path){
$file = realpath(FCPATH)."/uploads/PKB/".$path;
$reader = IOFactory::createReader('Xlsx');
$spreadsheet = $reader->load($file);
$active_sheet = $spreadsheet->getActiveSheet()->getTitle();
$reader->setLoadSheetsOnly($active_sheet); //only load active sheet
$worksheet = $reader->load($file);
$writer = IOFactory::createWriter($worksheet, 'Html');
$message = $writer->save('php://output');
}
不得不加载文件两次