thinkphp3.2与phpexcel基础生成

public function ff(){
import("Org.Util.PHPExcel");
import("Org.Util.PHPExcel.Worksheet.Drawing");
$objPHPExcel = new \PHPExcel();
$objDrawing = new \PHPExcel_Worksheet_Drawing(); $objDrawing->setPath('./images/01.gif');
/*设置图片高度*/
$objDrawing->setHeight();//照片高度
$objDrawing->setWidth(); //照片宽度
/*设置图片要插入的单元格*/
$objDrawing->setCoordinates('M2');
/*设置图片所在单元格的格式*/
$objDrawing->setOffsetX();
$objDrawing->setRotation();
$objDrawing->getShadow()->setVisible(true);
$objDrawing->getShadow()->setDirection();
$objDrawing->setWorksheet($objPHPExcel->getActiveSheet()); $objActSheet = $objPHPExcel->getActiveSheet();
$objActSheet->setCellValue('A1', '');
$objActSheet->setCellValue('B1', '');
$objActSheet->setCellValue('C1', '');
$objActSheet->setCellValue('D1', '');
$objActSheet->setCellValue('E1', '');
$objActSheet->setCellValue('F1', '');
$objActSheet->setCellValue('G8', ''); $date = date("Y_m_d",time());
$fileName .= "_{$date}.xls"; $fileName = iconv("utf-8", "gb2312", $fileName);
//重命名表
// $objPHPExcel->getActiveSheet()->setTitle('test');
//设置活动单指数到第一个表,所以Excel打开这是第一个表
$objPHPExcel->setActiveSheetIndex();
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'); //文件通过浏览器下载 }

可以生成图片加文字

实战:带数据生成

public function look_down(){
$id = I('get.id');
$m = M ('offer_goods');
$where['offer_id'] = $id;
$data = $m->field('goods_sn,goods_name,barcode,goods_type,price')->select(); // 导出Exl
import("Org.Util.PHPExcel");
import("Org.Util.PHPExcel.Worksheet.Drawing");
import("Org.Util.PHPExcel.Writer.Excel2007");
$objPHPExcel = new \PHPExcel();
$objDrawing = new \PHPExcel_Worksheet_Drawing();
$objWriter = new \PHPExcel_Writer_Excel2007($objPHPExcel);
//echo $objWriter->ss();
//exit; $objActSheet = $objPHPExcel->getActiveSheet(); $objActSheet->setCellValue('A1', '商品货号');
$objActSheet->setCellValue('B1', '商品名称');
$objActSheet->setCellValue('C1', '商品图');
$objActSheet->setCellValue('D1', '商品条码');
$objActSheet->setCellValue('E1', '商品属性');
$objActSheet->setCellValue('F1', '报价(港币)');
$objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth();
$objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth();
$objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth();
$objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth();
$objPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth();
$objPHPExcel->getActiveSheet()->getColumnDimension('F')->setWidth(); foreach($data as $k=>$v){
$k +=;
$objActSheet->setCellValue('A'.$k, $v['goods_sn']);
$objActSheet->setCellValue('B'.$k, $v['goods_name']);
$objActSheet->setCellValue('C'.$k, '');
$objActSheet->setCellValue('D'.$k, $v['barcode']);
$objActSheet->setCellValue('E'.$k, $v['goods_type']);
$objActSheet->setCellValue('F'.$k, $v['price']); // 表格高度
$objActSheet->getRowDimension($k)->setRowHeight();
// 垂直居中
$objPHPExcel->getActiveSheet()->getStyle('A'.$k)->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle('B'.$k)->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle('D'.$k)->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle('E'.$k)->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
$objPHPExcel->getActiveSheet()->getStyle('F'.$k)->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
// 水平居中
$objPHPExcel->setActiveSheetIndex()->getStyle('A'.$k)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$objPHPExcel->setActiveSheetIndex()->getStyle('D'.$k)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$objPHPExcel->setActiveSheetIndex()->getStyle('E'.$k)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
$objPHPExcel->setActiveSheetIndex()->getStyle('F'.$k)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
} $fileName = '报价表';
$date = date("Y-m-d",time());
$fileName .= "_{$date}.xls"; $fileName = iconv("utf-8", "gb2312", $fileName);
//重命名表
// $objPHPExcel->getActiveSheet()->setTitle('test');
//设置活动单指数到第一个表,所以Excel打开这是第一个表
$objPHPExcel->setActiveSheetIndex();
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
}
上一篇:js获取浏览器当前窗口的高度长度


下一篇:ACM——回文