การจัดแต่งเล็กน้อย จะทำให้รายงานออกมาดูน่าสนใจ จะยกตัวอย่างการใช้ style, การ merge cells, pinned (freeze) cell ไว้ไม่ให้ขยับ เช่น fixed หัวตารางไว้ให้อยู่กับที่ และการ flip กลับตัวอักษร
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 | <?php /* PHPExcel_IOFactory - Reader */ include 'PHPOffice/PHPExcel/Classes/PHPExcel.php' ; $objPHPExcel = new PHPExcel(); /* Set default style */ $defaultStyle = $objPHPExcel ->getDefaultStyle(); $defaultStyle ->getFont() ->setName( 'Arial' ) ->setSize(11); /* Set document properties */ $title = 'Exports_Datas_' . date ( 'Y-m-d_H:i' ); $objPHPExcel ->getProperties()->setCreator( 'Pitt Phunsanit' ) ->setCategory( 'Exports Datas' ) ->setDescription( $title ) ->setKeywords( 'Exports Datas ' . date ( 'Y-m-d' )) ->setSubject( $title ) ->setTitle( $title ); /* rename sheet */ $objWorkSheet = $objPHPExcel ->getActiveSheet(); $objWorkSheet ->setTitle( 'Exports Datas' ); $styles = [ 'tableHeader1' => [ 'alignment' => [ 'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER, ], 'fill' => [ 'color' => [ 'rgb' => 'C00000' ], 'type' => PHPExcel_Style_Fill::FILL_SOLID, ], 'font' => [ 'color' => [ 'rgb' => 'ffffff' ], 'name' => 'Arial' , ], 'name' => 'Arial' , ], 'tableHeader2' => [ 'alignment' => [ 'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER, ], 'fill' => [ 'color' => [ 'rgb' => '00c000' ], 'type' => PHPExcel_Style_Fill::FILL_SOLID, ], 'font' => [ 'color' => [ 'rgb' => 'ffffff' ], 'name' => 'Arial' , ], 'name' => 'Arial' , ], 'tableRow' => [ 'alignment' => [ 'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_LEFT, ], 'bold' => true, 'fill' => [ 'color' => [ 'rgb' => '0000c0' ], 'type' => PHPExcel_Style_Fill::FILL_SOLID, ], 'font' => [ 'color' => [ 'rgb' => 'ffffff' ], 'name' => 'Arial' , ], 'name' => 'Arial' , ], ]; /* raw datas */ $headers1 = [ '2557' , '2558' , '2559' ]; $headers2 = [ 'Shop A' , 'Shop B' ]; $headersRows = [ 'มกราคม' , 'กุมภาพันธ์' , 'มีนาคม' , 'เมษายน' , 'พฤษภาคม' , 'มิถุนายน' , 'กรกฎาคม' , 'สิงหาคม' , 'กันยายน' , 'ตุลาคม' , 'พฤศจิกายน' , 'ธันวาคม' ]; /* make table */ $header1ColNo = 1; $header2ColNo = 1; $header2PerHeaders1 = count ( $headers2 ); foreach ( $headers1 as $header1 ) { $header1CoordinateStart = PHPExcel_Cell::stringFromColumnIndex( $header1ColNo ) . '1' ; $header1CoordinateEnd = PHPExcel_Cell::stringFromColumnIndex( $header1ColNo + $header2PerHeaders1 - 1) . '1' ; $objWorkSheet ->getStyle( $header1CoordinateStart )->applyFromArray( $styles [ 'tableHeader1' ]); $objWorkSheet ->mergeCells( $header1CoordinateStart . ':' . $header1CoordinateEnd ); $objWorkSheet ->setCellValue( $header1CoordinateStart , $header1 ); /* set column */ $header1ColNo += $header2PerHeaders1 ; foreach ( $headers2 as $header2 ) { $coordinate = PHPExcel_Cell::stringFromColumnIndex( $header2ColNo ) . '2' ; $objWorkSheet ->getStyle( $coordinate )->applyFromArray( $styles [ 'tableHeader2' ]); $objWorkSheet ->setCellValue( $coordinate , $header2 ); $header2ColNo ++; } } $rowNo = 3; foreach ( $headersRows as $header ) { $coordinate = 'A' . $rowNo ; $objWorkSheet ->getStyle( $coordinate )->applyFromArray( $styles [ 'tableRow' ]); $objWorkSheet ->setCellValue( $coordinate , $header ); $rowNo ++; } /* random add datas */ $highestColumn = 1 + ( count ( $headers1 ) * count ( $headers2 )); $highestRow = 2 + count ( $headersRows ); for ( $rowNo = 3; $rowNo <= $highestRow ; $rowNo ++) { for ( $colNo = 1; $colNo < $highestColumn ; $colNo ++) { $colString = PHPExcel_Cell::stringFromColumnIndex( $colNo ); $coordinate = $colString . $rowNo ; $objWorkSheet ->setCellValue( $coordinate , rand(0 , 100)); } } /* right label */ $colString = PHPExcel_Cell::stringFromColumnIndex( $colNo ); $rowNo --; $objWorkSheet ->getStyle( $colString . '3' )->getAlignment() ->setTextRotation(-90) ->setVERTICAL(PHPExcel_Style_Alignment::VERTICAL_TOP); $objWorkSheet ->mergeCells( $colString . '3:' . $colString . $rowNo ); $objWorkSheet ->setCellValue( $colString . '3' , 'Gross Income Per Month' ); /* freeze pinned head column */ $objPHPExcel ->getActiveSheet()->freezePane( $colString . '3' ); $objWriter =PHPExcel_IOFactory::createWriter( $objPHPExcel , 'Excel2007' ); header( 'Content-Type: application/vnd.ms-excel' ); header( 'Content-Disposition: attachment;filename="' . $title . '.xlsx"' ); header( 'Cache-Control: max-age=0' ); header( 'Cache-Control: no-store, no-cache, must-revalidate, max-age=0' ); header( 'Cache-Control: post-check=0, pre-check=0' , false); header( 'Pragma: no-cache' ); $objWriter ->save( 'php://output' ); |
About the author