简体   繁体   English

在Symfony数据库中导入Excel数据

[英]Import Excel data in Symfony database

I'm working on a project where I need to import Excel data to my Symfony database. 我正在一个项目中,我需要将Excel数据导入到我的Symfony数据库中。 But the problem is that I don't know how to do that. 但是问题是我不知道该怎么做。 I tried with ExcelBundle. 我尝试使用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. 该项目是:用户必须使用表单按钮发送他的Excel文件,而我需要提取不带标题的数据来填充我的数据库。 Can you help me ? 你能帮助我吗 ?

You can use fgetcsv PHP function, an exemple here . 您可以使用fgetcsv PHP函数, 这里是一个示例。

Beford the Excel file must be changed to a CSV file. Beford Excel文件必须更改为CSV文件。

As mentioned in a comment you can use PHPExcel. 如评论中所述,您可以使用PHPExcel。 Install the library using composer 使用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. 是的,您可以转换为csv,但是为什么要打扰。 Just as easy to read the raw file and save your users an extra step. 就像读取原始文件一样容易,并为用户节省了额外的步骤。

If you can get your excel spreadsheet into CSV format, there is a really good package that can deal with it! 如果您可以将excel电子表格转换为CSV格式,那么可以使用一个非常好的程序包!

Have a look at this: http://csv.thephpleague.com/9.0/ 看看这个: 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! 试试看!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM