在 Symfony Command中自定义脚本把Excel数据导入到数据库中
2024-08-21 18:17:40
// 注:只是在此做下记录,有兴趣的可以参考,不做实际教程文档
<?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
//在命令行执行脚本,数据导入成功
最新文章
- Ionic Lab下载地址
- AC日记——与7无关的数 openjudge 1.5 39
- 【原】iOS下KVO使用过程中的陷阱
- weka特征选择(IG、chi-square)
- 最新的windows xp sp3序列号(绝对可通过正版验证)
- JS的强大
- 20145129 《Java程序设计》第1周学习总结
- 增强Eclipse ,MyEclipse 的代码自动提示功能
- Android专项面试训练题(一)
- Oracle 10gR2 Dataguard搭建(非duplicate方式)
- 发布系统Git使用指南 - the Git Way to Use Git
- windows 连接Linux
- springmvc 项目完整示例04 整合mybatis mybatis所需要的jar包 mybatis配置文件 sql语句 mybatis应用
- 字符IO流
- poj3630 Phone List
- .Net MVC 当前上下文中不存在名称“Style”
- impala记录-安装kudu和impala
- Java的初始化执行顺序(父类static变量->;子类static变量->;父类成员变量->;父类构造器->;成员变量->;构造器->;main函数)
- Android四大组件应用系列——实现电话拦截和电话录音
- doT.js模板和pagination分页应用
热门文章
- Kali-linux使用Wifite破解无线网络
- 算法——(5)B/B+/红黑树
- python中matplotlib总结
- 【hdu 3177 Crixalis&#39;s Equipment】 题解
- 如何处理Entity Framework / Entity Framework Core中的DbUpdateConcurrencyException异常(转载)
- 【Dubbo源码阅读系列】服务暴露之本地暴露
- (Les16 执行数据库恢复)-控制文件恢复
- vue-cli3 创建选项选择
- 如何使用yii2的缓存依赖特性
- Delphi 调试连接 任意Android手机/平板/盒子