Box/Spout 循环导出

发布时间 2023-06-05 10:39:25作者: 现世中的素人
<?php
$tmpFile = tmpfile();  # 创建临时文件
$meta    = stream_get_meta_data($tmpFile);
$writer  = ExcelWriter::newWriter($meta['uri']); # 等于最下方类

$writer->addHeader([
    'unionid',
    '真实姓名',
    '手机号'
], 11);

//获取数据【批次获取 每次获取N条 并写入】
$maxId = 0;
while (true) {
    //获取一批数据
    $resp = AgentNumberOfDoctorRepository::AgentNumberOfDoctorsDetail(
        $nickname,
        $truename,
        50
    );

    if (!$resp) {
        break;
    }
//构建数据集合 foreach ($resp as $key => $value) { $data[$key]['unionid'] = $value['unionid']; $data[$key]['nickname'] = $value['nickname'];      $data[$key]['share_phone_number'] = $value['share_phone_number'] ?? ''; } # 循环写入 $tmpRows = []; foreach ($rows as $row) { $tmpRows[] = WriterEntityFactory::createRowFromArray($row); } $this->writer->addRows($tmpRows); //循环游标起始位置 foreach ($resp as $item) { $maxId = max($maxId, item['id']); } $id = $maxId; } $writer->close(); fseek($tmpFile, 0, SEEK_END); $end = ftell($tmpFile); fseek($tmpFile, 0, SEEK_SET);

 

$writer = WriterEntityFactory::createXLSXWriter();
$writer->openToFile($filePath);

/** Create a style with the StyleBuilder */
$style = (new StyleBuilder())
           ->setFontBold()
           ->setFontSize(15)
           ->setFontColor(Color::BLUE)
           ->setShouldWrapText()
           ->setCellAlignment(CellAlignment::RIGHT)
           ->setBackgroundColor(Color::YELLOW)
           ->build();

# 官网地址:opensource.box.com