我正在开发一个项目,需要将 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();
}
试一试!
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)