php - 在 Symfony 数据库中导入 Excel 数据

标签 php excel symfony

我正在开发一个项目,需要将 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/

相关文章:

java - 如何将图像集放在excel java单元格的中心

excel - 在圆环图上定位标签

php - 如何在 Symfony2 中为特定页面使用 css 文件?

javascript - 如何更改组合框的选定值?

php - 如何在 Laravel 迁移中连接 3 个表(语法错误或访问冲突)

php - 在php中循环生成动态表

php - 通过 phpMyAdmin 更改表中的 URL

c# - EPPlus - 我是否需要对 ExcelRange 等对象调用 Dispose?

javascript - 无法从 mysql 中删除 (file.size)

symfony - 更新表单 vich 上传者,无法删除或编辑文件