在Symfony数据库中导入Excel数据 [英] Import Excel data in Symfony database
问题描述
我正在一个项目中,我需要将Excel数据导入到我的Symfony数据库中.但是问题是我不知道该怎么做. 我尝试使用ExcelBundle.该项目是:用户必须使用表单按钮发送他的Excel文件,并且我需要提取不带标题的数据来填充我的数据库. 你能帮我吗?
I'm working on a project where I need to import Excel data to my Symfony database. But the problem is that I don't know how to do that. I tried with ExcelBundle. The project is: User has to use a form button to send his Excel file and I need to extract the data without headers to fill my Database. Can you help me ?
推荐答案
如果您可以将excel电子表格转换为CSV格式,那么有一个非常好的软件包可以处理!
If you can get your excel spreadsheet into CSV format, there is a really good package that can deal with it!
看看这个: http://csv.thephpleague.com/9.0/
这是他们的示例,显示将表放入数据库有多容易
Here's their example showing how easy it is to get your table into the DB
<?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();
}
试试看!
这篇关于在Symfony数据库中导入Excel数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!