Symfony 如何使用liuggio/ExcelBundle将excel数据发布到数据库

Symfony 如何使用liuggio/ExcelBundle将excel数据发布到数据库,symfony,phpexcel,Symfony,Phpexcel,如果没有Symfony,这就是我将excel数据发布到数据库的方式 // Include PHPExcel_IOFactory require_once ('../Classes/PHPExcel/IOFactory.php'); $inputFileName = 'abc.xls'; // Read your Excel workbook try { $inputFileType = PHPExcel_IOFactory::identify($inputFileName);

如果没有Symfony,这就是我将excel数据发布到数据库的方式

//  Include PHPExcel_IOFactory

require_once ('../Classes/PHPExcel/IOFactory.php');
$inputFileName = 'abc.xls';

//  Read your Excel workbook
try {
    $inputFileType = PHPExcel_IOFactory::identify($inputFileName);
    $objReader = PHPExcel_IOFactory::createReader($inputFileType);
    $objPHPExcel = $objReader->load($inputFileName);
} catch(Exception $e) {
    die('Error loading file "'.pathinfo($inputFileName,PATHINFO_BASENAME).'": '.$e->getMessage());
}

//  Get worksheet dimensions
$sheet = $objPHPExcel->getSheet(0); 
$highestRow = $sheet->getHighestRow(); 
$highestColumn = $sheet->getHighestColumn();

//  Loop through each row of the worksheet in turn
for ($row = 1; $row <= $highestRow; $row++){ 

    //  Read a row of data into an array
    $rowData = $sheet->rangeToArray('A' . $row . ':' . $highestColumn . $row,
                                    NULL,
                                    TRUE,
                                    FALSE);
    //  Insert row data array into your database of choice here

}
//包含PHPExcel\u IOFactory
需要一次('../Classes/PHPExcel/IOFactory.php');
$inputFileName='abc.xls';
//阅读您的Excel工作簿
试一试{
$inputFileType=PHPExcel\u IOFactory::identify($inputFileName);
$objReader=PHPExcel\u IOFactory::createReader($inputFileType);
$objPHPExcel=$objReader->load($inputFileName);
}捕获(例外$e){
die('Error loading file'.pathinfo($inputFileName,pathinfo_BASENAME)。'”:'。$e->getMessage());
}
//获取工作表维度
$sheet=$objPHPExcel->getSheet(0);
$highestRow=$sheet->getHighestRow();
$highestColumn=$sheet->getHighestColumn();
//依次遍历工作表的每一行
对于($row=1;$row RANGETORARRAY('A'.$row'.:'。$HIGHEST COLUMN.$row),
无效的
是的,
假);
//在此处将行数据数组插入所选数据库
}
现在,我被卡住了。文档对我完成这项任务毫无帮助。我已经尝试了类似问题中给出的所有建议,但我无法做到这一点

从以下示例中的文件创建对象根本不起作用:

$phpExcelObject=$this->get('phpexcel')->createPHPExcelObject('file.xls');


如何完成这项任务?

我的计算方法如下:

<?php

namespace  Liuggio\ExcelBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;
use AppBundle\Entity\Product;

class FakeController extends Controller
{
    public function insertAction()
    {
        $data = [];
        $appPath = $this->container->getParameter('kernel.root_dir');
        $file = realpath($appPath . '/../web/excelFiles/abc.xls');

        $phpExcelObject = $this->get('phpexcel')->createPHPExcelObject($file);
        $sheet = $phpExcelObject->getActiveSheet()->toArray(null, true, true, true);

        $em = $this->getDoctrine()->getManager();
        $data['sheet'] = $sheet;
        //READ EXCEL FILE CONTENT
        foreach($sheet as $i=>$row) {
        if($i !== 1) {
            $product = new Product();

            $product->setProductCode($row['A']); 
            $product->setProductName($row['B']);
            $product->setProductRetailPrice($row['C']);
            $product->setProductCost($row['D']);
            $product->setProductTax($tax);
            $product->setCategory($category);
            //... and so on


            $em->persist($product);
            $em->flush();
            //redirect appropriately
            }
        }
        $data['obj'] = $phpExcelObject;
        return $this->render('excel/read.html.twig', ['data' => $data ] );
    }

}
  • 确保路径Liuggio/ExcelBundle/Controller/FakeController.php存在
  • 确保使用提供的文件(Liuggio/ExcelBundle/Tests/app)更新app/config.yml和routing.yml
  • 假设您正在更新产品表,请按如下方式更新FakeController:

    <?php
    
    namespace  Liuggio\ExcelBundle\Controller;
    
    use Symfony\Bundle\FrameworkBundle\Controller\Controller;
    use Symfony\Component\HttpFoundation\Response;
    use AppBundle\Entity\Product;
    
    class FakeController extends Controller
    {
        public function insertAction()
        {
            $data = [];
            $appPath = $this->container->getParameter('kernel.root_dir');
            $file = realpath($appPath . '/../web/excelFiles/abc.xls');
    
            $phpExcelObject = $this->get('phpexcel')->createPHPExcelObject($file);
            $sheet = $phpExcelObject->getActiveSheet()->toArray(null, true, true, true);
    
            $em = $this->getDoctrine()->getManager();
            $data['sheet'] = $sheet;
            //READ EXCEL FILE CONTENT
            foreach($sheet as $i=>$row) {
            if($i !== 1) {
                $product = new Product();
    
                $product->setProductCode($row['A']); 
                $product->setProductName($row['B']);
                $product->setProductRetailPrice($row['C']);
                $product->setProductCost($row['D']);
                $product->setProductTax($tax);
                $product->setCategory($category);
                //... and so on
    
    
                $em->persist($product);
                $em->flush();
                //redirect appropriately
                }
            }
            $data['obj'] = $phpExcelObject;
            return $this->render('excel/read.html.twig', ['data' => $data ] );
        }
    
    }
    

    您应该发布您使用Symfony尝试过的代码,并准确地指定哪些“根本不起作用”。根据指令阅读并更新您的问题。@gp\u sflover,如果我发布了我一直在尝试的内容,我会使一切变得复杂。我的问题是如何使用Symfony完成上述任务。谢谢。您是否按照安装说明进行操作?您尝试的代码在哪里?如果它不在控制器中,则无法运行work@JasonR阿曼,这正是我想让你帮我的。谢谢。我可以用symfony 3.3吗?是的,你可以。这就是我用的。