- Phpexcel 类库下载链接:
https://www.php.cn/xiazai/leiku
-
目录
-
只需把class文件拿出 放在 tp5 目录下 vendor ,重命名 PHPExcel;
-
数据库 和 excel
-
导出excel(不带图片)
public function excelText()
{
$res = Db::table('think_image')->select();
vendor("PHPExcel.PHPExcel");
$resultPHPExcel = new \PHPExcel();
//设置参数
//设值
$resultPHPExcel->getActiveSheet()->setCellValue('A1', '季度');
$resultPHPExcel->getActiveSheet()->setCellValue('B1', '名称');
$resultPHPExcel->getActiveSheet()->setCellValue('C1', '数量');
$i = 2;
foreach ($res as $item) {
$resultPHPExcel->getActiveSheet()->setCellValue('A' . $i, $item['id']);
$resultPHPExcel->getActiveSheet()->setCellValue('B' . $i, $item['name']);
$resultPHPExcel->getActiveSheet()->setCellValue('C' . $i, $item['image']);
$i++;
}
//设置导出文件名
$outputFileName = 'total.xls';
$xlsWriter = new \PHPExcel_Writer_Excel5($resultPHPExcel);
ob_end_clean();
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
header('Content-Disposition:inline;filename="' . $outputFileName . '"');
header("Content-Transfer-Encoding: binary");
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Pragma: no-cache");
$xlsWriter->save("php://output");
}
- 导出带图片的那种
//导出图片
public function excelPhoto()
{
$res = Db::table('think_image')->select();
vendor("PHPExcel.PHPExcel");
$resultPHPExcel = new \PHPExcel();
//设置参数
//设值
$resultPHPExcel->getActiveSheet()->setCellValue('A1', 'id');
$resultPHPExcel->getActiveSheet()->setCellValue('B1', '名称');
$resultPHPExcel->getActiveSheet()->setCellValue('C1', '图片');
$i = 2;
foreach ($res as $item) {
$resultPHPExcel->getActiveSheet()->setCellValue('A' . $i, $item['id']);
$resultPHPExcel->getActiveSheet()->setCellValue('B' . $i, $item['name']);
$objDrawing = new \PHPExcel_Worksheet_Drawing();
$objDrawing->setPath('.' . $item['image']);//这里拼接 . 是因为要在根目录下获取
// 设置宽度高度
$objDrawing->setHeight(50);//照片高度
$objDrawing->setWidth(50); //照片宽度
/*设置图片要插入的单元格*/
$objDrawing->setCoordinates('C' . $i);
// 图片偏移距离
$objDrawing->setOffsetX(0);
$objDrawing->setOffsetY(0);
$objDrawing->setWorksheet($resultPHPExcel->getActiveSheet());
$i++;
}
//设置导出文件名
$outputFileName = 'total.xls';
$xlsWriter = new \PHPExcel_Writer_Excel5($resultPHPExcel);
ob_end_clean();
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
header('Content-Disposition:inline;filename="' . $outputFileName . '"');
header("Content-Transfer-Encoding: binary");
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Pragma: no-cache");
$xlsWriter->save("php://output");
}
6.导入不带图片那种
//导入excel文本
public function import(Request $request)
{
//导入数据
if (request()->isPost()) {
vendor("PHPExcel.PHPExcel"); //方法一
$objPHPExcel = new \PHPExcel();
//获取表单上传文件
$file = request()->file('excel');
$info = $file->validate(['ext' => 'xlsx'])->move(ROOT_PATH . 'public'); //上传验证后缀名,以及上传之后移动的地址 E:\wamp\www\bick\public
if ($info) {
$exclePath = $info->getSaveName(); //获取文件名
$file_name = ROOT_PATH . 'public' . DS . 'excel'.$exclePath;//上传文件的地址
$objReader = \PHPExcel_IOFactory::createReader("Excel2007");
$obj_PHPExcel = $objReader->load($file_name, $encode = 'utf-8'); //加载文件内容,编码utf-8
$excel_array = $obj_PHPExcel->getSheet(0)->toArray(); //转换为数组格式
array_shift($excel_array); //删除第一个数组(标题);
$city = [];
$i = 0;
foreach ($excel_array as $k => $v) {
$city[$k]['id'] = $v[0];
$city[$k]['name'] = $v[1];
$city[$k]['image'] = $v[2];
$i++;
}
Db::table("think_image")->insertAll($city);
} else {
echo $file->getError();
}
}
}
7.导入带图片的那种
//导入excel图片
public function importPhoto(Request $request)
{
vendor('PHPExcel.PHPExcel');
vendor('PHPExcel.PHPExcel.PHPExcel_IOFactory');
vendor('PHPExcel.PHPExcel.PHPExcel_Cell');
//从前台获取excel文件
$file = request()->file('excel');
//上传该文件
$path = ROOT_PATH . 'public' . DS . 'excel'.DS;
$info = $file->move(ROOT_PATH . 'public' . DS . 'excel');//上传位置
if($info){
echo '文件上传成功'.'<br/>';
}
else{
echo '文件上传失败'.'<br/>';
}
//上传后的EXCEL路径
$file_path = $path . $info->getSaveName();
//图片配置******************
//设置一个存放图片的路径
$imgPath = ROOT_PATH . 'public' . DS .'images'.DS;
if(!file_exists($imgPath)){
mkdir($imgPath);
}
//获取文件后缀:xls、xlsx等
$extension = strtolower(pathinfo($file_path, PATHINFO_EXTENSION) );
//加上这个判断的目的就是防止报错,但目前只支持Excel5
if($extension =='xls'){
$objReader = \PHPExcel_IOFactory::createReader('Excel5');
}else{
$objReader = \PHPExcel_IOFactory::createReader('excel2007');
}
//载入文件
$objPHPExcel = $objReader->load($file_path);
foreach ($objPHPExcel->getSheet(0)->getDrawingCollection() as $k => $drawing) {
$filename = $drawing->getIndexedFilename(); //文件名
//如果多张图片,就存入数组
$filenames[]= '/images/'.$filename;
ob_start();
call_user_func(
$drawing->getRenderingFunction(),
$drawing->getImageResource()
);
$imageContents = ob_get_contents();
file_put_contents($imgPath.$filename,$imageContents); //把图片保存到本地(上方自定义的路径)
ob_end_clean();
}
//图片配置******************
//
// halt($filenames);
foreach($objPHPExcel->getSheet(0)->getDrawingCollection() as $img) {
list($startColumn,$startRow)= \PHPExcel_Cell::coordinateFromString($img->getCoordinates());//获取图片所在行和列
$imageFileName = $img->getCoordinates() . mt_rand(100, 999);
$imageFileName.='.'.substr($img->getMimeType(),6);
imagepng($img->getImageResource(),$imgPath.$imageFileName);
$startColumn = $this->ABC2decimal($startColumn);//由于图片所在位置的列号为字母,转化为数字
$data[$startRow-1][$startColumn]=$imgPath.$imageFileName;//把图片插入到数组中
}
ksort($data);
halt($data);
}
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;
}
注意:带图片的好像只支持 。.xls 格式的。第一个我已经把图片放到一个数组里面了,只有图片的操作。想要入库结合一个不带图片的,数组合并 insert 就可以入库了。
打完收工》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》