<?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里写入图片的更多相关文章

  1. excel中统计列中的值在其他列出现的次数多个条件

    excel中统计列中的值在其他列出现的次数多个条件 =COUNTIFS(E2:E373,"=VIP经销商",J2:J373,K2) 解释 E列的第二行到第373行中值 等于 VIP ...

  2. vue中直接修改props中的值并未给出警告,为啥?

    问:vue中直接修改props中的值并未给出警告,为啥? 答:如果props传入的值是引用类型,在子组件中改变其元素,不改变引用,那么不报错: 如果是基本类型,那么在修改时浏览器控制台会有报错信息. ...

  3. 判断修改的中的值,用前面的,否则容易获得空值;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 ...

  4. PHP 修改数组中的值

    PHP 修改数组中的值 ①.二维数组可以通过 for($i = 0; $i < count(Array()); ++ $i) 这种形式修改 实例代码: // 修改 二维数组中的 name为 Ge ...

  5. excel中使用统计列中的值在其他列出现的次数

    excel中使用统计一列的中值在其他列出现的次数 =COUNTIFS($J$:$J$,K2) 解释下 $J$2 J列中的第二行到 $J$373 J列的373行  范围内 查找 k列的第二行的值 出现的 ...

  6. Excel查看某列的重复值

    例如: 当查看的是B列的重复值时:=IF(COUNTIF(B:B,B1)>1,"重复","")

  7. ArcMap - 使用python更新列中的值

    概述:在外文网上,很多人都问在ArcMap中如何通过SQL修改属性字段的值,我见回答的人都说通过"Field Calculator",貌似不能直接通过SQL语句. 虽然学gis开发 ...

  8. MVC项目中怎样用JS导出EasyUI DataGrid为Excel

    在做一中考评项目的时候,遇到了这么一个需求.就是把评教后得到的老师的成绩导出为Excel.事实上需求非常普通.实现起来有些复杂.由于老师考评不但有固定的考核项,还有额外加分项.于是我们就抽出来了一个表 ...

  9. MySQL 查询某个列中同样值的数量统计

    数据如今是这种,我想确定出type列中的news和image....甚至以后有其它值,他们分别有多少个. SELECT type, count(1) AS counts FROM material G ...

随机推荐

  1. 洛谷P1459 三值的排序 Sorting a Three-Valued Sequence

    P1459 三值的排序 Sorting a Three-Valued Sequence 166通过 369提交 题目提供者该用户不存在 标签USACO 难度普及- 提交  讨论  题解 最新讨论 那么 ...

  2. javascript设计模式-工厂模式

    简单工厂模式:使用一个类来生成实例. 复杂工厂模式:使用子类来决定一个成员变量应该是哪个具体的类的实例. 简单工厂模式是由一个方法来决定到底要创建哪个类的实例, 而这些实例经常都拥有相同的接口.通过工 ...

  3. 保护企业的Word文档

    保护企业的Word文档 通常,我们可以对Word文件进行加密码.设置为只读.禁止复制甚至是将内容变成图片加以保护,但这仅限于个人少量文档,如果是企业每天生产大量的word文档好用这种方法就不行,今天为 ...

  4. .Net字符串驻留池

    在.Net中,对于相同的字符串,.Net会将它们指向同一个地址,它们是相同的实例..Net中的字符串并不会更新,当更改一个字符串变量时,由于字符串的不可变性,.Net实际上是新创建一个字符串,而将变量 ...

  5. win7和win8如何设置快速启动栏

    a.在任务栏上右键 -> 工具栏 -> 新建工具栏 -> 跳出选择文件夹对话框,在文件夹里面(光标山洞处)输入这个路径,然后按回车: %userprofile%\AppData\Ro ...

  6. sqlmap.config 配置

    <?xml version="1.0" encoding="utf-8"?> <sqlMapConfig xmlns="http:/ ...

  7. Recover deleted pictures in iOS 9

    A case about business secret. The suspect is an engineer in Hitec company, and compeitiors pay lots ...

  8. IEF could not decode Chinese character in IE history well

    My friend is working on some case, and she looks not in the mood. I ask her what's going on. She wan ...

  9. 014安装Linux系统到开发板

    SD卡----->开发板 1.安装准备: 硬件连接 USB下载线,一端连到开发板,另一端连到PC机: 串口线连好: 电源线连好: 设置开发板从SD卡启动: 2.打开开发板进入选单界面: 进入选单 ...

  10. MariaDB的双主

     MariaDB(mysql)之主主复制   一.主主复制说明    MySQL主主复制结构区别于主从复制结构.在主主复制结构中,两台服务器的任何一台上面的数据库存发生了改变都会同步到另一台服务器上, ...