// 注:只是在此做下记录,有兴趣的可以参考,不做实际教程文档
<?php
/**
* Created by IntelliJ IDEA.
* User: davis
* Date: 2019-03-12
* Time: 16:01
*/ namespace CommonBundle\Command; use AdminBundle\Entity\LiteList;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Wiz\AdminTools\Bundle\AdminToolsBundle\Entity\DictionaryTranslation; class ImportLiteListCommand extends ContainerAwareCommand
{
protected function configure()
{
$this
->setName('isa:import-lite-list')
->addArgument('path', InputArgument::OPTIONAL, '键入Excel文件路径!')
->setDescription('import lite List data');
} protected function execute(InputInterface $input, OutputInterface $output)
{
$em = $this->getContainer()->get('doctrine.orm.entity_manager');
$path = $input->getArgument('path');
if (file_exists($path) && is_dir($path) == false && pathinfo($path, PATHINFO_EXTENSION) == 'xlsx') {
//导入数据前,先清空表,并把自增id重置
$emConn = $this->getContainer()->get('doctrine.orm.entity_manager')->getConnection();
$emConn->executeQuery("DELETE FROM `isa_lite_list`");
$emConn->executeQuery("alter table `isa_lite_list` auto_increment =1;");
$emConn->executeQuery("alter table `isa_lite_list_translation` auto_increment =1;"); $excel = new \PHPExcel();
try {
$inputFileType = \PHPExcel_IOFactory::identify($path);
$objReader = \PHPExcel_IOFactory::createReader($inputFileType);
$excel = $objReader->load($path);
} catch (\Exception $e) {
$output->writeln('<error>文件加载失败!</error>');
die;
}
$sheet = $excel->getSheet(0);
$highestRow = $sheet->getHighestRow(); //读取到excel中数据
for ($i = 1; $i <= $highestRow; $i++) {
$title = $excel->getActiveSheet()->getCell("A" . $i)->getValue();
$energy = sprintf("%.2f", $excel->getActiveSheet()->getCell("B" . $i)->getValue());
$water = sprintf("%.2f", $excel->getActiveSheet()->getCell("C" . $i)->getValue());
//写入table中
$liteList = new LiteList();
$trans = new DictionaryTranslation();
$liteList->setSlug('Lite-' . $i);
$liteList->setStatus(2);
$liteList->setEnergySaving($water);
$liteList->setWaterSaving($energy);
$liteList->setInputAt(new \DateTime("-{$i} second"));
$trans->setLocale('en');
$trans->setTranslatable($liteList);
$liteList->translate('en')->setTitle($title);
$liteList->mergeNewTranslations();
$em->persist($liteList);
$em->flush();
if ($i == 1)
$output->writeln('<comment>数据导入中...</comment>');
}
$output->writeln('<info>Success!!</info>');
} else {
$output->writeln('<error>文件类型或路径有错,请检查!!</error>');
}
}
} //help

//在命令行执行脚本,数据导入成功


最新文章

  1. Ionic Lab下载地址
  2. AC日记——与7无关的数 openjudge 1.5 39
  3. 【原】iOS下KVO使用过程中的陷阱
  4. weka特征选择(IG、chi-square)
  5. 最新的windows xp sp3序列号(绝对可通过正版验证)
  6. JS的强大
  7. 20145129 《Java程序设计》第1周学习总结
  8. 增强Eclipse ,MyEclipse 的代码自动提示功能
  9. Android专项面试训练题(一)
  10. Oracle 10gR2 Dataguard搭建(非duplicate方式)
  11. 发布系统Git使用指南 - the Git Way to Use Git
  12. windows 连接Linux
  13. springmvc 项目完整示例04 整合mybatis mybatis所需要的jar包 mybatis配置文件 sql语句 mybatis应用
  14. 字符IO流
  15. poj3630 Phone List
  16. .Net MVC 当前上下文中不存在名称“Style”
  17. impala记录-安装kudu和impala
  18. Java的初始化执行顺序(父类static变量-&gt;子类static变量-&gt;父类成员变量-&gt;父类构造器-&gt;成员变量-&gt;构造器-&gt;main函数)
  19. Android四大组件应用系列——实现电话拦截和电话录音
  20. doT.js模板和pagination分页应用

热门文章

  1. Kali-linux使用Wifite破解无线网络
  2. 算法——(5)B/B+/红黑树
  3. python中matplotlib总结
  4. 【hdu 3177 Crixalis&#39;s Equipment】 题解
  5. 如何处理Entity Framework / Entity Framework Core中的DbUpdateConcurrencyException异常(转载)
  6. 【Dubbo源码阅读系列】服务暴露之本地暴露
  7. (Les16 执行数据库恢复)-控制文件恢复
  8. vue-cli3 创建选项选择
  9. 如何使用yii2的缓存依赖特性
  10. Delphi 调试连接 任意Android手机/平板/盒子