企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
# Box/Spout处理excel和csv - 导出csv和xlsx ``` <pre class="calibre14">``` use Box\Spout\Writer\WriterFactory<span class="token2">;</span> use Box\Spout\Common\Type<span class="token2">;</span> $result <span class="token">=</span> <span class="token2">[</span><span class="token2">]</span><span class="token2">;</span> <span class="token5">for</span> <span class="token2">(</span>$i <span class="token">=</span> <span class="token3">0</span><span class="token2">;</span> $i <span class="token"><</span> <span class="token3">100</span><span class="token2">;</span> $i<span class="token">++</span><span class="token2">)</span> <span class="token2">{</span> $arr <span class="token">=</span> <span class="token2">[</span> <span class="token4">'name'</span> <span class="token">=</span><span class="token">></span> \Mock<span class="token2">:</span><span class="token2">:</span><span class="token1">name</span><span class="token2">(</span><span class="token2">)</span><span class="token2">,</span> <span class="token4">'age'</span> <span class="token">=</span><span class="token">></span>\Mock<span class="token2">:</span><span class="token2">:</span><span class="token1">number</span><span class="token2">(</span><span class="token4">'10-80'</span><span class="token2">)</span><span class="token2">,</span> <span class="token4">'email'</span> <span class="token">=</span><span class="token">></span>\Mock<span class="token2">:</span><span class="token2">:</span><span class="token1">email</span><span class="token2">(</span><span class="token2">)</span><span class="token2">,</span> <span class="token4">'address'</span> <span class="token">=</span><span class="token">></span>\Mock<span class="token2">:</span><span class="token2">:</span><span class="token1">address</span><span class="token2">(</span><span class="token2">)</span><span class="token2">,</span> <span class="token4">'times'</span><span class="token">=</span><span class="token">></span>\Mock<span class="token2">:</span><span class="token2">:</span><span class="token1">datetime</span><span class="token2">(</span><span class="token2">)</span> <span class="token2">]</span><span class="token2">;</span> $result<span class="token2">[</span><span class="token2">]</span> <span class="token">=</span> $arr<span class="token2">;</span> <span class="token2">}</span> $reader <span class="token">=</span> WriterFactory<span class="token2">:</span><span class="token2">:</span><span class="token1">create</span><span class="token2">(</span>Type<span class="token2">:</span><span class="token2">:</span>XLSX<span class="token2">)</span><span class="token2">;</span> $reader<span class="token">-</span><span class="token">></span><span class="token1">openToBrowser</span><span class="token2">(</span><span class="token4">"测试.xlsx"</span><span class="token2">)</span><span class="token2">;</span><span class="token6">//输出到浏览器</span> <span class="token6">//$reader->openToFile(storage_path('file/s.xlsx'));//输出到文件</span> $reader<span class="token">-</span><span class="token">></span><span class="token1">addRow</span><span class="token2">(</span><span class="token2">[</span> <span class="token4">'name'</span> <span class="token">=</span><span class="token">></span> <span class="token4">'姓名'</span><span class="token2">,</span> <span class="token4">'age'</span> <span class="token">=</span><span class="token">></span> <span class="token4">'年龄'</span><span class="token2">,</span> <span class="token4">'email'</span> <span class="token">=</span><span class="token">></span> <span class="token4">'邮箱'</span><span class="token2">,</span> <span class="token4">'address'</span> <span class="token">=</span><span class="token">></span> <span class="token4">'地址'</span><span class="token2">,</span> <span class="token4">'times'</span><span class="token">=</span><span class="token">></span><span class="token4">'时间'</span> <span class="token2">]</span><span class="token2">)</span><span class="token2">;</span> $reader<span class="token">-</span><span class="token">></span><span class="token1">addRows</span><span class="token2">(</span>$result<span class="token2">)</span><span class="token2">;</span> $reader<span class="token">-</span><span class="token">></span><span class="token1">close</span><span class="token2">(</span><span class="token2">)</span><span class="token2">;</span> ``` ``` - 读取excel的xlsx和csv ``` <pre class="calibre16">``` use Box\Spout\Reader\ReaderFactory<span class="token2">;</span> use Box\Spout\Common\Type<span class="token2">;</span> $path <span class="token">=</span> <span class="token1">storage_path</span><span class="token2">(</span><span class="token4">'file/a.xlsx'</span><span class="token2">)</span><span class="token2">;</span> $type <span class="token">=</span> <span class="token1">pathinfo</span><span class="token2">(</span>$path<span class="token2">)</span><span class="token2">;</span> $type <span class="token">=</span> <span class="token1">strtolower</span><span class="token2">(</span>$type<span class="token2">[</span><span class="token4">"extension"</span><span class="token2">]</span><span class="token2">)</span><span class="token2">;</span> $type <span class="token">=</span> <span class="token2">(</span>$type <span class="token">===</span> <span class="token4">'xlsx'</span><span class="token2">)</span><span class="token">?</span>Type<span class="token2">:</span><span class="token2">:</span>XLSX<span class="token2">:</span>Type<span class="token2">:</span><span class="token2">:</span>CSV<span class="token2">;</span> $reader <span class="token">=</span> ReaderFactory<span class="token2">:</span><span class="token2">:</span><span class="token1">create</span><span class="token2">(</span>$type<span class="token2">)</span><span class="token2">;</span> $reader<span class="token">-</span><span class="token">></span><span class="token1">setShouldFormatDates</span><span class="token2">(</span><span class="token3">true</span><span class="token2">)</span><span class="token2">;</span> <span class="token5">if</span><span class="token2">(</span>$type <span class="token">===</span> <span class="token4">'csv'</span><span class="token2">)</span><span class="token2">{</span>$reader<span class="token">-</span><span class="token">></span><span class="token1">setEncoding</span><span class="token2">(</span><span class="token4">'GB2312'</span><span class="token2">)</span><span class="token2">;</span><span class="token2">}</span> $reader<span class="token">-</span><span class="token">></span><span class="token1">open</span><span class="token2">(</span>$path<span class="token2">)</span><span class="token2">;</span> $iterator <span class="token">=</span> $reader<span class="token">-</span><span class="token">></span><span class="token1">getSheetIterator</span><span class="token2">(</span><span class="token2">)</span><span class="token2">;</span> $iterator<span class="token">-</span><span class="token">></span><span class="token1">rewind</span><span class="token2">(</span><span class="token2">)</span><span class="token2">;</span> $sheet1 <span class="token">=</span> $iterator<span class="token">-</span><span class="token">></span><span class="token1">current</span><span class="token2">(</span><span class="token2">)</span><span class="token2">;</span> $rowIter <span class="token">=</span> $sheet1<span class="token">-</span><span class="token">></span><span class="token1">getRowIterator</span><span class="token2">(</span><span class="token2">)</span><span class="token2">;</span> $data <span class="token">=</span><span class="token2">[</span><span class="token2">]</span><span class="token2">;</span> foreach <span class="token2">(</span>$rowIter as $rowIndex <span class="token">=</span><span class="token">></span> $row<span class="token2">)</span> <span class="token2">{</span> $data<span class="token2">[</span><span class="token2">]</span> <span class="token">=</span> $row<span class="token2">;</span> <span class="token2">}</span> $reader<span class="token">-</span><span class="token">></span><span class="token1">close</span><span class="token2">(</span><span class="token2">)</span><span class="token2">;</span> ``` ```