I need to upload an excel file and import the content in the database to the related table.
I have already created a script to import it. The script below works properly
$excelObj = $this->get('phpexcel')->createPHPExcelObject($path_file);
$sheet = $excelObj->getActiveSheet()->toArray(null,true,true,true);
$em = $this->getDoctrine()->getManager();
//READ EXCEL FILE CONTENT
foreach($sheet as $i=>$row) {
if($i !== 1) {
$account = $em->getRepository('ExcelBundle:Users')->findOneByUsername($row['A']);
if(!$account) {
$user = new Users();
}
$user->setName($row['A']);
$user->setUsername($row['B']);
$user->setEmail($row['C']);
//... and so on
$em->persist($user);
$em->flush();
}
}
Now, instead of importing the row A, row B...etc of the excel file I need to import the name of the row.
name username email ...and so on
$user->setName($row['name']);
$user->setUsername($row['username']);
$user->setEmail($row['email']);
How can I do it?
So you need to map the headings row (row #1) to the actual values in each subsequent row
foreach($sheet as $i=>$row) {
if($i == 1) {
$headings = $row;
} else {
$row = array_combine($headings, $row);
.... do the rest of your stuff here
$user->setName($row['name']);
$user->setUsername($row['username']);
$user->setEmail($row['email']);
....
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.