thinkphp5中使用excel导出数据表格(包涵图片)

首先使用composer require phpoffice/phpexcel下载安装phpexcel包。

将包放入extend下面。

不附加图片的导出

/**
* 导出excel(不带图片)
* @param string $fileName [description]
* @param [type] $headArr [description]
* @param [type] $data [description]
* @return [type] [description]
*/
public function excelExport($fileName = '', $headArr = [], $data = []) {

$fileName .= "_" . date("Y_m_d", time()) . "_".time().".xls";

Loader::import("PHPExcel.Classes.PHPExcel");
Loader::import("PHPExcel.Drawing");

$objPHPExcel = new \PHPExcel;

$objPHPExcel->getProperties();

$key = ord("A"); // 设置表头

foreach ($headArr as $v) {

$colum = chr($key);

$objPHPExcel->setActiveSheetIndex(0)->setCellValue($colum . '1', $v);

$objPHPExcel->setActiveSheetIndex(0)->setCellValue($colum . '1', $v);

$key += 1;

}


$column = 2;

$objActSheet = $objPHPExcel->getActiveSheet();

foreach ($data as $key => $rows) { // 行写入

  $span = ord("A");

  foreach ($rows as $keyName => $value) { // 列写入

    $objActSheet->setCellValue(chr($span) . $column, $value);

    $span++;

  }

  $column++;

}

$fileName = iconv("utf-8", "gb2312", $fileName); // 重命名表

$objPHPExcel->setActiveSheetIndex(0); // 设置活动单指数到第一个表,所以Excel打开这是第一个表

header('Content-Type: application/vnd.ms-excel');

header("Content-Disposition: attachment;filename='$fileName'");

header('Cache-Control: max-age=0');

$objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');

//$objWriter->save('php://output'); // 文件通过浏览器下载

exit();
}

控制器方法

public function dcexcel(){
$name='物流信息表';
$header=['商品编号','商品名称','库存数量','商品图'];
/*需要哪张表的数据就查询哪张表的数据*/
$list = db('goods')->limit(1)->select();
$str = array();
foreach ($list as $k => $v) {
/*对应的数据库信息*/
$str[] = [$v['good_sn'],$v['good_name'],$v['store_num'],$v['good_thumb']];
}
$data=$str;
$this->excelExport($name,$header,$data);

}

带图片的导出

/**
* 导出excel表格(带图片)
* @return [type] [description]
*/
function excel_down($data = []){

// 导出Exl
Loader::import("PHPExcel.Classes.PHPExcel");
Loader::import("PHPExcel.Worksheet.Drawing");

$objPHPExcel = new \PHPExcel();

$objWriter = new \PHPExcel_Writer_Excel2007($objPHPExcel);

$objActSheet = $objPHPExcel->getActiveSheet();

// 水平居中(位置很重要,建议在最初始位置)
$objPHPExcel->setActiveSheetIndex(0)->getStyle('A')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$objPHPExcel->setActiveSheetIndex(0)->getStyle('B')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$objPHPExcel->setActiveSheetIndex(0)->getStyle('C')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$objPHPExcel->setActiveSheetIndex(0)->getStyle('D')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$objPHPExcel->setActiveSheetIndex(0)->getStyle('E')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);

$objActSheet->setCellValue('A1', 'good_sn');
$objActSheet->setCellValue('B1', 'good_name');
$objActSheet->setCellValue('C1', 'store_num');
$objActSheet->setCellValue('D1', 'sales_sum');
$objActSheet->setCellValue('E1', 'good_thumb');

// 设置个表格宽度
$objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(16);
$objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(15);
$objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(15);
$objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(20);
$objPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth(12);

// 垂直居中
$objPHPExcel->setActiveSheetIndex(0)->getStyle('A')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$objPHPExcel->setActiveSheetIndex(0)->getStyle('B')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$objPHPExcel->setActiveSheetIndex(0)->getStyle('C')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$objPHPExcel->setActiveSheetIndex(0)->getStyle('D')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$objPHPExcel->setActiveSheetIndex(0)->getStyle('E')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);

foreach($data as $k=>$v){
$k +=2;
$objActSheet->setCellValue('A'.$k, $v['good_sn']);
$objActSheet->setCellValue('B'.$k, $v['good_name']);
$objActSheet->setCellValue('C'.$k, $v['store_num']);
$objActSheet->setCellValue('D'.$k, $v['sales_sum']);

//获取到图片信息
$img =db('goods')->where('good_id = '.$v['good_id'])->field('good_thumb')->find();
// 图片生成
$objDrawing[$k] = new \PHPExcel_Worksheet_Drawing();
$objDrawing[$k]->setPath('./uploads/'.$img['good_thumb']);//这里拼接 . 是因为要在根目录下获取
// 设置宽度高度
$objDrawing[$k]->setHeight(80);//照片高度
$objDrawing[$k]->setWidth(80); //照片宽度
/*设置图片要插入的单元格*/
$objDrawing[$k]->setCoordinates('E'.$k);
// 图片偏移距离
$objDrawing[$k]->setOffsetX(12);
$objDrawing[$k]->setOffsetY(12);
$objDrawing[$k]->setWorksheet($objPHPExcel->getActiveSheet());

// 表格高度
$objActSheet->getRowDimension($k)->setRowHeight(80);

}

$fileName = 'duola物流信息表';
$date = date("Y-m-d",time());
$fileName .= "_{$date}.xls";
$fileName = iconv("utf-8", "gb2312", $fileName);
//重命名表
// $objPHPExcel->getActiveSheet()->setTitle('test');
//设置活动单指数到第一个表,所以Excel打开这是第一个表
$objPHPExcel->setActiveSheetIndex(0);
header('Content-Type: application/vnd.ms-excel');
header("Content-Disposition: attachment;filename=\"$fileName\"");
header('Cache-Control: max-age=0');
$objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output'); //文件通过浏览器下载
// END
}

控制器使用

public function dcexcel(){
/*需要哪张表的数据就查询哪张表的数据*/
$list = db('goods')->limit(1)->select();
$this->excel_down($list);

}

转载于:https://www.cnblogs.com/zdzdbk/p/9957048.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值