$objPHPExcel = new \PHPExcel(); /* 以下是一些设置 ,什么作者 标题啊之类的 */ $objPHPExcel->getProperties()->setCreator("开票明细记录") ->setLastModifiedBy("开票明细记录") ->setTitle("开票明细记录") ->setSubject("开票明细记录") ->setDescription("开票明细记录") ->setKeywords("excel") ->setCategory("result file"); /* 以下就是对处理Excel里的数据, 横着取数据,主要是这一步,其他基本都不要改 */ $num = 0; foreach ($data as $k => $v) { $num ++; if ($num == 1) { $objPHPExcel->setActiveSheetIndex(0) ->setCellValue('A' . $num, '序号') ->setCellValue('B' . $num, '发票抬头') ->setCellValue('C' . $num, '发票号码') ->setCellValue('D' . $num, '发票日期') ->setCellValue('E' . $num, '发票类型') ->setCellValue('F' . $num, '发票金额') ->setCellValue('G' . $num, '快递单号') ->setCellValue('H' . $num, '发票状态') ->setCellValue('Y' . $num, '签收人') ->setCellValue('Z' . $num, '签收日期'); $num++; } $objPHPExcel->setActiveSheetIndex(0) //Excel的第A列,uid是你查出数组的键值,下面以此类推 ->setCellValue('A' . $num, $v['id']) ->setCellValue('B' . $num, $v['title']) ->setCellValue('C' . $num, $v['invoice_number']) ->setCellValue('D' . $num, $v['invoice_date']) ->setCellValue('E' . $num, $v['invoice_type'] . $v['invoice_material']) ->setCellValue('F' . $num, $v['amount']) ->setCellValue('G' . $num, $v['express_number']) ->setCellValue('H' . $num, $v['invoice_status']) ->setCellValue('Y' . $num, $v['title']) ->setCellValue('Z' . $num, $v['modified']); } $objPHPExcel->getActiveSheet()->setTitle('开票明细记录'); $objPHPExcel->setActiveSheetIndex(0); $name = md5(time() . rand(1000, 9999)); header('Content-Type: application/vnd.ms-excel'); header('Content-Disposition: attachment;filename="' . $name . '.xls"'); header('Cache-Control: max-age=0'); $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5'); $objWriter->save('php://output'); exit;
关键字词: