我可以用fzaninoto/Faker生成doc.xls文件吗



正在阅读laravel 9站点的文档https://github.com/fzaninotto/Faker我没有发现是否有一种方法可以用这个库生成doc(microsoft word(.xls(microsoft excel(文件?也许是别的图书馆?

提前感谢!

Faker用于生成简单的数据,如句子、电子邮件、电话等。但您可以使用,例如maatwebsite/excel来制作用Faker填充的xlsx。创建doc文件的最简单方法是制作扩展名为dochtml文件。

更新:

请记住,maatwebsite/excel是为Laravel框架构建的,但也有其他包可以独立于框架使用,例如XLSWriter

maatwebsite/excel

Laravel的工厂将生产用户模型

class UserFactory extends Factory
{
protected $model = User::class;
public function definition()
{
return [
'login' => '0'.$this->faker->numberBetween(70000000000, 80000000000),
'email' => $this->faker->unique()->safeEmail,
'lastname' => $this->faker->lastName,
'firstname' => $this->faker->firstNameMale,
'phone' => $this->faker->phoneNumber,
];
}
}

maatwebsite的导出类(此类使用集合作为源(

use MaatwebsiteExcelConcernsFromCollection;
class UsersExport implements FromCollection
{
private $collection;
public function __construct($collection)
{
$this->collection = $collection;
}
public function collection()
{
return $this->collection;
}
}

使用laravel的种子制作10个用户的xlsx文件

class XlsSeeder extends Seeder
{
public function run()
{
$users = User::factory()
->count(10)
->make();
Excel::store(new UsersExport($users), 'users.xlsx');
}
}

XLS编写器

无论您在哪里需要创建xlsx,都可以编写以下内容:

for($i = 0; $i < 10; $i++) {
$users[] = [
$faker->numberBetween(70000000000, 80000000000),
$faker->unique()->safeEmail,
$faker->lastName,
$faker->firstName,
$faker->phoneNumber,
];
}
$fileObject  = new VtifulKernelExcel([
'path' => './tests'
]);
$file = $fileObject->fileName('users.xlsx', 'sheet_one')
->header(['login', 'email', 'lastname', 'firstname', 'phone'])
->data($users);
$file->output();

最新更新