首页 > 解决方案 > 在书中发现了无法阅读的内容

问题描述

每次我制作 xlsx 文件时,我都无法毫无错误地打开它:“在书中找到了无法阅读的内容”。

代码:

function getNameFromNumber($num) {
        $numeric = ($num - 1) % 26;
        $letter = chr(65 + $numeric);
        $num2 = intval(($num - 1) / 26);
        if ($num2 > 0) {
            return getNameFromNumber($num2) . $letter;
        } else {
            return $letter;
        }
    }

    require_once $_SERVER['DOCUMENT_ROOT'] . '/local/vendor/autoload.php';
    use PhpOffice\PhpSpreadsheet\Spreadsheet;
    use PhpOffice\PhpSpreadsheet\Writer\Xlsx;

    $objPHPExcel = new Spreadsheet();

    $date =  date("Y-m-d H:i:s");

    // Set document properties
    $objPHPExcel->getProperties()
        ->setTitle("Выгрузка заявлений от" . $date)
        ->setSubject("Выгрузка заявлений от" . $date)
        ->setDescription("Выгрузка заявлений по фильтру от" . $date);

    // Rename worksheet
    $objPHPExcel->getActiveSheet()->setTitle('Выгрузка заявлений по фильтру');


    // Set active sheet index to the first sheet, so Excel opens this as the first sheet
    $objPHPExcel->setActiveSheetIndex(0);


    $headers = ['ID', 'Фамилия абитуриента', 'Имя абитуриента', 'Отчество абитуриента', 'EMAIL', 'Статус', 'Условния приема', 'Уровень образования', 'Дата отправки заявления'];
    if($arParams['GET_DOCS'] == 'Y')
        $headers[] = 'Ссылки на файлы';


    $activeSheet = $objPHPExcel->setActiveSheetIndex(0);
    foreach( $headers as $key => $header ){
        $activeSheet->setCellValue(getNameFromNumber($key + 1) . '1' , $header);
    }

    foreach($arResult["ITEMS"] as $rowIndex => $arItem){
        //Here i prepare $row to set values    
        $c = 0;
        foreach($row  as $columnIndex => $item ){
            $c++;
            $activeSheet->setCellValue(getNameFromNumber($c) . (2+$rowIndex) , $item);
        }

        $c = 0;
        foreach($row  as $columnIndex => $item ){
            $c++;
            $activeSheet->getColumnDimension(getNameFromNumber($c))->setAutoSize(true);
        }

    }

    $APPLICATION->RestartBuffer();

    header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
    header('Content-Disposition: attachment;filename="' . $date . '.xlsx"');
    header('Cache-Control: max-age=0');

    $writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($objPHPExcel, 'Xlsx');
    $writer->save('php://output');

我试图制作空文件 -

$objPHPExcel = new Spreadsheet();
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="123.xlsx"');
header('Cache-Control: max-age=0');

$writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($objPHPExcel, 'Xlsx');
$writer->save('php://output');

它没有帮助。错误不会消失

我的 MS Excel 可以正确恢复这个文件,但它不会在 OpenOffice 或其他东西中打开,所以我的客户端无法在 MAC 上打开它。我该如何解决这个问题?

标签: phpexcelphpspreadsheet

解决方案


mb_internal_encoding('latin1');

这有帮助


推荐阅读