thinkphp用phpexcel读取excel,并修改列中的值,再导出excel,带往excel里写入图片
<?php class GetpriceAction extends AdministratorAction { // 文件保存路径
protected $savepath;
// 允许上传的文件类型
protected $allowFileType; public function _initialize(){
parent::_initialize();
$this->savepath = './xxx/'.date('Ymd').'/';
$this->allowFileType = array('xlsx','xls');
} public function index(){
$this->display();
} public function upload(){
$beginLine = intval($_POST['beginLine']);
$bnCol = htmlspecialchars(strtoupper($_POST['bnCol']));
// $numCol = htmlspecialchars(strtoupper($_POST['numCol'])); if($beginLine<1){
$this->dialogmessage('须正确填写开始行数','error');
}
if(!preg_match('/^[a-zA-Z]+$/',$bnCol)) {
$this->dialogmessage('须正确填写货号所在列', 'error');
} $savepath = $this->savepath;
mk_dir($savepath);
import("ORG.Net.UploadFile");
$upload = new UploadFile();
$upload->maxSize = 114570;
$upload->allowExts = $this->allowFileType;
$upload->savePath = $savepath.''; // 讴置附件上传目录
if(!$upload->upload()){
$this->dialogmessage($upload->getErrorMsg(),'excel');
exit();
}else{
$fileInfo = $upload->getUploadFileInfo();
$data = $this->editStore($fileInfo,$beginLine,$bnCol);
}
$this->assign($data);
$this->display();
} /**
* $info
*
* @param $fileInfo
* @param $beginLine
* @param $bnCol
* @return array
*/
protected function editStore($fileInfo,$beginLine,$bnCol){
require_once './excel/Classes/PHPExcel.php';
$filePath = $fileInfo[0]['savepath'].$fileInfo[0]['name'];
chmod($filePath,0777);
//建立reader对象
$PHPReader = new PHPExcel_Reader_Excel2007();
if(!$PHPReader->canRead($filePath)){
$PHPReader = new PHPExcel_Reader_Excel5();
if(!$PHPReader->canRead($filePath)){
$this->dialogmessage('no excel','error');
}
} //建立excel对象,此时你即可以通过excel对象读取文件,也可以通过它写入文件
$PHPExcel = $PHPReader->load($filePath); /**读取excel文件中的第一个工作表*/
$currentSheet = $PHPExcel->getSheet(0);
/**取得最大的列号*/
$allColumn = $currentSheet->getHighestColumn();
/**取得一共有多少行*/
$allRow = $currentSheet->getHighestRow();
//循环读取每个单元格的内容。注意行从1开始,列从A开始
$codes=array();
for($rowIndex=$beginLine;$rowIndex<=$allRow;$rowIndex++){
$bn=$currentSheet->getCell($bnCol.$rowIndex)->getValue();
$b = $currentSheet->getCell('B'.$rowIndex)->getValue();
$c = $currentSheet->getCell('C'.$rowIndex)->getValue();
$d = $currentSheet->getCell('D'.$rowIndex)->getValue();
$e = $currentSheet->getCell('E'.$rowIndex)->getValue();
if($bn){
$f = model('Product')->where('bn=\''.$bn.'\'')->getField('price');
$codes[] = array('a'=>$bn,'b'=>$b,'c'=>$c,'d'=>$d,'e'=>$e,'f'=>$f);
}
} $data['data'] = $codes;
$data['title'] = array('货号','1','1','1','1','销售价');
$data['width'] = array(30,30,30,30,30,30);
$add = array();
$add['ctime'] = time();
$add['data'] = serialize($data);
$add['title'] = '价格' . date('Ymdhis');
$name = uniqid();
$add['file'] = SITE_PATH . '/Public/excel/' . $name . '.xls';
$add['url'] = SITE_URL . '/Public/excel/' . $name . '.xls';
$add['uid'] = 1;
$id = M('excel')->add($add);
if ($id > 0) {
pclose(popen('/usr/bin/php -f /xxx/exceltask.php ' . $id . ' &', 'r'));
echo '导出任务成功,请等待系统完成';
exit;
} else {
echo '导出任务失败';
exit;
} } /**
* 写入日志
* @param Array $arrContent
* @param String $filename
* @return int
*/
protected function writeLog($arrContent,$filename){
$intLogStatus = file_put_contents($this->savepath.$filename.'.php','<?php '.PHP_EOL.' return '.
var_export($arrContent,true).';');
return $intLogStatus;
}
}
/xxx/exceltask.php
<?php
error_reporting(0);
date_default_timezone_set('RPC');
define('SITE_PATH',dirname(__FILE__));
include SITE_PATH.'/db.class.php'; $id=intval($argv[1]);
if($id<=0){
exit();
} $exceldata=M('excel')->where('id='.$id)->find();
$data=unserialize($exceldata['data']);
include SITE_PATH.'/libs/PHPExcel.php';
$objExcel=new PHPExcel();
$columns=array('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','AA','AB','AC','AD','AE','AF','AG','AH','AI','AJ','AK','AL','AM','AN','AO','AP','AQ','AR','AS','AT','AU','AV','AW','AX','AY','AZ','BA','BB','BC','BD','BE','BF','BG','BH','BI','BJ','BK','BL','BM','BN','BO','BP','BQ','BR','BS','BT','BU','BV','BW','BX','BY','BZ');
$objExcel->getProperties()->setCreator("xxx");
$objExcel->getProperties()->setLastModifiedBy("xxx");
$objExcel->getProperties()->setTitle($title);
$objExcel->getProperties()->setSubject($title);
$objExcel->getProperties()->setDescription($title);
$objExcel->getProperties()->setKeywords($title);
$objExcel->getProperties()->setCategory($title);
$objExcel->setActiveSheetIndex(0);
foreach($data['title'] as $k=>$v){
$objExcel->getActiveSheet()->setCellValue($columns[$k].'1',$v);
}
if($data['width']){
foreach($data['width'] as $k=>$v){
$objExcel->getActiveSheet()->getColumnDimension($columns[$k])->setWidth($v);
}
}
$i=2;
foreach($data['data'] as $v){
$v=array_values($v);
foreach($v as $k=>$vv){
preg_match_all('/http:\/\/img\.xxx\.com\//', $vv, $matches);
if($matches[0]){
$objExcel->getActiveSheet()->getRowDimension($i)->setRowHeight(100);
$objDrawing = new PHPExcel_Worksheet_Drawing();
$file=SITE_PATH.'/tmp/'.uniqid().'.jpg';
file_put_contents($file,file_get_contents($vv));
$objDrawing->setPath($file);
$objDrawing->setCoordinates($columns[$k].$i);
$objDrawing->setOffsetX(10);
$objDrawing->setOffsetY(10);
$objDrawing->setWidth(100);
$objDrawing->setHeight(100);
$objDrawing->setWorksheet($objExcel->getActiveSheet());
unset($objDrawing); }
else{
if(is_numeric($vv)){
//$vv=number_format($vv,2);
$objExcel->getActiveSheet()->setCellValueExplicit($columns[$k].$i,$vv);
}
else{
$objExcel->getActiveSheet()->setCellValueExplicit($columns[$k].$i,$vv,PHPExcel_Cell_DataType::TYPE_STRING);
}
} }
$i++;
}
$objWriter = PHPExcel_IOFactory::createWriter($objExcel, 'Excel5');
$objWriter->save($exceldata['file']);
M('excel')->where('id='.$id)->setField('endtime',time());
exit;
?>
thinkphp用phpexcel读取excel,并修改列中的值,再导出excel,带往excel里写入图片的更多相关文章
- excel中统计列中的值在其他列出现的次数多个条件
excel中统计列中的值在其他列出现的次数多个条件 =COUNTIFS(E2:E373,"=VIP经销商",J2:J373,K2) 解释 E列的第二行到第373行中值 等于 VIP ...
- vue中直接修改props中的值并未给出警告,为啥?
问:vue中直接修改props中的值并未给出警告,为啥? 答:如果props传入的值是引用类型,在子组件中改变其元素,不改变引用,那么不报错: 如果是基本类型,那么在修改时浏览器控制台会有报错信息. ...
- 判断修改的中的值,用前面的,否则容易获得空值;this.dataGridView1.Rows[i].Cells[0].EditedFormattedValue; VS bool b = (bool)this.dataGridView1.Rows[i].Cells[0].Value;
判断修改的中的值,用前面的,否则容易获得空值:this.dataGridView1.Rows[i].Cells[0].EditedFormattedValue; VS bool b = (b ...
- PHP 修改数组中的值
PHP 修改数组中的值 ①.二维数组可以通过 for($i = 0; $i < count(Array()); ++ $i) 这种形式修改 实例代码: // 修改 二维数组中的 name为 Ge ...
- excel中使用统计列中的值在其他列出现的次数
excel中使用统计一列的中值在其他列出现的次数 =COUNTIFS($J$:$J$,K2) 解释下 $J$2 J列中的第二行到 $J$373 J列的373行 范围内 查找 k列的第二行的值 出现的 ...
- Excel查看某列的重复值
例如: 当查看的是B列的重复值时:=IF(COUNTIF(B:B,B1)>1,"重复","")
- ArcMap - 使用python更新列中的值
概述:在外文网上,很多人都问在ArcMap中如何通过SQL修改属性字段的值,我见回答的人都说通过"Field Calculator",貌似不能直接通过SQL语句. 虽然学gis开发 ...
- MVC项目中怎样用JS导出EasyUI DataGrid为Excel
在做一中考评项目的时候,遇到了这么一个需求.就是把评教后得到的老师的成绩导出为Excel.事实上需求非常普通.实现起来有些复杂.由于老师考评不但有固定的考核项,还有额外加分项.于是我们就抽出来了一个表 ...
- MySQL 查询某个列中同样值的数量统计
数据如今是这种,我想确定出type列中的news和image....甚至以后有其它值,他们分别有多少个. SELECT type, count(1) AS counts FROM material G ...
随机推荐
- 在ASPxGridView的主从表显示中,有什么属性可以只让其每次选择只展开一列?
在ASPxGridView的主从表显示中,有什么属性可以只让其每次选择只展开一列?(效果图如下:): 在ASPxGridView的主从表显示中,有什么属性可以只让其每次选择只展开一列(效果图) 实现该 ...
- 实时显示GetLastError值
在Watch窗口中输入$err,hr 自动显示上个函数返回值
- asp.net 在线人数
很网站都有在线人数,这一功能无处不在.现在,我们就介绍在.NET中一个简单明了的方法来统计在线用户的多少,该方法的特点就是充分的利用了ASP.NET的特点,结合global.asax文件,用Appli ...
- JB for iOS 9.3
令人期待已久的JB再度现世,真是望穿秋水,千呼万唤始出来~ http://www.pangu.io/?flag=cn JB的过程顺利不卡关. 完成后就可以看到心爱的Cydia出现在桌面上了. 还需要A ...
- leetcode 8
string类型转换为int类型,需要考虑不同的转换情况. “ 04” 转换结果 4: “ 4 43” 转换结果 4: “a@12 ” 转换结果 0: “12a” ...
- 理解DOM中的事件流
浏览器发展到第四代时(IE4和Netscape Communicator 4),浏览器团队遇到一个很有意思的问题:页面的哪一部分会拥有特定的事件?想象下在一张纸上有一组同心圆,如果你把手指放在圆心上, ...
- CentOS编译安装lamp
LAMP环境搭建(编译安装CentOS+httpd2.2+mysql5.5+php5.4) 首先准备以下压缩包 <ignore_js_op> (1)编译安装apache 1.配置防火墙,开 ...
- Linux使用Shell脚本实现ftp的自动上传下载
1. ftp自动登录批量下载文件. #####从ftp服务器上的/home/data 到 本地的/home/databackup#####!/bin/bashftp -n<<!open 1 ...
- sql server 查询表信息
SELECT '表名' = e.[name], '表说明' = f.[value], '字段序号' = a.colorder, '字段名' = a.[name], '字段类型' = b.[name], ...
- 关于js一般对象与标配对象
当一个js函数对象被创建时,Function 构造器产生的函数对象会运行类似这样的一些代码 this.prototype={constructor:this} 新函数被赋予了一个prototype属性 ...