3

以下代码仅在我评论第三张表时才有效,我可以看到打开并查看插入的内容,但是当我包含第三张表时,它不会打开文件但显示错误,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/ (开发)分支

4

2 回答 2

0

尝试删除或注释掉该行

$writer->setOffice2003Compatibility(true);

它在我的情况下抛出了那个错误

于 2021-05-21T15:58:21.383 回答
0

根据文档,您可以尝试以下操作:

$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');
于 2019-06-11T17:58:14.460 回答