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

时间:2017-08-06 17:35:41

标签: 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);
    $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

}

现在有了ExcelBundle,我被困住了。在这项任务中,文档根本没有帮助我。我已尝试过在类似问题中给出的所有建议,但我无法做到这一点。

如下例所示,从文件创建对象根本不起作用:

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

如何完成这项任务?

1 个答案:

答案 0 :(得分:1)

我按照以下方式解决了这个问题:

  1. 确保路径Liuggio / ExcelBundle / Controller / FakeController.php存在
  2. 确保更新app / config config.yml并使用提供的文件更新routing.yml(Liuggio / ExcelBundle / Tests / app)
  3. 假设您正在更新产品表,请按以下步骤更新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 ] );
        }
    
    }