下面的代码只在我注释第三个工作表时才起作用,我可以看到打开并查看插入的内容,但是当我包含第三个工作表时,它不会打开文件,而是显示错误,can’t be opened for some reason.

    $this->spreadsheet = new Spreadsheet;
    $this->spreadsheet->setActiveSheetIndex(0);
    $sheet = $this->spreadsheet->getActiveSheet();
    $sheet->setCellValue('A1', 'Hello world');
    $sheet->setTitle('First');

    $this->spreadsheet->createSheet();
    $this->spreadsheet->setActiveSheetIndex(1);
    $sheet = $this->spreadsheet->getActiveSheet();
    $sheet->setCellValue('A1', 'Hello');
    $sheet->setTitle('Second');

    $this->spreadsheet->createSheet();
    $this->spreadsheet->setActiveSheetIndex(2);
    $sheet = $this->spreadsheet->getActiveSheet();
    $sheet->setTitle('Third');


    $writer = new Xlsx($this->spreadsheet);
    $writer->setOffice2003Compatibility(true);
    $writer->save($fileName);

请帮我解决这个问题。https://github.com/PHPOffice/PhpSpreadsheet/
(开发)分公司

最佳答案

根据documentation您可以尝试:

$reader =  \PhpOffice\PhpSpreadsheet\IOFactory::createReader("Xlsx");

$spreadsheet = $reader->load('<path>'); //Path of reader sheet
// Create a new worksheet called "Name" in your case 3 Times
$myWorkSheet = new \PhpOffice\PhpSpreadsheet\Worksheet\Worksheet($spreadsheet, 'Name');

// Attach the "Name" worksheet as the first worksheet in the Spreadsheet object
$spreadsheet->addSheet($myWorkSheet, 0);

然后你可以叫一张单子的名字
$spreadsheet->getSheetByName('Name');

关于php - PhpOffice\PhpSpreadsheet多个工作表不起作用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44236582/

10-10 14:10