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 ?
Asked
Active
Viewed 1.2k times
3
-
1Possible duplicate of [how to use phpexcel to read data and insert into database?](https://stackoverflow.com/questions/9695695/how-to-use-phpexcel-to-read-data-and-insert-into-database) – Eimsas Sep 06 '17 at 11:22
-
I already checked but Im using Symfony. – Koila69 Sep 06 '17 at 11:25
-
It does not matter what fremework you use, you can use PHPExcel library in Symfony project and include it in service – Eimsas Sep 06 '17 at 11:25
3 Answers
2
If you can get your excel spreadsheet into CSV format, there is a really good package that can deal with it!
Have a look at this: 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();
}
Give it a try!

delboy1978uk
- 12,118
- 2
- 21
- 39
-
1I use now with a Symfony 5, is necessary change in foreach but is perfect. Thanks – juanitourquiza Feb 20 '20 at 15:44
2
You can use fgetcsv PHP function, an exemple here.
Beford the Excel file must be changed to a CSV file.

user7867717
- 285
- 3
- 15
2
As mentioned in a comment you can use PHPExcel. Install the library using composer
composer require phpoffice/phpexcel
A typical reader might look something like
class GameImportReaderExcel
{
public function read($filename)
{
// Tosses exception
$reader = \PHPExcel_IOFactory::createReaderForFile($filename);
// Need this otherwise dates and such are returned formatted
/** @noinspection PhpUndefinedMethodInspection */
$reader->setReadDataOnly(true);
// Just grab all the rows
$wb = $reader->load($filename);
$ws = $wb->getSheet(0);
$rows = $ws->toArray();
foreach($rows as $row) {
// this is where you do your database stuff
$this->processRow($row);
}
Call the reader class from your controller
public function (Request $request)
{
$file = $request->files->has('file') ? $request->files->get('file') : null;
if (!$file) {
$errors[] = 'Missing File';
}
$reader = new GameImportReaderExcel();
$reader->read($file->getRealPath());
That should get you started. And yes you could convert to csv but why bother. Just as easy to read the raw file and save your users an extra step.

Cerad
- 48,157
- 8
- 90
- 92
-
This package is archived, use PHPspreadsheet instead: [https://github.com/PHPOffice/PhpSpreadsheet](https://github.com/PHPOffice/PhpSpreadsheet) – sneaky Mar 09 '20 at 09:13