如何使用liuggio/ExcelBundle到postexcel数据到数据库
How to use liuggio/ExcelBundle to post excel data to database
没有 Symfony,这就是我 post 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');
如何完成这个任务?
我的计算如下:
- 确保路径 Liuggio/ExcelBundle/Controller/FakeController.php 存在
- 确保更新 app/config config.yml 和 routing.yml 使用提供的文件更新 (Liuggio/ExcelBundle/Tests/app)
假设您正在更新产品table,更新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,这就是我 post 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');
如何完成这个任务?
我的计算如下:
- 确保路径 Liuggio/ExcelBundle/Controller/FakeController.php 存在
- 确保更新 app/config config.yml 和 routing.yml 使用提供的文件更新 (Liuggio/ExcelBundle/Tests/app)
假设您正在更新产品table,更新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 ] ); } }