PHP导出excle图片与文字

时间:2023-01-25 11:11:17
try {
      //CI框架引入PHPExcel
$this->load->library('PHPExcel');
$objPHPExcel = new PHPExcel();
      //图片处理类
$objDrawing = new PHPExcel_Worksheet_Drawing();
$objPHPExcel->createSheet(0);
$objPHPExcel->setActiveSheetIndex(0);
$currentSheet = $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);
$objPHPExcel->setActiveSheetIndex(0)->getStyle('F')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$objPHPExcel->setActiveSheetIndex(0)->getStyle('G')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
// 设置个表格宽度
$objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(20);
$objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(20);
$objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(20);
$objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(20);
$objPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth(20);
$objPHPExcel->getActiveSheet()->getColumnDimension('F')->setWidth(20);
$objPHPExcel->getActiveSheet()->getColumnDimension('G')->setWidth(20);
// $objPHPExcel->getActiveSheet()->getDefaultRowDimension()->setRowHeight(60);
$currentSheet->setCellValue('A1', "序列号");
$currentSheet->setCellValue('B1', "设备ID");
$currentSheet->setCellValue('C1', "设备密码");
$currentSheet->setCellValue('D1', "设备编码");
$currentSheet->setCellValue('E1', "二维码");
$currentSheet->setCellValue('F1', "二维码");
$currentSheet->setCellValue('G1', "二维码");
$idx = 2;
foreach ($devices as $device) {
$currentSheet->setCellValue('A' . $idx, $device['sequence']);
$currentSheet->setCellValue('B' . $idx, $device['device_unique']);
$currentSheet->setCellValue('C' . $idx, $device['device_pwd']);
$currentSheet->setCellValue('D' . $idx, $device['device_code']);
$currentSheet->getRowDimension($idx) -> setRowHeight(60);
$this->load->model('Device_codes_model');
$codes = $this->Device_codes_model->getOneCodes($id);
$objDrawing = new PHPExcel_Worksheet_Drawing();
$objDrawing->setPath($codes['code_file']); // 设置宽度高度
$objDrawing->setHeight(80);//照片高度
$objDrawing->setWidth(80); //照片宽度
/*设置图片要插入的单元格*/
$objDrawing->setCoordinates('E'.$idx);
// 图片偏移距离
$objDrawing->setOffsetX(12);
$objDrawing->setOffsetY(12);
$objDrawing->setWorksheet($objPHPExcel->getActiveSheet()); $objDrawing = new PHPExcel_Worksheet_Drawing();
$objDrawing->setPath($codes['code_file']); // 设置宽度高度
$objDrawing->setHeight(80);//照片高度
$objDrawing->setWidth(80); //照片宽度
/*设置图片要插入的单元格*/
$objDrawing->setCoordinates('F'.$idx);
// 图片偏移距离
$objDrawing->setOffsetX(12);
$objDrawing->setOffsetY(12);
$objDrawing->setWorksheet($objPHPExcel->getActiveSheet()); $objDrawing = new PHPExcel_Worksheet_Drawing();
$objDrawing->setPath($codes['code_file']); // 设置宽度高度
$objDrawing->setHeight(80);//照片高度
$objDrawing->setWidth(80); //照片宽度
/*设置图片要插入的单元格*/
$objDrawing->setCoordinates('G'.$idx);
// 图片偏移距离
$objDrawing->setOffsetX(12);
$objDrawing->setOffsetY(12);
$objDrawing->setWorksheet($objPHPExcel->getActiveSheet());
/*--------------------------------------聂向阳end----------------------------*/ $idx++;
}
$sheetWrite = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');