PHP导出excel表格
今天给大家说的是php导出excel表格,也是很多项目需要的。
首先先下载PHPExcel,没有的可以到(随便一个网站)http://phpexcel.codeplex.com/releases/view/119187中下载PHPExcel这个包,当然有水平的亲们可以自己写一个。下载好后解压,解压完直接引用,下图是解压以后必须要有的文件
其次,在PHP中包含
最后在附上剩下代码,比较简洁。如果还需要别的excel功能的可以看看我之前的随笔
$objPHPExcel = new PHPExcel();
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel,'Excel5');
ob_end_clean();
header('Content-Type: application/vnd.ms-excel');
header('Content-type:text/xls; charset=utf-8');
header('Content-Disposition: attachment;filename="拱顶沉降日报表.xls"');
header('Cache-Control: max-age=0');
$objPHPExcel->getActiveSheet()->setTitle('拱顶沉降日报表');//设置sheet的name值
$objPHPExcel->getProperties()->setCreator("我啦");//设置创建者
//设置单元格信息
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A1', '开挖方法:')
->setCellValue('F1','围岩类别:')
->setCellValue('A2', '掌子面里程')
->setCellValue('C2','二衬里程')
->setCellValue('E2','仰拱里程')
->setCellValue('G2','二衬到掌子面距离')
->setCellValue('I2','仰拱到掌子面距离')
->setCellValue('A5', '测点编号')
->setCellValue('B5','测点埋设日期')
->setCellValue('C5','测量时间')
->setCellValue('D5','观测值(m)')
->setCellValue('E5','上次观测值(m)')
->setCellValue('F5','上次观测值(m)')
->setCellValue('G5','总沉降量(mm)')
->setCellValue('H5','相对上次沉降(mm)')
->setCellValue('I5','时间间隔')
->setCellValue('J5','沉降速率(nm/d)')
->setCellValue('K5','备注');
//合并单元格
$objPHPExcel->getActiveSheet()->mergeCells('A1:E1');//标准方法
$objPHPExcel->getActiveSheet()//脑洞大开方法
->mergeCells('A2:A4')
->mergecells('B2:B4')
->mergecells('C2:C4')
->mergecells('D2:D4')
->mergecells('E2:E4')
->mergecells('F2:F4')
->mergecells('G2:G4')
->mergecells('H2:H4')
->mergecells('I2:I4')
->mergecells('J2:J4')
->mergecells('K2:K4')
->mergecells('A5:A6')
->mergecells('B5:B6')
->mergecells('C5:C6')
->mergecells('D5:D6')
->mergecells('E5:E6')
->mergecells('F5:F6')
->mergecells('G5:G6')
->mergecells('H5:H6')
->mergecells('I5:I6')
->mergecells('J5:J6')
->mergecells('K5:K6');
//设置保护单元格
/* $objPHPExcel->getActiveSheet()->getProtection()->setSheet(true);
/*$objPHPExcel->getActiveSheet()->protectCells('A2:K6');*/
$objWriter->save('php://output');