您的位置:首页 > 编程语言 > PHP开发

PHPExcel 读取文字+图片,保存数据,储存图片的例子

2015-11-28 09:56 609 查看
今天在项目开发时,突然遇到了一个问题,就是一个excel需要保存文字和图片,把文字和图片分别存入数据库,然后就google了一下,经过分析,弄出了一个简单的案例,自己在这里mark一下。

思路如下:先获取图片,保存到本地,然后把图片替换成图片名称,然后再遍历数据,存储数据

去PHPExcel下载:https://phpexcel.codeplex.com/

$xlsFile = 'a.xlsx';
require_once 'PHPExcel/Reader/Excel2007.php';
$PHPReader = new PHPExcel_Reader_Excel2007();
//$objReader->setReadDataOnly(true);
$PHPExcel = $PHPReader->load($xlsFile);

$objWorksheet = $PHPExcel->getActiveSheet();

foreach ($objWorksheet->getDrawingCollection() as $drawing) {
//for XLSX format
$string = $drawing->getCoordinates();
$coordinate = PHPExcel_Cell::coordinateFromString($string);
if ($drawing instanceof PHPExcel_Worksheet_Drawing){
$filename = $drawing->getPath();
$drawing->getDescription();
copy($filename, 'uploads/' . $drawing->getDescription());
$cell = $objWorksheet->getCell($string);
$cell->setValue($drawing->getDescription());
}
}

$currentSheet = $PHPExcel->getSheet(0);
$allColumn = $currentSheet->getHighestColumn();
$allRow = $currentSheet->getHighestRow();
for($rowIndex=1;$rowIndex<=$allRow;$rowIndex++){
for($colIndex='A';$colIndex<=$allColumn;$colIndex++){
$addr = $colIndex.$rowIndex;
$cell = $currentSheet->getCell($addr)->getValue();
if($cell instanceof PHPExcel_RichText){
$cell = $cell->__toString();
echo $cell;
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  PHPExcel php 文字 图片