<?php
include('php_script/db.php');
use Box\Spout\Common\Type;
use Box\Spout\Writer\Style\Border;
use Box\Spout\Writer\Style\BorderBuilder;
use Box\Spout\Writer\Style\Color;
use Box\Spout\Writer\Style\StyleBuilder;
use Box\Spout\Writer\WriterFactory;
include('php_script/spout/src/Spout/Autoloader/autoload.php');
$sql = mysqli_query($con,"select * from person ");
$border = (new BorderBuilder())
        ->setBorderBottom(Color::GREEN, Border::WIDTH_THIN, Border::STYLE_DASHED)
        //->setFontColor(Color::BLUE)
        //->setBackgroundColor(Color::YELLOW)
        ->build();
    $style = (new StyleBuilder())
        ->setBorder($border)
        ->build();
    $filePath = "person".date("Y-m-d-H-i-s").'.xlsx';
    $writer = WriterFactory::create(Type::XLSX);
    $writer->openToFile($filePath);

    $array = ['TYPE'];

    $writer->addRowWithStyle($array, $style);



    while( $rows = mysqli_fetch_assoc($sql)) {

    $Type_subsidiary = $rows['Type_subsidiary'];
    $data =  [$Type_subsidiary];
    $writer->addRow($data);
    }

    $writer->close();
     if (file_exists($filePath)) {
            header('Content-Description: File Transfer');
            header('Content-Type: application/octet-stream');
            header('Content-Disposition: attachment; filename="'.basename($filePath).'"');
            header('Expires: 0');
            header('Cache-Control: must-revalidate');
            header('Pragma: public');
            header('Content-Length: ' . filesize($filePath));
            readfile($filePath);
            exit;
        }


?>

javascript - 使用PHP中的Spout库导出Excel时文件扩展名或文件无效-LMLPHP
   我在使用spout库导出Excel时遇到问题。我不知道在哪
     是我的代码中的问题。我对喷口库了解不多。我已经尝试了很多次,但是相同的错误一次又一次地发生。请指导我问题出在哪里。

最佳答案

您是否尝试过让spout直接创建文件并将其“发送”到浏览器,而不指定其文档http://opensource.box.com/spout/getting-started/中指定的标头?

$writer->openToBrowser($fileName); // stream data directly to the browser


希望这会有所帮助,在我们的应用程序(基于Symfony)中,我们使用spout但无需指定标头和文件路径。

08-26 08:53