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 ...
随机推荐
- html5相关
1.语义化标签: header\section\aside\footer 2.音视频播放: video\audio 其中video支持ogg\mpeg4\webm格式的视频: 3.canvas 一.画 ...
- Duilib学习笔记《03》— 控件使用
在前面已经对duilib有个一个基本的了解,并且创建了简单的空白窗体.这仅仅只是一个开始,如何去创建一个绚丽多彩的界面呢?这就需要一些控件元素(按钮.文本框.列表框等等)来完善. 一. Duilib控 ...
- openstack openrpc
- OpenGL函数解析之glLoadIdentity()
函数原型:void glLoadIdentity(void) 函数说明:调用glLoadIdentity()函数可以恢复初始坐标系,用一个4x4的单位矩阵来代替当前矩阵,实际上就是对当前矩阵进行初始化 ...
- Loadrunner:安装LR11时提示缺少vc2005_sp1_with_atl_fix_redist
[问题现象] 安装LR11时提示缺少vc2005_sp1_with_atl_fix_redist: [解决办法] 手动安装缺少的组件,LR安装包中已自带该组件,为何不自动捕捉异常去获取该自带的组件去安 ...
- hbase很有价值的读写性能提升
NoSQL现在风生水起,hbase的使用也越来越广,但目前几乎所有的NoSQL产品在运维上都没法和DB相提并论,在这篇blog中来总结下我们在运维hbase时的一些问题以及解决的方法,也希望得到更多h ...
- Mybatis SQL语句查询
MyBatis中使用in查询时的注意事项 foreach的主要用在构建in条件中,它可以在SQL语句中进行迭代一个集合. foreach一共有三种类型,分别为List,[](array),Map三种. ...
- js动态改变图片热区坐标,手机端图片热区自适应
<img id='banner1' src="images/banner.jpg" usemap="#banner" border="0&quo ...
- linux下最大文件数
系统级:系统级设置对所有用户有效.可通过两种方式查看系统最大文件限制1 cat /proc/sys/fs/file-max 2 sysctl -a 查看结果中fs.file-max这项的配置数量如果需 ...
- Custom Sort Order
When trying to sort based on values that do not fit the standard ascending and descending sort logic ...