12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091 |
- <?php
- namespace Tests\Exporters\CSV;
- use Box\Spout\Common\Entity\Style\Color;
- use Box\Spout\Writer\Common\Creator\Style\StyleBuilder;
- use Box\Spout\Writer\Common\Creator\WriterEntityFactory;
- use Dcat\EasyExcel\Excel;
- use Dcat\EasyExcel\Support\SheetCollection;
- use Tests\Exporters\Exporter;
- use Tests\TestCase;
- class WithSheetTest extends TestCase
- {
- use Exporter;
- /**
- * @group exporter
- */
- public function test()
- {
- $users = include __DIR__.'/../../resources/users.php';
- $storePath = $this->generateTempFilePath('csv');
- $sheet = Excel::createSheet($users)
- ->headingStyle(
- (new StyleBuilder)
- ->setFontColor(Color::BLUE)
- ->setFontSize(14)
- ->build()
- )
- ->row(function (array $row) {
- $style = (new StyleBuilder)
- ->setFontColor(Color::PURPLE)
- ->setFontSize(14)
- ->build();
- return WriterEntityFactory::createRowFromArray($row, $style);
- });
- // 保存
- Excel::export($sheet)->store($storePath);
- // 读取
- $this->assertSingleSheet($storePath, 0, $users);
- /*
- |---------------------------------------------------------------
- | 测试多个sheet
- |---------------------------------------------------------------
- */
- $users1 = new SheetCollection(array_slice($users, 0, 30));
- $users2 = new SheetCollection(array_values(array_slice($users, 30, 30)));
- $storePath = $this->generateTempFilePath('csv');
- $sheet1 = Excel::createSheet($users1, 'sheet1');
- $sheet2 = Excel::createSheet($users2, 'sheet2');
- // 保存
- Excel::export([$sheet1, $sheet2])->store($storePath);
- // 读取
- $this->assertSingleSheet($storePath, 0, $users);
- }
- public function testWithChunkQuery()
- {
- $users = include __DIR__.'/../../resources/users.php';
- $users1 = new SheetCollection(array_slice($users, 0, 30));
- $users2 = new SheetCollection(array_values(array_slice($users, 30, 30)));
- $storePath = $this->generateTempFilePath('csv');
- $sheet1 = Excel::createSheet()->name('sheet1')->chunk(function (int $times) use ($users1) {
- return $users1->forPage($times, 10);
- });
- $sheet2 = Excel::createSheet()->name('sheet2')->chunk(function (int $times) use ($users2) {
- return $users2->forPage($times, 10);
- });
- // 保存
- Excel::export([$sheet1, $sheet2])->store($storePath);
- // 读取
- $this->assertSingleSheet($storePath, 0, $users);
- }
- }
|