前提:有個需求需要實現(xiàn)帶圖片的excel數(shù)據(jù)導入數(shù)據(jù)庫中,發(fā)現(xiàn)PHPExcel - Excel 操作庫已經(jīng)停止維護,在PHP8的版本中,有些語法不支持,報錯一堆,改了一堆,又還有一堆。所以決定找個替代的擴展:phpoffice/phpspreadsheet
技術背景前提:
- ThinkPHP8.0
- php8.0.2nts
- “phpoffice/phpspreadsheet”: “^1.20.0”
在composer.json中加入"phpoffice/phpspreadsheet": "^1.20.0"
后composer update
,如果選擇的phpoffice/phpspreadsheet版本不同,可能又有不同的坑存在,得進行一一排查。這里記錄下。文章來源:http://www.zghlxwxcb.cn/news/detail-790304.html
實現(xiàn)步驟看代碼注釋部分,實現(xiàn)代碼如下:文章來源地址http://www.zghlxwxcb.cn/news/detail-790304.html
/**
* 字母序列化為數(shù)字
*/
public function ABC2decimal($abc)
{
$ten = 0;
$len = strlen($abc);
for ($i = 1; $i <= $len; $i++) {
$char = substr($abc, 0 - $i, 1);//反向獲取單個字符
$int = ord($char);
$ten += ($int - 65) * pow(26, $i - 1);
}
return $ten;
}
public function excel()
{
if ($file = request()->file('excel')) {
try {
$saveName = Filesystem::disk('public')->putFile('/static/upload/excels', $file);
if (!is_file($saveName)) {
return json(['code' => 1, 'msg' => '文件不存在', 'data' => null]);
}
$ext = pathinfo($saveName, PATHINFO_EXTENSION);
// xls不支持圖片導入
// if (!in_array($ext, ['xlsx', 'xls'])) {
if (!in_array($ext, ['xlsx'])) {
return json(['code' => 1, 'msg' => '文件類型不正確', 'data' => null]);
}
// 有兩種格式,xlsx和xls
if ($ext == 'xlsx') {
$objReader = IOFactory::createReader('Xlsx');
} else {
$objReader = IOFactory::createReader('Xls');
}
// 圖片保存路徑
$imageFilePath1 = root_path() . '/public/'; // 圖片保存目錄
$imageFilePath2 = 'static/upload/images/' . date("Ymd") . '/';
$imageFilePath = $imageFilePath1 . $imageFilePath2;
if (!file_exists($imageFilePath)) {
mkdir("$imageFilePath", 0777, true);
}
// 載入excel文件
$excel = $objReader->load($saveName);
// 讀取第一張表
$sheet = $excel->getActiveSheet();
// 讀取總行數(shù)
$highestRow = $sheet->getHighestRow();
// 讀取第一張表轉(zhuǎn)換成數(shù)組
$data = $sheet->toArray();
// 處理圖片
foreach ($sheet->getDrawingCollection() as $drawing) {
list($startColumn, $startRow) = Coordinate::coordinateFromString($drawing->getCoordinates());
$imageFileName = $drawing->getIndexedFilename(); // 獲取文件名
switch ($drawing->getExtension()) {
case 'jpg':
case 'jpeg':
$source = imagecreatefromjpeg($drawing->getPath());
imagejpeg($source, $imageFilePath . $imageFileName);
break;
case 'gif':
$source = imagecreatefromgif($drawing->getPath());
imagegif($source, $imageFilePath . $imageFileName);
break;
case 'png':
$source = imagecreatefrompng($drawing->getPath());
imagepng($source, $imageFilePath . $imageFileName);
break;
}
$startColumn = $this->ABC2decimal($startColumn);
$data[$startRow - 1][$startColumn] = $imageFilePath2 . $imageFileName;
}
// 數(shù)據(jù)寫入數(shù)據(jù)庫
$add_data = [];
for ($i = 1; $i <= $highestRow - 1; $i++) {
$add_data[$i]['institution'] = $data[$i][0];
$add_data[$i]['name'] = $data[$i][1];
$add_data[$i]['sex'] = $data[$i][2];
$add_data[$i]['phone'] = $data[$i][3];
$add_data[$i]['id_type'] = $data[$i][4];
$add_data[$i]['id_no'] = $data[$i][5];
$add_data[$i]['occupation'] = $data[$i][6];
$add_data[$i]['job'] = $data[$i][7];
$add_data[$i]['skill_level'] = $data[$i][8];
$add_data[$i]['certificate_no'] = $data[$i][9];
$add_data[$i]['create_time'] = $data[$i][10];
$add_data[$i]['start_time'] = $data[$i][11];
$add_data[$i]['end_time'] = $data[$i][12];
$add_data[$i]['path'] = $data[$i][13];
}
// 數(shù)據(jù)插入數(shù)據(jù)庫
$success_count = Db::table('certificate')->insertAll($add_data);
if ($success_count > 0) {
return json(['code' => 0, 'msg' => '數(shù)據(jù)插入成功', 'data' => null]);
} else {
return json(['code' => 1, 'msg' => '數(shù)據(jù)插入失敗', 'data' => null]);
}
} catch (\Exception $e) {
return json(['code' => 1, 'msg' => $e->getMessage(), 'data' => null]);
}
} else {
return json(['code' => 1, 'msg' => '上傳文件不能為空', 'data' => null]);
}
}
到了這里,關于PHP中excel帶圖片數(shù)據(jù)導入的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!