今天在项目开发时,突然遇到了一个问题,就是一个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;
}
}
}