Box/Spout 循环导出

<?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,
     $maxId,  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']); } }
$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

posted @ 2023-06-05 10:35  现世中的素人  阅读(87)  评论(0编辑  收藏  举报