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

问题描述:

没有Symfony,这是我将excel数据发布到数据库的方式。如何使用liuggio/ExcelBundle将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

你应该用** Symfony发布你试过的代码**,并确切指定“_doesn't没有任何工作。”。阅读[如何创建一个最小,完整和可验证的示例](http://*.com/help/mcve)并相应地将问题更新到[如何提问](http://*.com/help/)如何提问)指令。 –

+0

@gp_sflover,如果我发布了我一直在尝试的内容,我会使所有内容复杂化。我如何用Symfony完成上述任务是我的问题。谢谢 – maestro

+0

您是否按照安装说明操作?你试过的代码在哪里?如果它不在控制器中不起作用 –

我工作了这一点,如下所示:

  1. 确保路径Liuggio/ExcelBundle /控制器/ FakeController.php存在
  2. 请确保您更新的应用程序/配置config.yml和routing.yml中是与所提供的文件更新(Liuggio/ExcelBundle /测试/应用)
  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 ]); 
        } 
    
    } 
    
+0

非常感谢你......你节省了我的时间。 – Josiah

+0

我可以在symfony 3.3中使用它吗? –

+0

是的,你可以。这就是我正在使用的。 – maestro