1 <?php 2 header("Content-type: text/html; charset=gb2312"); 3 error_reporting(E_ALL); 4 ini_set(‘display_errors‘, TRUE); 5 ini_set(‘display_startup_errors‘, TRUE); 6 7 define(‘EOL‘,(PHP_SAPI == ‘cli‘) ? PHP_EOL : ‘<br />‘); 8 9 date_default_timezone_set(‘Europe/London‘); 10 11 /** PHPExcel_IOFactory */ 12 require_once dirname(__FILE__) . ‘/../Classes/PHPExcel/IOFactory.php‘; 13 $objReader = PHPExcel_IOFactory::createReader(‘Excel5‘); 14 //设置模板文件 15 $objPHPExcel = $objReader->load("templates/bills.xls"); 16 //将json字符串转换为数组 17 $billData = json_decode($_REQUEST["billData"],true); 18 //填充单元格 19 $objPHPExcel->getActiveSheet()->setCellValue(‘B4‘, $billData[‘party‘]) 20 ->setCellValue(‘B5‘, $billData[‘game‘]) 21 ->setCellValue(‘A9‘, $billData[‘month‘]) 22 ->setCellValue(‘I28‘, $billData[‘total‘]) 23 ->setCellValue(‘H30‘, $billData[‘partyaccount‘][‘name‘]) 24 ->setCellValue(‘H31‘, $billData[‘partyaccount‘][‘bank‘]) 25 ->setCellValue(‘H32‘, $billData[‘partyaccount‘][‘account‘]) 26 ->setCellValue(‘H33‘, $billData[‘partyaccount‘][‘address‘]) 27 ->setCellValue(‘H34‘, $billData[‘partyaccount‘][‘contactpeople‘]) 28 29 ->setCellValue(‘H35‘, $billData[‘partyaccount‘][‘contacttel‘]); 30 31 //循环填充相同标题单元格 32 $row=11; 33 foreach($billData[‘bills‘] as $r => $dataRow) { 34 $objPHPExcel->getActiveSheet()->setCellValue(‘B‘.$row, $dataRow[‘cardtype‘]) 35 ->setCellValue(‘c‘.$row, $dataRow[‘orderamount‘]) 36 ->setCellValue(‘D‘.$row, $dataRow[‘feerate‘]) 37 ->setCellValue(‘E‘.$row, $dataRow[‘fee‘]) 38 ->setCellValue(‘F‘.$row, $dataRow[‘amount‘]) 39 ->setCellValue(‘G‘.$row, $dataRow[‘partyratio‘]) 40 ->setCellValue(‘H‘.$row, $dataRow[‘partynumber‘]) 41 ->setCellValue(‘I‘.$row, $dataRow[‘partyamount‘]) 42 ->setCellValue(‘J‘.$row, $dataRow[‘memo‘]); 43 $objPHPExcel->getActiveSheet()->getStyle(‘c‘.$row)->getBorders()->getBottom()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN); 44 $row++; 45 } 46 47 $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, ‘Excel5‘); 48 49 $str= mb_convert_encoding("out/bill_".$billData[‘party‘].‘_‘.$billData[‘game‘].‘_‘.$billData[‘month‘].".xls","gb2312","UTF-8"); 50 $objWriter->save($str); 51 //让访问浏览器直接下载文件流 52 Header("Location: /out/bill_".$billData[‘party‘].‘_‘.$billData[‘game‘].‘_‘.$billData[‘month‘].".xls");