我正在开发一个项目,需要将 Excel 数据导入到我的 Symfony 数据库中。但问题是我不知道该怎么做。 我尝试使用 ExcelBundle。该项目是:用户必须使用表单按钮发送他的 Excel 文件,我需要提取不带标题的数据来填充我的数据库。 你能帮我吗?
最佳答案
如果您可以将 Excel 电子表格转换为 CSV 格式,那么有一个非常好的软件包可以处理它!
看看这个:http://csv.thephpleague.com/9.0/
这是他们的示例,展示了将表放入数据库是多么容易
<?php
use League\Csv\Reader;
//We are going to insert some data into the users table
$sth = $dbh->prepare(
"INSERT INTO users (firstname, lastname, email) VALUES (:firstname, :lastname, :email)"
);
$csv = Reader::createFromPath('/path/to/your/csv/file.csv')
->setHeaderOffset(0)
;
//by setting the header offset we index all records
//with the header record and remove it from the iteration
foreach ($csv as $record) {
//Do not forget to validate your data before inserting it in your database
$sth->bindValue(':firstname', $record['First Name'], PDO::PARAM_STR);
$sth->bindValue(':lastname', $record['Last Name'], PDO::PARAM_STR);
$sth->bindValue(':email', $record['E-mail'], PDO::PARAM_STR);
$sth->execute();
}
尝试一下!
关于php - 在 Symfony 数据库中导入 Excel 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46073838/