<?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. HTML5-新API-geolocation-实例-距离跟踪器

    <body onLoad="loadDemo()"> <header> <h1>oldmeter演示</h1> <h4> ...

  2. hdu2053

    查找1-n中能整除n的数的个数. 如果是偶数的话,结果为0 奇数的话,结果为1 #include <stdio.h> int main(){ int i,cnt,n; while(~sca ...

  3. Android窗口为弹出框样式

    1.XML android:theme="@android:style/Theme.Dialog <?xml version="1.0" encoding=&quo ...

  4. [转]10分钟入门python

    本原创文章属于<Linux大棚>博客,博客地址为http://roclinux.cn.文章作者为Rocrocket Wu. 为了防止某些网站的恶性转载,特在每篇文章前加入此信息,还望读者体 ...

  5. 编程作业—C++初探 简单的学生信息处理程序实现

    简单的学生信息处理程序实现 来源: POJ (Coursera声明:在POJ上完成的习题将不会计入Coursera的最后成绩.) 注意: 总时间限制: 1000ms 内存限制: 65536kB 描述 ...

  6. 使用C#中JavaScriptSerializer类将对象转换为Json格式数据

    将对象转换为json格式字符串: private JavaScriptSerializer serializer = new JavaScriptSerializer(); protected voi ...

  7. 根据Ip获取城市帮助类

    思路构建 1.先通过本地的测IP地址库进行匹配 2.如果本地IP地址库存在此IP的城市信息,就直接返回,调用速度也快 3.如果本地没有对应的IP城市信息,必须通过调用网络的IP查询的API了,这里我使 ...

  8. Maven的HTTP代理设置

    http://blog.sina.com.cn/s/blog_4f925fc30102ed3y.html   第一.检测本地网络是否不能直接访问Maven的远程仓库,命令为ping repo1.mav ...

  9. 线程间通信--wait和notify

    使用wait.notify方法实现线程间的通信(注意这两个方法都是object的类的方法,换句话说java为所有的对象都提供了这两个方法) 1.wait和notify必须配合synchronized关 ...

  10. linux中PHP dirname(__FILE__)路径问题解决

    近期在给wordpress开发模板功能时发现,直接使用include(“文件名”)的形式调用其他php代码片段时会出现路径错误.之前服务器环境一直都是iis,未曾出现过类似的BUG,但换成linux服 ...