下载phpexecl1.7.6 这版本能设置缓存,导入的时候,内存会少一半左右,但还是很多
代码:
PHP复制代码
class Main extends CI_Controller {
function __construct (){
//必需先覆盖父类的析构函数
parent ::__construct ();
//加载session
$this->load->library('session');
//加载加密类
$this->load->library('encrypt');
}
function index (){
$Vdate=array("title"=>"XX系统");
//图片tmp path
$TmpPath="images/tmp_execl/";
//加载Execl读取自定义类
$this->load->library('phpexecl/PHPExcel');
$objReader = PHPExcel_IOFactory ::createReader('Excel5');
//$objReader->setReadDataOnly(true);
$objPHPExcel = $objReader->load('images/tmp/test.xls');
$currentSheet = $objPHPExcel->getActiveSheet();
//先处理图片
$AllImages= $currentSheet->getDrawingCollection();
$ArrayTmp="";
foreach($AllImages as $drawing){
if($drawing instanceof PHPExcel_Worksheet_MemoryDrawing ){
$image = $drawing->getImageResource();
$filename=$drawing->getIndexedFilename();
$XY=$drawing->getCoordinates();
//把图片存起来
imagepng($image, $TmpPath.$filename);
//把图片的单元格的值设置为图片名称
$cell = $currentSheet->getCell($XY);
$cell->setValue($filename);
}
}
//处理每个单元格的数据
$allColumn = $currentSheet->getHighestColumn();
$allRow = $currentSheet->getHighestRow();
for($currentRow = 1;$currentRow<=$allRow;$currentRow++){
for($currentColumn='A';$currentColumn<=$allColumn;$currentColumn++){
$address = $currentColumn.$currentRow;
//获取单元格数据
$CellDate=$currentSheet->getCell($address)->getValue();
//echo $currentSheet->getCell($address)->getValue()."\t";
//写入数据库.....
//.....
}
//echo "\n";
}
$this->load->view('main_view',$Vdate);
}
}
复制代码
整个过程就是先保存图片,改变图片单元格的值,再读取写进库里.
这个方法导入大文件效率肯定不好,有好办法的同学,大家讨论一下 |