用php 生成 excel 表格
1
2
|
//引用新建对象<br>require "../phpexcel/Classes/PHPExcel.php"; $excel = new PHPExcel(); |
建表格
1
2
|
//Excel表格式,这里简略写了4列 $letter = array ( 'A' , 'B' , 'C' , 'D' ,); |
填充值
1
2
3
4
5
6
|
$excel ->getActiveSheet()->setCellValue( "A1" , "差旅费报销单" ); //填充A1的值 $excel ->getActiveSheet()->mergeCells( 'A1:D1' ); //合并行从A1到D1<br>$excel->getActiveSheet()->mergeCells('C7:c10'); //合并列从C7到C10<br> $excel ->getActiveSheet()->setCellValue( "A2" , "姓名" ); $excel ->getActiveSheet()->setCellValue( "B2" , "{$name}" ); $excel ->getActiveSheet()->setCellValue( "C2" , "部门" ); $excel ->getActiveSheet()->setCellValue( "D2" , "{$bm}" ); |
表格样式设置
1
2
3
4
5
6
7
8
9
10
|
$excel ->getActiveSheet()->getColumnDimension( 'A' )->setWidth(13); //设置列的宽度 $excel ->getActiveSheet()->getColumnDimension( 'B' )->setWidth(30); $excel ->getActiveSheet()->getColumnDimension( 'C' )->setWidth(13); $excel ->getActiveSheet()->getColumnDimension( 'D' )->setWidth(30); $excel ->getActiveSheet()->getRowDimension(5)->setRowHeight(100); //设置行的高度数字代表第几行 $excel ->getActiveSheet()->getRowDimension(2)->setRowHeight(25); $excel ->getActiveSheet()->getRowDimension(3)->setRowHeight(25); $excel ->getActiveSheet()->getRowDimension(4)->setRowHeight(25); $excel ->getActiveSheet()->getRowDimension(6)->setRowHeight(25);<br> $excel ->getActiveSheet()->getStyle( 'A1' )->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER); //横向居中<br>$excel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);//纵向居中 |
生成表格
1
2
3
4
5
6
7
8
9
10
11
|
$write = new PHPExcel_Writer_Excel5( $excel ); header( "Pragma: public" ); header( "Expires: 0" ); header( "Cache-Control:must-revalidate, post-check=0, pre-check=0" ); header( "Content-Type:application/force-download" ); header( "Content-Type:application/vnd.ms-execl" ); header( "Content-Type:application/octet-stream" ); header( "Content-Type:application/download" );; header( "Content-Disposition:attachment;filename={$ids}出差报销表.xls" ); //要生成的表名 header( "Content-Transfer-Encoding:binary" ); $write ->save( 'php://output' ); |